Please note, this is a STATIC archive of website technosmarter.com from 20 Jul 2022, cach3.com does not collect or store any user information, there is no "phishing" involved.
 

C# function with examples


A function is a block of code that contains statements to be executed when needed. In other words, functions are known as methods. In C#, the method is a collection of properties to create functionality.

A programmer makes code to execute the application. Programmer can divides codes into functions to make code easier. Every function performs a different task.

 

How to create a function in C#?


In C# programming, we create a function using the function name.

 

function_name - It is the name of the function. A programmer can create a function with any unique name. The function name is used to call function. Programmer can create a functional name according to function behavior. In C# or another language, behavior denotes the functionality of function.

 


Return Type - A function can return a value. The return type indicates the data type of function return.


Block - Function functionalities are created inside the block. The block start and end with curly braces. Inside the block, we define the behavior of that function. A block is known as the body of function that contains statements to be executed.


Parameters - A parameter is like a placeholder. Parameter is a list of argument(S). It can be passed in a function during call time. When a function invoked, you pass a value to the parameter. We can create function with argument(S) or without arguments.


Access Specifier - Access specific set the accessibility of function functionality outside or inside. There is three access specifier in C# programming.
1. Public
2. Private
3. Protected.


The biggest thing is to know how to declare the function in C# programming.
Let's understand with a syntax.

C# function syntax.


Function_Name()  
{  
//body , block of code 
//return statement 
} 

In the above syntax, the access specifier, parameter, and return are not mandatory. These are optional in C# function.
In C#, a function is called by the function name.

Let's learn function calling in the C# program.


C# function call |function with parameter and return type-

We call a function using function(method) name. In the example, we will create a function with parameters and also return type statement. This type of function is known as a parametrized function in C# programming.

 


using System;

namespace CircleApplication {

   class CirclePerimeter{
   
      public double GetParimeter(double pai, double r) {
         /* local variable declaration */
         
        
         double a;  
         
         a=pai*r*r; //Find Area of circle using function 
         return a;
         
      }
      
      static void Main(string[] args) {
         /* local variable definition */
         double pai =3.14;
         double r= 2.5;
         double cir;
         CirclePerimeter obj= new CirclePerimeter();

         //calling the GetParimeter function
         cir = obj.GetParimeter(pai, r);
         Console.WriteLine("Area of Circle : {0}", cir);
         Console.ReadLine();
      }
   }
}

Output -

 Area of Circle : 19.625 


In the above C# example, we found area of the circle using the function call.

C# Non parameterized function -

In the previous C# constructor tutorial , we discussed defaults and parameterized constructor. In this tutorial, we are creating a function without a list of arguments and return type. In this C# example, we will use void as return type.

If a function does not contain any list of argument(s) which is known as Non parameterized function


C# function without parameter and return type -


using System;  
namespace TechnoSmarter 
{  
    class Techno 
    {  
        double a; 
        double b; 
        double h; 
        
        // User defined function without return type  
        public void GetArea() // No Parameter  
        {  
            h =3.0; 
            b=6.0; 
            a=b*h/2; //Area of triangle 
            
            Console.WriteLine("Area of triangle: "+a);  
            // No return statement  
        }  
        // Main function, entry point 
        static void Main(string[] args)  
        {  
            Techno triangle = new Techno(); // Creating Object  
            triangle.GetArea(); // Calling Function GetArea()             
        }  
    }  
}  

Compiler and execute the above C# program. It gives the result as output.

Output

 Area of triangle: 9


In the above example, we create a function without any parameter and no return type in the C# program.


Please Share

Recommended Posts:-