+ 1

Does the return breaks the method?

when the return ... runs does it breaks method or it continues

14th Sep 2017, 9:00 PM
MAHER TAYARA
MAHER TAYARA - avatar
4 Réponses
+ 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.
14th Sep 2017, 9:08 PM
Rrestoring faith
Rrestoring faith - avatar
+ 12
It returns from the method immediately without executing the code after the return statement.
14th Sep 2017, 9:06 PM
Tashi N
Tashi N - avatar
+ 5
It is like a break and an equal cimbined, for a function. But using it outside a function yields an error
15th Sep 2017, 1:39 AM
👑 Prometheus 🇸🇬
👑 Prometheus 🇸🇬 - avatar
+ 3
as i understood, the called method will not continue after the return statement (Thanks for explanation 👍)
14th Sep 2017, 9:08 PM
MAHER TAYARA
MAHER TAYARA - avatar