C# Keywords Tutorial Part 71: return

C# Keywords Tutorial Part 71: return

C# is a powerful programming language that provides developers with a wide range of tools to create robust and efficient applications. One of the fundamental constructs in C# is the “return” keyword. In this blog post, we will discuss the various uses of the “return” keyword and provide code examples to illustrate each scenario.

The “return” Keyword in C#

In C#, the “return” keyword is used to exit a function and return a value to the calling code. The syntax for the “return” statement is as follows:

return [expression];

The “expression” represents the value to be returned to the calling code. The data type of the expression must match the return type of the function.

Using “return” to Exit a Function

The primary use of the “return” keyword is to exit a function and return a value to the calling code. In the following example, we have a function that calculates the sum of two numbers and returns the result to the calling code:

public static int Add(int a, int b)
{
    int result = a + b;
    return result;
}

static void Main(string[] args)
{
    int sum = Add(2, 3);
    Console.WriteLine("The sum is " + sum);
}

In this example, the “Add” function takes two integer parameters and returns the sum of the two numbers to the calling code. The “return” statement exits the function and returns the result to the calling code. The “Main” function calls the “Add” function and displays the result to the console.

Using “return” to Exit a Void Function

In C#, a void function is a function that does not return a value. However, you can still use the “return” keyword to exit a void function. In this case, you can use the “return” statement without an expression.

public static void PrintMessage()
{
    Console.WriteLine("Hello, World!");
    return;
}

static void Main(string[] args)
{
    PrintMessage();
}

In this example, the “PrintMessage” function prints a message to the console and exits. The “return” statement is used to exit the function, even though it does not return a value.

Using “return” in a Conditional Statement

The “return” keyword can also be used in a conditional statement to exit a function and return a value based on a condition. In the following example, we have a function that checks whether a number is even or odd and returns a string indicating the result:

public static string CheckNumber(int number)
{
    if (number % 2 == 0)
    {
        return "The number is even";
    }
    else
    {
        return "The number is odd";
    }
}

static void Main(string[] args)
{
    string result = CheckNumber(3);
    Console.WriteLine(result);
}

In this example, the “CheckNumber” function takes an integer parameter and checks whether the number is even or odd. If the number is even, the function returns a string indicating that the number is even. If the number is odd, the function returns a string indicating that the number is odd. The “Main” function calls the “CheckNumber” function and displays the result to the console.

Share this post

Leave a Reply

Your email address will not be published. Required fields are marked *