I didn't get "ref" goal | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 2

I didn't get "ref" goal

I come from JS/PHP/SQL/... languages, now I'm curious to learn C# to play a while with Unity. I'm at the chapter talking about "ref", I have read many times the lesson but didn't get the philosophy of passing argument using "ref". Some help please?

5th Mar 2017, 6:21 PM
Geoffrey L
Geoffrey L - avatar
5 Answers
+ 1
The concept of "call by reference" exists in PHP as well. It is about the way of passing variables. The default way - "call by value" doesn't really pass the variable, it makes a copy of it, so what happens within function stays in the function, except you return it explicitly. The second method, "call by reference" passes the address of the original variable, so each change will apply to the original as well. The full explanation is of course a bit more complicated.
5th Mar 2017, 6:43 PM
spotbot2k
spotbot2k - avatar
+ 1
Hmmm no I realize that my exemple is more that ugly and incorrect, I think I get it. With ref it really works with the TRUE variable and without it kind of copy it value and use it without update the root variable until using return?
5th Mar 2017, 6:52 PM
Geoffrey L
Geoffrey L - avatar
0
Oh so if I say it in a (very) ugly way, that would be similar as something like: myFunction( arg1 ){ blah...blah... } myFunction( return arg1 ) (I said ugly... xD)
5th Mar 2017, 6:48 PM
Geoffrey L
Geoffrey L - avatar
0
I need to -1 myself for my very ugly exemple xD
5th Mar 2017, 6:52 PM
Geoffrey L
Geoffrey L - avatar
0
Looks like right direction. You will get it, once you'll really need it. +1 you again.
5th Mar 2017, 6:58 PM
spotbot2k
spotbot2k - avatar