Lesson 7 – Functions in C#

If you have a large piece of code, that you repeatedly execute, it is not advisable to write that piece of code again and again. Such a piece of code can be wrapped inside a function and then function call can be used to execute that piece of code via a single line of code. In this lesson, you will see functions in action.

Simple Function

A function has two parts: function definition and function call. The function definition is used to specify the piece of code that you want to execute when the function is run and the function call is used to actually execute the function code. Let’s see a very simple example of a function that just prints a welcome message on the screen.

using System;

namespace C_Project
{
    class Program
    {
        static void Main(string[] args)
        {
            ShowMessage(); // call to ShowMessage Function
        }

        public static void ShowMessage()
        {
            Console.WriteLine("Welcom to dronaschool.com");
        }

    }
}

In the script above we define a function “ShowMessage” which simply prints a message on the function. The function is marked as public which means that it can be accessed anywhere. A private function can only be accessed within the class in which it is defined whereas protected function can be accessed within the same namespace and within the child classes. Next, we use the keyword “static” which means that the function can be accessed without the object of the class. The keyword “void” means that the function doesn’t return any value. You can see the opening and closing parenthesis after the function name. These parentheses are used to pass values to the function.

Inside the “Main” method, you can see the call to “ShowMessage” function. To call a function, you simply have to write the method name followed by the opening and closing parenthesis. Inside the parenthesis you specify the values that you pass to the function. Since we are not passing any value to the function. We kept it empty. The output of the above script is as follows:

Welcom to dronaschool.com

Parameterized Functions

Parameterized functions are the functions that accept values from a function call. In the following script, we create a parameterized function “ShowPassedMessage” which accepts one string type parameter. The parameter passed to it is displayed on the console.

using System;

namespace C_Project
{
    class Program
    {
        static void Main(string[] args)
        {
            ShowPassedMessage("This is message is passed from function call."); // call to ShowPassedMessage Function
        }

        public static void ShowPassedMessage(string message)
        {
            Console.WriteLine(message);
        }
    }
}

The output of the above script is as follows:

This is message is passed from function call.

Returning Values from Functions

You can also return a value from a function. The value is returned is using the “return” keyword. In the following script, we define a method “ReturnProduct” which accepts two integer parameters, multiply the variables and then return the result to the calling function.

using System;

namespace C_Project
{
    class Program
    {
        static void Main(string[] args)
        {
            int result = ReturnProduct(5, 10);
            Console.WriteLine(result);
        }

        public static int ReturnProduct(int num1, int num2)
        {
            int result = num1 * num2;
            return result;
        }
    }
}

In the output, you will see 50 which is the product of 5 and 10.

What’s Next?

In this lesson, you studied functions. In the next lesson, we will start our discussion about objects and classes in C#.