Modify method parameter within method or return result

前端 未结 7 2399
余生分开走
余生分开走 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 02:59

    With most programming languages, you would need to change up your void function by passing the parameter by reference. A function usually can't change the value of its parameters; instead, it creates a copy of the parameter and works with that instead.

    In order to work with the actual variable, you have to change the function header to accept a reference to the variable, with a preceding ampersand (&) like this:

    private void DoSomething(int &value)
    

    Hope that helps!

提交回复
热议问题