What is the difference between
private void DoSomething(int value) { value++; }
and
private int DoSomething(int value) { return value++; }
when used as either
DoSomething(value);
versus
value = DoSomething(value);
In general you can use the return value as a method parameter in the same way as you can use any variable.
Modifying a parameter does not modify the corresponding argument passed by the function call. However, because arguments can be addresses or pointers, a function can use addresses to modify the values of variables defined in the calling function.
The called function can modify the value of the argument by using its reference passed in. The following example shows how arguments are passed by reference. The reference parameters are initialized with the actual arguments when the function is called.
Unlike many other languages, Java has no mechanism to change the value of an actual parameter.
You are talking about the difference between passing by reference and passing by value, which is conceptually similar to the idea of value types vs reference types.
If you pass a value type into the method, you have to use the second example; otherwise you are just incrementing an integer that exists inside the scope of DoSomething(). Try it: if you execute your first example, after DoSomething() has run, the value of your int will be unchanged.
However, if you are passing in something other than a value type (say object foo), you are actually passing a reference to the original object. Anything you do to it inside DoSomething() will take effect outside the method as well, since you are still referring to the same object.
You can accomplish what you're attempting in the first example by writing:
void DoSomething(ref int value)
That instructs .NET to pass a reference to the item regardless of whether it is a value type.
See this writeup on Value Types vs Reference Types on MSDN for a more detailed look.
Additionally, as zodoz points out (upvote appropriately), by returning value++
you are returning and then incrementing. To return the incremented value, use ++value
.
Return a value.
Why?
Correctness, Readability, and Self-Documentation
Intentional and easy to understand code is better than side-effect code. Consider:
float area = pi * Square(r);
vs.
Square(r); float area = pi * r; // ... intervening code float x = r * 5; // did you mean to use the original r or r-squared here?
Also consider the advantages of terseness through composability in the first example.
Consider the methods themselves, compare:
int DoSomething(int value) { return value+1; }
Which is pretty obviously correct. vs.
void DoSomething(int value) { value++; }
Which seems right and will compile just fine but is actually just a no-op. What you really want is this:
void DoSomething(ref int value) { value++; } // client code: DoSomething(ref a);
Variables are Cheap
Many well-named variables is preferable over few reused general purpose variables. Resist the temptation to prematurely optimize, the chance that you will need to cut down on the number of local variables to improve the performance of your system is cosmically tiny. Again, Variables are Cheap, DON'T REUSE VARIABLES!
Testability
Consider:
Assert.IsTrue(Square(2) == 4);
vs.
float a = 2; Square(a); Assert.IsTrue(a == 4);
There are many other advantages to avoiding mutation in preference to returning a value. It's not merely an accident that mathematics defines a function as a mapping of input values to output values.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With