Author : Rahul
Last Modified : 03-Dec-2021
Complexity : Beginner

Function Parameters in C#


Introduction

A function is a block of statement which perform a particular task. A function can or can not return the result to the caller. The main benefit of function is to remove the duplicacy of code and it increase the code readiability.

Sometime a function require some input data to complete its task. This input data is called parameter. There are different type of parameters which can be used to pass input data to function. These parameters are

  1. Value Type
  2. Ref Type
  3. Out Type
  4. Named Type
  5. Optional Type
  6. Params Type
  7. Dynamic Type
     

Value Type Parameter

It is the normal value parameter which is used to passed the value by value. When variable is passed as value type, data is passed not the reference. If we make any change into this variable within called function and when control is move from called function to calling function, the original variable value is not updated.

Program

using System;

namespace FunctionParameter
{
   class Program
   {
       static void Main(string[] args)
       {
           int firstNum = 10;
           int secondNum = 20;
           
           Console.WriteLine("Variable values before function call:");
           Console.WriteLine($"First Num:{firstNum}, Second Num:{secondNum}");
           
           int result = AddUpdateNumber(firstNum,secondNum);
           
           Console.WriteLine("Variable values after function call:");
           Console.WriteLine($"First Num:{firstNum}, Second Num:{secondNum}");
           
           Console.WriteLine($"Sum of two number:{result}");
           
           Console.Read();
       }
       
       /// <summary>
       /// Method is used to add and update two number
       /// </summary>
       /// <param name="firstNum">firstNum</param>
       /// <param name="secondNum">secondNum</param>
       /// <returns>Add and update two number</returns>
       public static int AddUpdateNumber(int firstNum, int secondNum)
       {
           int result = firstNum + secondNum;
           firstNum = 20;
           secondNum = 30;
           return result;
       }
   }
}

Output

Variable values before function call:
First Num:10, Second Num:20
Variable values after function call:
First Num:10, Second Num:20
Sum of two number:30

 

Ref Type Parameter

The ref keyword is used to pass the value by reference. The ref type parameter are bi directional meaning that the value of ref type parameter is passed from calling function to called function and vice versa. In ref type parameters, it is required that the variable should be initialize in calling function before it passed to called function. Suppose we made any changes in this parameter within called function, so when control come back from called function to calling function, this variable value also updated in calling function. This parameter is mainly used when we want to return multiple values from the function.

Program

using System;

namespace FunctionParameter
{
   class Program
   {
       static void Main(string[] args)
       {
           int firstNum = 10;
           int secondNum = 20;
           
           Console.WriteLine("Variable values before function call:");
           Console.WriteLine($"First Num:{firstNum}, Second Num:{secondNum}");
           
           int result = AddUpdateNumber(ref firstNum,ref secondNum);
           
           Console.WriteLine("Variable values after function call:");
           Console.WriteLine($"First Num:{firstNum}, Second Num:{secondNum}");
           
           Console.WriteLine($"Sum of two number:{result}");
           
           Console.Read();
       }
       
       /// <summary>
       /// Method is used to add and update two number
       /// </summary>
       /// <param name="firstNum">firstNum</param>
       /// <param name="secondNum">secondNum</param>
       /// <returns>Add and update two number</returns>
       public static int AddUpdateNumber(ref int firstNum, ref int secondNum)
       {
           int result = firstNum + secondNum;
           firstNum = 20;
           secondNum = 30;
           return result;
       }
   }
}

Output

Variable values before function call:
First Num:10, Second Num:20
Variable values after function call:
First Num:20, Second Num:30
Sum of two number:30

 

Out Type Parameter

The out keyword is also used to pass the value by reference. The out type parameter are one directional meaning that the value of out type parameter is passed from called function to calling function only. In out type parameters, it is required that the parameter should be initialize in called function before it passed to calling function. suppose we made any changes in this parameter within called function, so when control come back from called function to calling function, this variable value also updated in calling function. This parameter is mainly used when we want to return multiple values from the function.

Program

using System;

namespace FunctionParameter
{
   class Program
   {
       static void Main(string[] args)
       {
           int firstNum = 10;
           int secondNum = 20;
           
           Console.WriteLine("Variable values before function call:");
           Console.WriteLine($"First Num:{firstNum}, Second Num:{secondNum}");
           
           int result = AddUpdateNumber(out firstNum,out secondNum);
           
           Console.WriteLine("Variable values after function call:");
           Console.WriteLine($"First Num:{firstNum}, Second Num:{secondNum}");
           
           Console.WriteLine($"Sum of two number:{result}");
           
           Console.Read();
       }
       
       /// <summary>
       /// Method is used to add and update two number
       /// </summary>
       /// <param name="firstNum">firstNum</param>
       /// <param name="secondNum">secondNum</param>
       /// <returns>Add and update two number</returns>
       public static int AddUpdateNumber(out int firstNum, out int secondNum)
       {
           firstNum = 20;
           secondNum = 30;
           int result = firstNum + secondNum;          
           return result;
       }
   }
}

Output

Variable values before function call:
First Num:10, Second Num:20
Variable values after function call:
First Num:20, Second Num:30
Sum of two number:50

 

Named Type Parameter

