What does return mean
What does return mean in c# ? like
return; return a + b;
Answer by tanoshimi · Nov 05, 2016 at 10:31 AM
This is not really a Unity question... but anyway:
Functions generally have none or more inputs (parameters) and return none or one output.
The return
statement simply specifies what the output returned from a function is.
return;
on its own means there is no output.
return a+b;
means that the result of a+b is output to the calling method.
So, for example in a function that doesn't have an output, i.e. a void;
void Update ()
{
//Do something
return;
//Do something else
}
In the above, "Do something" is executed, however the function is "finished" afterwards and as such does not execute "something else". The above is mostly pointless, but if you used it with a conditional you can stop certain parts of code from "working" as such. If the function has an output, i.e.
float $$anonymous$$yFunction ()
{
return 3;
}
This will return a value of "3" or whatever you want it to, and you can then use that as whatever type it is, i.e.
public float myNumber;
float $$anonymous$$yFunction ()
{
return 3;
}
void Update ()
{
myNumber = $$anonymous$$yFunction () * 2; //This will set the "myNumber" variable to 6
}