Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Modify method parameter within method or return result

Tags:

c#

.net

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); 
like image 959
JMS Avatar asked Feb 17 '09 03:02

JMS


People also ask

Can the return value of a method be used as a parameter?

In general you can use the return value as a method parameter in the same way as you can use any variable.

Why can we not modify parameter variables in a function?

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.

Can a function modify parameter?

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.

Can you change parameter values in Java?

Unlike many other languages, Java has no mechanism to change the value of an actual parameter.


2 Answers

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.

like image 180
Rex M Avatar answered Sep 22 '22 10:09

Rex M


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.

like image 37
Wedge Avatar answered Sep 23 '22 10:09

Wedge