C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML
A return statement is a jump statement. It transfers control unconditionally to the end point of the call site in the stack frame.
Example. First, the return statement is classified as a jump statement. It affects control flow. And control flow models the path of the execution engine when it manipulates evaluation stacks in methods.
Then: Whenever an argument to a method is encountered, this token is pushed onto the stack and the new method is invoked.
In the called method, three kinds of memory are allocated: the parameter slots, the local variable slots, and the evaluation stack. When a non-void return is reached, there must be one value on the stack and that value is returned.
C# program that uses return statement using System; class Program { static void Main() { // Call four methods and store their return values. int value1 = MethodA(); string value2 = MethodB(true); string value3 = MethodB(false); int value4 = MethodC(5, 2); // Display the results. Console.WriteLine(value1); Console.WriteLine(value2); Console.WriteLine(value3); Console.WriteLine(value4); // Invoke a void method. MethodD(); } static int MethodA() { // This method returns a constant integer. return 100; } static string MethodB(bool flag) { // This method returns a string reference based on the flag. return flag ? "cat" : "dog"; } static int MethodC(int operand1, int operand2) { // This method returns an integer based on an expression. return (operand1 * 10) + operand2; } static void MethodD() { // This method uses a return statement with no expression (void). Console.WriteLine("MethodD executed"); return; } } Output 100 cat dog 52 MethodD executed
The three methods MethodA, MethodB, and MethodC all return a value that they compute. MethodA loads the constant integer 100 and returns that value to the call site. This method is small—so it is often inlined by the JIT compiler.
MethodB accepts a parameter of type boolean. This method first evaluates the ternary operator expression and returns the value of that evaluation. The logic contained in the method is not returned, just the value of the evaluation.
Tip: You can find more details about the ternary operator. It is a special representation of an if-statement.
MethodC shows another way an expression can be evaluated in the return statement context. The two parameters to this method are termed operands and are used in a mathematical computation which multiplies the first operand by ten.
And: The integer resulting from this expression is placed on the stack and this single 4-byte value is copied to the call site.
Reachability. The specification describes reachability and end points when discussing statements such as the return statement. The return statement has an end point but this end point is never reached, and is termed unreachable.
We can use the concepts of the reachability and end points when evaluating whether code will be executed and whether it is unnecessary, as it influences control flow. And removing unused code influences performance.
Void. The void keyword provides a way to specify that no specific value is returned from the method. You must use the void keyword in the method signature. Void is a term that was inherited from older C-like languages.
Void methods do not always require a return statement. Sometimes you can use a return in a strategic place to avoid executing unnecessary statements when you have encountered an exit condition.
Expressions. You could specify if-statements and other selection statements to compute the value of a local variable and simply return that value, but this approach is more verbose. Expressions can instead be used to condense high-level code.
Normally: This has no performance impact, although sometimes using local variables can improve or degrade speed.
Other returns. Many programs return string references of either constant form or newly-constructed or concatenated strings. The string type is a reference type in the language that is special-cased by the runtime.
Tip: Like all reference types, the string type is pushed to the stack and it is only four or eight bytes on computers.
The string data is never copied when you assign a variable to the result of the method that returns a string. But if you are using unmanaged code or marshalling data, string data may be copied.
Return ArrayReturn BoolReturn Multiple Values
Ret. We examine the ret instruction in the intermediate language that is encountered by the execution engine in the metadata. Ret can be executed when there is one or zero items in the evaluation stack.
Tip: The evaluation stack contains varying types of elements, unlike the parameter slots and local variable slots.
If the method returns one value, the evaluation stack should have one value of that type when the ret instruction is executed. If the method returns void, the evaluation stack can be empty.
Note: The best description of these concepts in the .NET Framework is found in Serge Lidin's book Expert .NET 2.0 IL Assembler.
Summary. It is common to return values in methods in the C# language. We discussed the concepts of reachability and end points, and how the execution engine processes ret instructions in general. Returns are used in every C# program.