What happens to a parameter passed by value that is modified locally?

后端 未结 5 379
粉色の甜心
粉色の甜心 2021-01-24 19:53

I am well aware that modifying a function\'s argument that is passed by value is ineffective outside of the C/C++ function, but compilers allow it - but what happens? Is a loca

5条回答
  •  夕颜
    夕颜 (楼主)
    2021-01-24 20:31

    A value parameter of a function is effectively a local variable of the function, except that it is initialised in the function call. So these:

     void f( int n ) {
          n++;
       }
    

    and:

     void g() {
          int n = 0;
          n++;
       }
    

    are effectively the same, if the call to f() was made as f(0). In both cases, the variable will be discarded on function exit.

提交回复
热议问题