When pass a variable to a function, why the function only gets a duplicate of the variable?

前端 未结 9 1293
野的像风
野的像风 2020-12-06 18:05

When pass a variable to a function, why the function only gets a copy/duplicate of the variable?

int n=1;

void foo(int i)
{
    i++;
}

As

9条回答
  •  既然无缘
    2020-12-06 18:31

    Depending on what you're trying to do, you can do this:

    int n = 1;

    n = foo(n);

    Just make sure foo(int i) returns i after modifying it.

提交回复
热议问题