user3589718
user3589718

Reputation: 469

Simple calculation inside a function

I'm trying to do the calculation inside a function, but i'm not sure why it's not working:

int calculate(int x){
  x = x + 2;
  return x;
}

int main(){
  int x = 0;
  int i;

  for(i=0;i<10;i++){
    calculate(x);
  }

  printf("i = %d, x = %d\n", i, x); 
}

I understand that x is 0 every time it passes through the function. But how do I fix it? Supposedly i should return 10, and x should return 20.

Upvotes: 0

Views: 319

Answers (6)

devesh48
devesh48

Reputation: 81

In the function you can just change one thing and iguess it will work

Int calculate(int &x)

And keep rest other things same Basically u can use "alias"

Upvotes: 0

Peter Varo
Peter Varo

Reputation: 12150

You can actually pass the pointer of that integer you want to change, not the value itself. In that case, the new (increased) integer will be stored in the original level of scope (actually at the exact same memory spot), where it was defined, which is in this case is your main function. So your code, should look like this:

void calculate(int *x)
{
    *x += 2;
}

int main(void)
{
    int x = 0;
    for (int i=0; i<10; i++)
    {
        calculate(&x);
        printf("i=%d, x=%d\n", i, x);
    }
    return 0;
}

OUTPUT:

i=0, x=2
i=1, x=4
i=2, x=6
i=3, x=8
i=4, x=10
i=5, x=12
i=6, x=14
i=7, x=16
i=8, x=18
i=9, x=20

Upvotes: 2

that other guy
that other guy

Reputation: 123440

Variables can shadow each other. You don't have to ensure that you never, ever use i anywhere else in fear of messing with the i in your for loop, because a new scope will get a new copy of the same name (like when two different people have the same name).

To fix this, you can return the value from your calculate function. I named it x2 to clearly differentiate it from your original x:

int calculate(int x2){
  x2 = x2 + 2;
  return x2;
}

int main(){
  int x = 0;
  int i;

  for(i=0;i<10;i++){
    x = calculate(x);
  }

  printf("i = %d, x = %d\n", i, x); 
}

Upvotes: 1

Nathan
Nathan

Reputation: 33

Change:

for(i=0;i<10;i++){
    calculate(x);
}

to:

for(i=0;i<10;i++){
    x = calculate(x);
}

Your function returns a value, thus you need to store it somewhere.

Upvotes: 0

Cameron Kerr
Cameron Kerr

Reputation: 1875

You're passing in x as a value (ie. it is copied). So x inside the calculate function is not the same as x outside of it. Thus, when you change its value, the change is not reflected in the x that is in main.

The following would be preferable. Note that you need to return a value from the calculate function, and then assign what it returns to some value.

int calculate(int x){
  return x + 2;             /* CHANGED */
}

int main(){
  int x = 0;
  int i;

  for(i=0;i<10;i++){
    x = calculate(x);       /* CHANGED */
  }

  printf("i = %d, x = %d\n", i, x); 
}

Upvotes: 0

Jiminion
Jiminion

Reputation: 5168

If you want x to change, you need to pass it by reference, not by value.

void calculate(int *x){
  *x = *x + 2;
}

int main(){
  int x = 0;
  int i;

  for(i=0;i<10;i++){
    calculate(&x);
  }

  printf("i = %d, x = %d\n", i, x); 
}

Upvotes: 0

Related Questions