Question #39951

The execution goes back to the caller by default, when the last statement in a method is
reached.? Assuming that no return statement is used, do you agree with the preceding statement?
Specify the reason for your answer.
1

Expert's answer

2014-03-17T11:40:58-0400

Here is the processed document with the code fragments restored and formatted:

Answer on Question#39951- Programming, C#

1. The execution goes back to the caller by default, when the last statement in a method is reached.? Assuming that no return statement is used, do you agree with the preceding statement? Specify the reason for your answer.

Solution.

Methods can return a value to the caller. If the return type, the type listed before the method name, is not void, the method can return the value by using the return keyword. A statement with the return keyword followed by a value that matches the return type will return that value to the method caller. The return keyword also stops the execution of the method. If the return type is void, a return statement without a value is still useful to stop the execution of the method. Without the return keyword, the method will stop executing when it reaches the end of the code block. Methods with a non-void return type are required to use the return keyword to return a value.


public int Add(int a, int b)
{
    int result = a + b;
    return result;
}
public void PrintMessage(string message)
{
    Console.WriteLine(message);
    return; // Optional in this case, but stops the method execution
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!
LATEST TUTORIALS
APPROVED BY CLIENTS