In this article I am going to discuss about C# Functions with example but before that if you had not read my previous article of C# goto statement then must read it for more clear understanding. In any programming language functions play crucial role to improve the code readablity and reusuability because with functions we can divide our code into small parts so anyone can understand the code and easy to find the error.

Introduction to C# Functions

In C#, Functions are group of instructions or group of any code statement which is used to perform a specific task. The functions can be large or small it totally depends on us but it will perform the task completely. Functions will give the result with the return statement. When we create a function then we also define the return type of functions that means the function’s result will be returned in form of that return type. At this type, you might be confused but don’t worry this will clear to you very shortly.

In so many places you must listen the word methods in csharp. If yes then I will clear to you that methods are the functions which is defined inside the classes. In C# sometimes functions and methods are used interchangeably. In my next articles I can use the word methods in place of functions so don’t worry about it. The syntax for creating a function is given below:

public returnType functionName(//Parameters){ //public is access specifier
//block of code
}

In above syntax public is access specifier which is used to define the accessibility of function. For now you just know by defining public that means we can access the function anywhere in the class. In our next articles we will discuss different-2 types of access specifier and their usage.

What is need of Functions in C#?

If you are familiar with Monolithic Programming then you must know the need of functions but if you don’t know about monolithic programming then don’t worry. In every C# program there is one main function and if we write our whole code inside that main function then it is called monolithic programming. So the need of functions arise due to maintaining code readability and for code reusability. For example if we will write one function and we have to use that function in so many places then we simply have to call only that function. But if we not make function then we have to write whole code again and again that makes our code lengthy and not readable. So with functions we can use the code reusability.

So if we write our code inside the functions then it becomes Functional or Procedural Programming and today every programs are written either with procedural programming or object oriented programming. C# is an object oriented programming language and we must have to follow object oriented programming and we will definitely discuss object oriented programming in next articles. But today we will covering about functions and its first type which is user-defined functions.

Types of C# Functions

  • User-defined Functions
  • Built-In Functions

User-defined Functions in C#

User-defined Functions are the functions that is created by the programmer or user who writes the program. In csharp programming language we can create the function with two ways and the way for calling these functions are also different. We can create the function with static keyword or without static keyword. If we are simply creating one function inside the class then we must have to create the instance(object) of class to call the function. But if we are creating a static function then we can call this function with using class name only. Let see how to create an function.

C# Function without Static keyword

using System;
public class Functions
{
    public static void Main(string[] args)
    {
        Functions obj= new Functions(); //creating an instance of Function
        int result=obj.AddTwoNumber(10,20);  // calling the function with instance
        Console.WriteLine(result);
    }
    
    public int AddTwoNumber(int firstNumber, int secondNumber){ 
    // Function without static keyword
        return firstNumber+secondNumber;
    }
}
Output:
The result of adding two number is :30

In above program we have created a C# function inside the Functions class which takes two parameters named firstNumber and secondNumber of type integer and return type of integer. It means the result of function will be integer and if we trying to return anything else rather then integer then we will get compilation error.

C# Function with static keyword

using System;
public class Functions
{
    public static void Main(string[] args)
    {
        int result=Functions.AddTwoNumber(10,20); //calling function with class name
        Console.WriteLine("The result of adding two number is :" +result);
    }
    
    public static int AddTwoNumber(int firstNumber, int secondNumber){
        return firstNumber+secondNumber;
    }
}
Output:
The result of adding two number is :30

In above program we have created the same C# function with static keyword and you can see that this type while calling this function we have not created the instance of function and calling directly with name of class. This is the magic of static function we don’t have to create an object of class to call C# functions.

C# Functions without Parameters

We can also create the functions without parameters. This type of function is also called parameterless function or method. Let see an example to see the working of parameterless function in csharp.

using System;
public class ParameterLessFunctions
{
    public static void Main(string[] args)
    {
        int result=Functions.AddTwoNumber();
        Console.WriteLine("The result of adding two number is :" +result);
    }
    
    public static int AddTwoNumber(){
        Console.WriteLine("Enter First Number");
        int firstNumber=Convert.ToInt32(Console.ReadLine());
        Console.WriteLine("Enter Second Number");
        int secondNumber=Convert.ToInt32(Console.ReadLine());

        return firstNumber+secondNumber;
    }
}
Output:
Enter First Number
67
Enter Second Number
32
The result of adding two number is :99

Conclusion

In this C# tutorials article I have discussed C# functions in detail with examples. Instead of this we have also covered how to create static functions and how to call the function in program. In this article we have basically discussed about user-defined functions in detail and in my next article I am going to discuss about various built-in functions in csharp programming language. Hope you like this article and understand user-defined functions.