Home > C# > C# Functions

A C# function is a set or block of code that collectively performs a task. It is same as functions in C++. Just like C++, every C# program has at least 1 method in it which is the main. It is the point from where every C# program starts its working or execution. In some cases, we may have some programs written in C# that don’t have the main function in them.
Like C++ we don’t need to declare functions in C#. We just define a c# function and then use it. So to use a method, we just define & call it.

Defining a C# Function

Functions in c# are defined using the following syntax.

access-specifier return-type method-name (argument(s))
{
    //method body.
}

Access-specifier We use it to specify the visibility of a function.
Return-type it specifies the type of value a function returns. It should be same as the value i.e. written with return statement inside the method. If a method is returning no value, we should specify it as void.
Method-name it is the name for the method it follows same rules as in C++.
Argument(s) arguments or parameters are values that function uses to perform some task.
Method body then comes method body. Where a set of statements collectively performs some task.

Example Program

using System;

namespace method_example
{
    class Program
    {
        public void myFunction()
        {
            Console.WriteLine("This is our first function.");
        }
        static void Main(string[] args)
        {
            
        }
    }
}

In the above example, we wrote a method named myFunction () with the public as access specifier. i.e. it is available to other classes. Its return type is void as it is not returning any value. And this method is not taking any arguments.

Calling C# Function

To use a function, we call it. But It is necessary to call it using an object of the class.
Example Program
Below example will clarify how to call a method
Example program

using System;

namespace method_example
{
    class Program
    {
        public void myFunction()
        {
            Console.WriteLine("Function Example in C#.");
        }
        static void Main(string[] args)
        {
            Program p = new Program();
            p.myFunction();
        }
    }
}

Output
Functions in C#

Functions in C# with Arguments

Parameters are values or variables which we use to send and receive data from a method. We can pass parameters to functions in c# in the following ways:

  1. Passed By value
  2. By reference
  3. By Output

Passing Arguments By value

In this way of passing parameters, Compiler creates a new memory location for each argument. The values are passed to the method and those values are then saved to respective memory locations. In this way, the actual value of parameters has no effect of changes made in the method body. Below example shows passing parameters by value:

Example program

using System;

namespace method_example
{
    class Program
    {
        public void swap_Numbers(int a, int b)
        {
            Console.WriteLine("\n***Swap Function Start***");
            int temp;
            temp = a;
            a = b;
            b = temp;
            Console.WriteLine("Value of a inside function call = " + a);
            Console.WriteLine("Value of b inside function call = " + b);
            Console.WriteLine("***Swap Function End***\n");
        }
        static void Main(string[] args)
        {
            Program p = new Program();
            int a = 15;
            int b = 25;
            Console.WriteLine("Value of a before function call = " + a);
            Console.WriteLine("Value of b before function call = " + b);
            p.swap_Numbers(a, b);
            Console.WriteLine("Value of a after function call = " + a);
            Console.WriteLine("Value of b after function call = " + b);
        }
    }
}

Output
C# function argument passed by value

From the above example, We can see that when we pass arguments by value, changes in the values of arguments inside the function body have no effect on the parameters outside its body.

Arguments Passing By reference

Passing arguments by reference will not create a new memory location for arguments. Instead of value when we pass a parameter by reference it refers to the same memory location where the value of a parameter is saved. Thus effecting the actual value of parameters when we the function makes some changes to the values inside its body. We use ref keyword with parameters in both definition and call to point to the memory location. Below example elaborates passing arguments by reference.
Example program

using System;

namespace method_example
{
    class Program
    {
        public void swap_Numbers(ref int a, ref int b)
        {
            Console.WriteLine("\n***Swap Function Start***");
            int temp;
            temp = a;
            a = b;
            b = temp;
            Console.WriteLine("Value of a inside function call = " + a);
            Console.WriteLine("Value of b inside function call = " + b);
            Console.WriteLine("***Swap Function End***\n");
        }
        static void Main(string[] args)
        {
            Program p = new Program();
            int a = 15;
            int b = 25;
            Console.WriteLine("Value of a before function call = " + a);
            Console.WriteLine("Value of b before function call = " + b);
            p.swap_Numbers(ref a,ref  b);
            Console.WriteLine("Value of a after function call = " + a);
            Console.WriteLine("Value of b after function call = " + b);
        }
    }
}

Output

C# function argument passed by reference

Here we can see that when we pass arguments by reference, changes in the values of arguments inside the body have a direct effect on the parameters outside the function body.

By Output

As we already know that return statement is used for returning a single value from a method. With output parameters, we can return more than one value from a method. Output parameters are mostly like reference parameters with a difference that output parameters transfer data out of method instead of into method as in reference parameters. out a reserved word is used with parameters when passes by output.
Below example will clarify this concept.
Example program

using System;

namespace method_example
{
    class Program
    {
        public void swap_Numbers(out int a, out int b)
        {
            Console.WriteLine("\n***Swap Function Start***");
            a = 100;
            b = 200;
            Console.WriteLine("Value of a inside function call = " + a);
            Console.WriteLine("Value of b inside function call = " + b);
            Console.WriteLine("***Swap Function End***\n");
        }
        static void Main(string[] args)
        {
            Program p = new Program();
            int a = 15;
            int b = 25;
            Console.WriteLine("Value of a before function call = " + a);
            Console.WriteLine("Value of b before function call = " + b);
            p.swap_Numbers(out a,out  b);
            Console.WriteLine("Value of a after function call = " + a);
            Console.WriteLine("Value of b after function call = " + b);
        }
    }
}

Output
C# function - argument passed by output

As it can be seen that parameters are passed by output and change in values of parameters inside function body effected their values inside the main function from where they were passed.
As we assigned new values to parameters and these values are changed in the main function as well.

Leave a Reply

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.