C Sharp Function

Education is not limited to just classrooms. It can be gained anytime, anywhere... - Ravi Ranjan (M.Tech-NIT)

C# Function

Function is a block of code that has a signature. Function is used to execute statements specified in the code block. A function consists of the following components:

Function name: It is a unique name that is used to make Function call.

Return type: It is used to specify the data type of function return value.

Body: It is a block that contains executable statements.

Access specifier: It is used to specify function accessibility in the application.

Parameters: It is a list of arguments that we can pass to the function during call.

C# Function Syntax

  1. <access-specifier><return-type>FunctionName(<parameters>)  
  2. {  
  3. // function body  
  4. // return statement  
  5. }  

Access-specifier, parameters and return statement are optional.

Let's see an example in which we have created a function that returns a string value and takes a string parameter.

C# Function: using no parameter and return type

A function that does not return any value specifies void type as a return type. In the following example, a function is created without return type.

  1. using System;  
  2. namespace FunctionExample  
  3. {  
  4.     class Program  
  5.     {  
  6.         // User defined function without return type  
  7.         public void Show() // No Parameter  
  8.         {  
  9.             Console.WriteLine("This is non parameterized function");  
  10.             // No return statement  
  11.         }  
  12.         // Main function, execution entry point of the program  
  13.         static void Main(string[] args)  
  14.         {  
  15.             Program program = new Program(); // Creating Object  
  16.             program.Show(); // Calling Function             
  17.         }  
  18.     }  
  19. }  

 

Output:

This is non parameterized function

C# Function: using parameter but no return type

  1. using System;  
  2. namespace FunctionExample  
  3. {  
  4.     class Program  
  5.     {  
  6.         // User defined function without return type  
  7.         public void Show(string message)  
  8.         {  
  9.             Console.WriteLine("Hello " + message);  
  10.             // No return statement  
  11.         }  
  12.        // Main function, execution entry point of the program  
  13.         static void Main(string[] args)  
  14.         {  
  15.             Program program = new Program(); // Creating Object  
  16.             program.Show("Rahul Kumar"); // Calling Function             
  17.         }  
  18.     }  
  19. }  

Output:

Hello Rahul Kumar

A function can have zero or any number of parameters to get data. In the following example, a function is created without parameters. A function without parameter is also known as non-parameterized function.

C# Function: using parameter and return type

  1. sing System;  
  2. namespace FunctionExample  
  3. {  
  4.     class Program  
  5.     {  
  6.         // User defined function  
  7.         public string Show(string message)  
  8.         {  
  9.          Console.WriteLine("Inside Show Function");  
  10.          return message;  
  11.         }  
  12.         // Main function, execution entry point of the program  
  13.         static void Main(string[] args)  
  14.         {  
  15.             Program program = new Program();  
  16.             string message = program.Show("Rahul Kumar");  
  17.             Console.WriteLine("Hello "+message);  
  18.         }  
  19.     }  
  20. }  

Output:

Inside Show Function
Hello Rahul Kumar