1

Trying to get it to subtract down from i by 4's and return the final results (answer is either: 1, 2, 3 or 4)

for(uint i = 1000; i >= 1; i-=4)    { 

            return(i);  

        }

but when I return(i); or return i; i get the initial value of i (in this case 1000) not the subtracted value.

Any explanations or help is appreciated.

  • 1
    Are you trying to replicate the modulus operator? (%) This is the same as remainder. Thus 1000 % 4 = 0 because 4 divides 1000 perfectly. If you want results between 1-4, you can have a check for 0 and turn it into 4. – Shawn Tabrizi Aug 27 '18 at 22:46
  • im not dividing, im subtracting. here is a simpler example: 10 - 4 = 6, 6 - 4 = 2 -- the result (i) should be "2" – jon Aug 27 '18 at 23:53
  • Repeated subtraction is equal to division. It appears to me you are looking for the remainder of 10/4. That is written as 10 % 4 which is equal to 2. You can try yourself in google search. – Shawn Tabrizi Aug 27 '18 at 23:55
2

It exits/breaks from the loop on the first iteration of return.

It sounds like you're expecting it to return multiple times but only one response is possible.

Hope it helps.

EDIT

Based on the comment below, a loop isn't the solution. You want a modulo (remainder) and this can be done in one step.

pragma solidity 0.4.24;

contract Modulo {

    function getMod4(uint number) public pure returns(uint modulo) {
        return number % 4;
    }
}
  • i want the "last" result returned, ie, when it cant subtract 4 anymore and get a number greater than 1. for example, lets start with 10. so 10 - 4 = 6, then 6 - 4 = 2 and since we cant subtract 4 and get a number greater than 1, 2 would be our answer and the desired return for i. – jon Aug 27 '18 at 23:52
  • Have a look at the gist posted above. It works perfectly in Remix so you can try it out. Input 10 returns 2. The answer, though, is 0,1,2 or 3. – Rob Hitchens - B9lab Aug 28 '18 at 0:13
  • 1
    You can see how @Shawn catches the 0 case and just swaps it for a 4 to strictly match the results you expect. – Rob Hitchens - B9lab Aug 28 '18 at 0:14
2

More specifically, as soon as you call return, the whole function which contains this loop will stop and return whatever you passed in the return statement. Think of return as a hard stop to any function execution.

In this case, as soon as return(i) runs the first time, the loop will stop, which is why you are getting back 1000, which is the value you started with.

If you want to return the last value i you need to do something like this:

function remainder() public pure returns(uint) {
  uint r;
  for(uint i = 1000; i >= 1; i-=4) { 
    r = i;  
  }

  return r;
}

But this is super inefficient and will waste a bunch of gas (if called within another function). You should be using the modulus operator for this kind of math. In solidity, the modulus operator is represented by %.

Here is an example of a function which does what I think you want:

function remainderBetter(uint i) public pure returns(uint){
    uint r = i % 4;
    if (r == 0) {
        return 4;
    }

    return r;
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.