Methods can return values of any data type. These values can be used for calculations or obtaining data that the method has calculated.  In a real life scenario, imagine that the method is your employee and you called  him to finish some documents. You are then expecting to receive the finished  documents from him. Those documents are considered the return value of a method.  In fact, when looking at a method, one important thing to know is the value it  will return so you will know how to process the returned values. Returning a value from a method is easy, just follow the syntax  below when creating a method which returns a value.

returnType MethodName()
{
   return value;
}

The returnType here is the data type of the value that the method will return to the caller. Inside a method, we use the return keyword followed by a value or an expression that will result in a value. The value’s  data type to be returned should have a similar data type with the one indicated in the method header. If we are not returning a value, we should use void as the return type. Figure 1 shows an example of a method that will return a value.

using System;

namespace ReturningValueFromMethodDemo
{
    public class Program
    {
        static int CalculateSum()
        {
            int firstNumber = 10;
            int secondNumber = 5;

            int sum = firstNumber + secondNumber;

            return sum;
        }

        public static void Main()
        {
            int result = CalculateSum();

            Console.WriteLine("Sum is {0}.", result);
        }
    }
}

Example 1 – Returning a Value from the Method

Sum is 15.

As you can see in line 7 of Example 1, we used int as the return type in the method header instead of void because we are expecting an int value to be returned. Lines 9-10 declares two variables and initialize them with values. Note that these variables are local variables. It means that it cannot be accessed by other methods such as the Main method. They can only be used by the method who owns them. Line 12 determines the sum of  2 numbers and then assigned to the sum variable.  Line 14 returns the value of sum to the caller using a return statement.

Inside the Main() method, we declared a variable in line 19 and called the CalculateSum() method. The CalculateSum() method returns the value 15 which is the result of the expression inside its body. The value returned is assigned to result  variable. Line 21 prints the result containing the returned  value.

The method in Example 1 is not very useful. We hard coded a value inside a method and it will always return one value which is 15. You can simply assign the value 15 in the result variable. This method will be more useful if we add parameters in a the method. This will be discussed in the next lesson.

When doing decisions inside a method such as using an if or switch statement, all code paths must return a value. This is better demonstrated by the following code example.

using System;

namespace ReturningValueFromMethodDemo2
{
    public class Program
    {
        static int GetNumber()
        {
            int number;

            Console.Write("Enter a number greater than 10: ");
            number = Convert.ToInt32(Console.ReadLine());

            if (number > 10)   
            {                  
                return number; 
            }                  
            else               
            {                  
                return 0;      
            }                  
        }

        public static void Main()
        {
            int result = GetNumber();

            Console.WriteLine("Result = {0}.", result);
        }
    }
}

Example 2 – All Code Paths Must Return a Value

Enter a number greater than 10: 11
Result = 11
Enter a number greater than 10: 9
Result = 0

Lines 7-22 defines a method named GetNumber()  which asks a user of a number which is greater than 10. Failing to give the proper number, the method will return 0. If we are to omit the  else part(lines 18-21) of the if statement or we didn’t supply a returnstatement inside its body, an error will show up when compiling the code. That’s because if the condition of the if statement results to false, the path that the program will follow doesn’t have a return statement which is an error because the method is expected to return a value.

One last thing I want to imply in this lesson. You can exit a method that has no return type. Even if you use the data type void for a method, you can still use the return keyword. Using the return keyword in a method that has no return type exits the method and continue to the next code of the statement that called the method.

using System;

namespace ReturningValueFromMethodDemo3
{
    public class Program
    {
        static void TestReturnExit()
        {
            Console.WriteLine("Line 1 inside the method TestReturnExit()");
            Console.WriteLine("Line 2 inside the method TestReturnExit()");

            return;

            //The following lines will not execute                          
            Console.WriteLine("Line 3 inside the method TestReturnExit()");
            Console.WriteLine("Line 4 inside the method TestReturnExit()");
        }

        public static void Main()
        {
            TestReturnExit();
            Console.WriteLine("Hello World!");
        }
    }
}

Example 3 – Returning from a void type Method

Line 1 inside the method TestReturnExit()
Line 2 inside the method TestReturnExit()
Hello World!

The program demonstrates that using the return keyword(line  12) immediately take you out from the method, ignoring all the codes that follow the return statement. The next line following the method call inside the Main  method is then executed.