Scope and return values in C++

前端 未结 6 1211
北恋
北恋 2020-11-30 01:44

I am starting again with c++ and was thinking about the scope of variables. If I have a variable inside a function and then I return that variable will the variable not be \

6条回答
  •  攒了一身酷
    2020-11-30 02:26

    When you return a value, a copy is made. The scope of the local variable ends, but a copy is made, and returned to the calling function. Example:

    int funcB() {
      int j = 12;
      return j;
    }
    
    void A() {
      int i;
      i = funcB();
    }
    

    The value of j (12) is copied and returned to i so that i will receive the value of 12.

提交回复
热议问题