+ 1
Does the return breaks the method?
when the return ... runs does it breaks method or it continues
4 Answers
+ 7
It will break the method.
But break is sort of a miss-leading word to use.
When a method is returned, it terminates.
However, the value of what was returned (if any) is given as the function call.
Here's some examples/
static void ex(){
return;
Console.Write("After return!");
}
Then if I call the method:
ex();
There is no output.
Since 'return' terminated the method before the statement 'Console.Write("...")' could run.
But you can also do something like this:
int pi = getPi();
static float getPi(){
return 3.14;
// terminates the method, and returns 3.14
}
Now the variable pi will have the value of what the function 'getPi()' returned. Which was 3.14.
So, pi is 3.14.
+ 12
It returns from the method immediately without executing the code after the return statement.
+ 5
It is like a break and an equal cimbined, for a function.
But using it outside a function yields an error
+ 3
as i understood, the called method will not continue after the return statement
(Thanks for explanation đ)