Modify method parameter within method or return result

前端 未结 7 2395
余生分开走
余生分开走 2020-12-05 02:47

What is the difference between

private void DoSomething(int value) {
    value++;
}

and

private int DoSomething(int value)          


        
7条回答
  •  独厮守ぢ
    2020-12-05 03:00

    Since you are using postfix ++ the value returned is the original value of the number. Additionally since you are passing a copy of the number and not the number itself (ie by reference) the changes made to value don't affect the variable you passed.

    So a program like this:

    int value=1;
    std::cout<

    Should output as follows:

    1
    1
    1
    

    If you were to use prefix ++ in the returning or if you were to pass by reference in the non-returning function the same program would output as follows.

    1
    2
    3
    

    Hope that this helps.

提交回复
热议问题