In named type parameter, we can define the value of the parameter based on parameter name instead of parameter order in the function. Meaning that we don't need to learn the order of the parameter just pass the value with parameter name. The main benifit of this type of parameters is that, it makes the program more readable if we work on large number of parameters in the function. The main rectriction with these type of parameters is that they should be the last parameters in the parameter list of the function definition.

Program

using System;

namespace FunctionParameter
{
   class Program
   {
       static void Main(string[] args)
       {
           int firstNum = 10;
           int secondNum = 20;
           
           Console.WriteLine("Variable values before function call:");
           Console.WriteLine($"First Num:{firstNum}, Second Num:{secondNum}");
           
           int result = AddUpdateNumber(secondNum:secondNum, firstNum:firstNum);
           
           Console.WriteLine("Variable values after function call:");
           Console.WriteLine($"First Num:{firstNum}, Second Num:{secondNum}");
           
           Console.WriteLine($"Sum of two number:{result}");
           
           Console.Read();
       }
       
       /// <summary>
       /// Method is used to add and update two number
       /// </summary>
       /// <param name="firstNum">firstNum</param>
       /// <param name="secondNum">secondNum</param>
       /// <returns>Add and update two number</returns>
       public static int AddUpdateNumber(int firstNum, int secondNum)
       {
           int result = firstNum + secondNum;
           firstNum = 20;
           secondNum = 30;                      
           return result;
       }
   }
}

Output

Variable values before function call:
First Num:10, Second Num:20
Variable values after function call:
First Num:10, Second Num:20
Sum of two number:30

 

Optional Type Parameter

These parameters are the optional parameters not the required parameters. We can give some default value to these parameters in the function definition. If we pass some value for these type of parameters from calling function, these parameters receive update values and if don't pass the value for these type of parameters from calling function, these parameters receive default values. The main benifit of these type of parameters is that, if we want to increase the parameter list in the method, we dont want to update all function call for this method. The main rectriction with these type of parameters is that they should be the last parameters in the parameter list of the function definition.

Program

using System;

namespace FunctionParameter
{
   class Program
   {
       static void Main(string[] args)
       {
           int firstNum = 10;
           
           Console.WriteLine("Variable values before function call:");
           Console.WriteLine($"First Num:{firstNum}");
           
           int result = AddUpdateNumber(firstNum);
           
           Console.WriteLine("Variable values after function call:");
           Console.WriteLine($"First Num:{firstNum}");
           
           Console.WriteLine($"Sum of two number:{result}");
           
           Console.Read();
       }
       
       /// <summary>
       /// Method is used to add and update two number
       /// </summary>
       /// <param name="firstNum">firstNum</param>
       /// <param name="secondNum">secondNum</param>
       /// <returns>Add and update two number</returns>
       public static int AddUpdateNumber(int firstNum, int secondNum=5)
       {
           int result = firstNum + secondNum;
           firstNum = 20;
           secondNum = 30;                      
           return result;
       }
   }
}

Output

Variable values before function call:
First Num:10
Variable values after function call:
First Num:10
Sum of two number:15

 

Params Type Parameter

The params keyword is used to pass the dynamic number of parameters. The length of param type parameters can be zero or more. These type of parameters are very usefull if the programmers does not have knowledge about the number of parameters. The main rectrction with these type of parameters is that they should be last parameters in the parameter list and contains only 1 params parameter in the function definition.

Program

using System;

namespace FunctionParameter
{
   class Program
   {
       static void Main(string[] args)
       {
           int firstNum = 10;
           int secondNum = 20;
           
           Console.WriteLine("Variable values before function call:");
           Console.WriteLine($"First Num:{firstNum}, Second Num:{secondNum}");
           
           int result = AddUpdateNumber(firstNum, secondNum);
           
           Console.WriteLine("Variable values after function call:");
           Console.WriteLine($"First Num:{firstNum}, Second Num:{secondNum}");
           
           Console.WriteLine($"Sum of two number:{result}");
           
           Console.Read();
       }
       
       /// <summary>
       /// Method is used to add and update two number
       /// </summary>
       /// <param name="num">num</param>
       /// <returns>Add and update two number</returns>
       public static int AddUpdateNumber(params int[] num)
       {
           int result = 0;
           for(int i=0;i<num.Length;i++)
           {
               result = result + num[i];
               num[i] = num[i] + 1;
           }
           return result;
       }
   }
}

Output

Variable values before function call:
First Num:10, Second Num:20
Variable values after function call:
First Num:10, Second Num:20
Sum of two number:30

 

Dynamic Type Parameter

The dynamic keyword is used to pass the dynamic type variable. In these type of parameters, compiler can not check the type of the dynamic type variable at compile-time, its type will be check at the run time.

Program

using System;

namespace FunctionParameter
{
   class Program
   {
       static void Main(string[] args)
       {
           int result = AddNumber(10, 20);
           
           Console.WriteLine($"Sum of two number:{result}");
           
           Console.Read();
       }
       
       /// <summary>
       /// Method is used to add two number
       /// </summary>
       /// <param name="firstNum">firstNum</param>
       /// <param name="secondNum">secondNum</param>
       /// <returns>Add two number</returns>
       public static int AddNumber(dynamic firstNum, dynamic secondNum)
       {
           int result = 0;
           result = firstNum + secondNum;
           return result;
       }
   }
}

Output

Sum of two number:30