문제

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.

도움이 되었습니까?

해결책 2

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); 
}

다른 팁

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

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); 
}

You're passing x by value, so calculate only changes a local copy. When calculate returns, the result is lost. You need to return the modified value from calculate and assign it to something in main.

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); 
}

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.

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"

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top