0

What some use cases for this methods of passing arguments?

I dont see myself using any other this method but the passby. If I need to pass out values out of a method, why not just use the return keyword?

4th Jun 2017, 4:07 PM
James Smith
James Smith - avatar
5 ответов
+ 3
return is not related to passing arguments, so I'm not sure what you mean by that. I can give you some examples, but if you really just practice you'll be using them all the time. int add(int a, int b){ return a + b; } int a and b are what we pass as arguments. Now we can just call add(int, int) to always add together those 2 numbers. You use these all the time. Ex/ Console.Write() accepts arguments, printing them to the console. Anything that's a method where you put something in the parenthesis is passing something as arguments. void print(string msg){ Console.WriteLine(msg); } Now whenever we call print(string) whatever our message is will be printed. print("Test"); Output: Test
4th Jun 2017, 4:43 PM
Rrestoring faith
Rrestoring faith - avatar
+ 2
https://code.sololearn.com/cX73VP0vlmGI it is a matter of style. I mostly use return as method to pass results. But in more complex situation you want to use ref. I could image reading a record form a database. using a bool to confirm that the action is executed succesfully and return the the record in the ref-parameter.
4th Jun 2017, 7:30 PM
sneeze
sneeze - avatar
+ 2
Oh, he meant passing references? The problem is in order to pass a reference you already need a reference. When I'm just returning I don't need anything, I can just use it as a value. Example: Console.WriteLine(double(2)); int double(int a){ return a*2; } Or int a = 2; double(ref 2); WriteLine(a); void double(ref int a){ a *= 2; } Why create an int if I don't need to?
4th Jun 2017, 7:41 PM
Rrestoring faith
Rrestoring faith - avatar
+ 1
True. I did it for demonstrating the system. It is not usefull in my code. ref could be usefull in more complex situation or when returning multiple parameters.I do no use it a lot.
4th Jun 2017, 7:47 PM
sneeze
sneeze - avatar
0
Thanks for the answer guys. The answers clarified my question a bit.
4th Jun 2017, 9:27 PM
James Smith
James Smith - avatar