0

im confronted with some code and i wondered if there is a need for the return value in this method. the method changes some values of an two-dimensonal double array.

public double[,] startsimplex_werte(double[,]f)
{
    double standardabstand_s = 1.0;

    double[] vektor_e1 = newdouble[2]{1.0,0.0};
    double[] vektor_e2 = newdouble[2]{0.0,1.0};
    f[1,1]=f[0,1]+vektor_e1[0]*standardabstand_s;
    f[1,2]=f[0,2];
    f[2,1]=f[0,1];
    f[2,2]=f[0,2]+vektor_e2[1]*standardabstand_s;

    return f;
}

theres an 2 dimensional array called:

double[,]f = new double[3,3];

so then the method is used on the array:

f = berechne.startsimplex_werte(f);

its not important what the method does in detial. my question is:

is it necessary to return the values of the array by writing "return f " in the method. since arrays are reference types, there is a call by reference. so the method already changes the actual data members of the array. i would have written the same method with no return value (void method) and just call the method without assigning it to the array. imo its just important to transfer the array via parameter. both variantes would do the same, or am I wrong?

I hope somebody can tell me if my suggestion is true.

dcastro
  • 66,540
  • 21
  • 145
  • 155
Julian Herbold
  • 537
  • 9
  • 20
  • You need not return f, you can create a new two dimensional array and return it without touching the parameter. In your case you dont need to so this becuase f's only purpose is as a return object. – Jimmy Feb 20 '14 at 10:47

3 Answers3

3

You're right, you don't need to return the array.

You're modifying its content. If you were creating a whole new instance, then you'd have to either return it or pass the array using the ref/out keywords.

dcastro
  • 66,540
  • 21
  • 145
  • 155
  • Personally I would add `ref` here anyway, just to make sure that the caller is aware the function will *change* the input. – CompuChip Feb 20 '14 at 10:48
  • @CompuChip You should not use the `ref` keyword for readability. You're misusing a language feature. – dcastro Feb 20 '14 at 10:50
  • Parameter f is a reference to an object in memory. There is no need to pass it by reference (using "ref" keyword). – semao Feb 20 '14 at 10:50
  • @CompuChip- ref will change the meaning of the parameter. It will let the reference of the object changed within the function. – Jimmy Feb 20 '14 at 10:51
  • thank you, those answers help me to improve my understanding in programming – Julian Herbold Feb 20 '14 at 10:53
  • 1
    @CompuChip Please take a look at these two posts: [Ref Abuse: Worth Cleaning Up?](http://stackoverflow.com/q/748878/857807) and [Should I use “ref” to pass a collection by reference to a method?](http://stackoverflow.com/a/3473560/857807). – dcastro Feb 20 '14 at 10:58
  • Thanks @dcastro, that's useful to know. Is there any other way to notify the client of your method that the collection will be changed in the method then, or should they just RTFM (where M == XmlDoc)? – CompuChip Feb 20 '14 at 11:12
  • 1
    @CompuChip They should just RTFM :P Also, as a client, you should be aware that members of your reference type instances *may* change. But most importantly, that's why we should never underestimate the importance of choosing *good* names for methods. They should speak for themselves, and communicate your intention clearly. – dcastro Feb 20 '14 at 11:16
3

You're right, in this case there's no need to return f as it is always the same as the input.

There are a few reason why this might have been done:

  • It does allow the function to allocate a new array and return that, if necessary
  • It's a more "functional" approach to API design
  • If may be part of an API that uses the convention of taking an array and always returning one, and the author has chosen to follow this convention for consistency.
Sean
  • 60,939
  • 11
  • 97
  • 136
0

The problem is, that although it is call by reference, only a copy of that reference is passed to your method and not the original reference.

As long as you are modifing the content of the array both references are still pointing to the same object. But if you assign a new array within the method this change would not affect the object outside of your method.

So in general it is better to use the ref keyword in your method if you want to make sure that the orginial object reflects the changes or to use a return value of that method.

But in this special case you don't need a return value or the keyword ref. But again, better use a return value or ref, depending on what you are doing.

user1567896
  • 2,398
  • 2
  • 26
  • 43
  • It is **NOT** a best practice to needlessly use the `ref` keyword. See these two posts: [Ref Abuse: Worth Cleaning Up?](http://stackoverflow.com/q/748878/857807) and [Should I use “ref” to pass a collection by reference to a method?](http://stackoverflow.com/a/3473560/857807). – dcastro Feb 20 '14 at 11:00
  • I not said that using `ref` is best practise, but that using `return values` or `ref` are best practice and that is correct, depending on what you are doing. – user1567896 Feb 20 '14 at 11:01
  • Your last paragraph suggests otherwise – dcastro Feb 20 '14 at 11:02
  • I changed my last paragraph to prevent missunderstandings – user1567896 Feb 20 '14 at 11:04