Method parameters in C# – Free C# Tutorials

In the previous blog we saw a general introduction to methods here we will discuss in detail about method parameters in c#.A method invocation creates a copy, specific to that invocation.The actual argument list of the invocation assigns the value to the newly created formal argument list.Within the body of the loop the formal parameters can be used like any other local variables.The invocation not only passes the values to the method but also is involved in returning the results of the operations that was executed in that method.

For managing the process of passing the value and getting back the result.C# employs four kinds of parameters.

  1. Value parameters
  2. Reference parameters
  3. output parameter
  4. parameter array

Value parameter or pass by value in c#

Value parameter are used for passing the parameter as value to the method.By default all method parameters are pass by value.When a method is invoked the value of the actual argument are assigned to the formal arguments.These values of the value parameter can be changed.Any change made in the formal parameter will not be reflected in the actual parameter.This is because in pass by value the value alone is copied in new location in the memory thus the same data is available twice where one is the actual parameter and the other is a formal parameter.Since they are available in separate memory locations any change made in one will not affect the other.

The following program illustrates hoe any change made in the formal parameter will not affect the original value.

code

using System;
class PassByValue
{
   public static void change( int m )
   {
        m = m+10;
   }
   public static void Main()
   {
        int x = 10;
        change(x);  // function call
        Console.WriteLine( "x=" + x  );
   }
}



let us see the execution steps for the above code

  1. compiler begins its execution at the main function.A variable x is declared and assigned a value 10.
  2. Next function call happens.The compiler stops executing the main function (i.e) it pauses the main function and starts executing the change function.In the function call we are passing the value of x to m.So here x is an actual parameter and m is a formal parameter.
  3. Now the change function is being executed here the value if m is altered but that alteration is not passed onto the main function
  4. After the execution of the change function the compiler resumes the main method.

output

x = 10

when the change method is called, the value of x is assigned to m and a new location for m is created in memory.Therefore any change made in m will not affect the value stored in x.


Reference parameter/ pass by reference in c#

pass by value is a default behavior of all the methods in c#.However we can force the value parameter to be passed as reference.To do this we have to use the ref keyword.Example

void Modify( ref int x )

here x is declared as a reference parameter

Unlike the value parameter the reference parameter does not create a new memory location for the formal parameter.Instead it represents the same memory location of the actual parameter thus any change made in the formal parameter will actually affect the actual parameter as well.

Note: when  a formal parameter is declared as ref, the corresponding argument in the method invocation must also be declared as ref.

Example:

void  Change( ref int x )
{
   x = x + 10;  // value of  m will be changed
}
...............
int m = 5;  // m is initialized
Change( ref m );  //pass by reference



When we pass arguments by reference ,the formal arguments in the called method become aliases to the actual arguments in the calling method.This means that the method is  actually working with it’s own arguments.

code- swapping values using ref parameters

using System;
class PassByReference
{
   public static void swap ( ref int m ,ref int n )
   { 
       int temp = x;
       x = y ;
       y = temp;
   }
   public static void Main()
   {
       int  m = 10;
       int n = 20;
       Console.WriteLine( " before swapping ");
       Console.WriteLine( " m = " + m);
       Console.WriteLine( " n = " +n);
       swap ( ref a, ref b )
       Console.WriteLine( " after swapping ");
       Console.WriteLine( " m = " + m);
       Console.WriteLine( " n = " +n);
   }
}

output

before swapping  10 20
after swapping 20 10

When we say pass by reference the address of the value is passed and not the value that’s why we do not have separate memory locations.


Output parameters in c#

The output parameter are used to pass the results back to the calling method.This can be done by declaring the parameters with the  out keyword .This is quite similar to reference parameter it does not create a new memory location.Instead it becomes alias to the parameter in the calling method.When the formal parameter is declared as out,the corresponding actual argument in the calling method must also be declared as  out.

example

void Output ( out int m )
{
   m= 20;
}
...................
int n;  // m is uninitialized
Output ( out m ); // value of m is set

Note that the actual parameter m is not assigned any value before it is passed as output parameter.Since the parameters  x and m refer to the same memory location, m takes the value that is assigned to x. Every formal parameter has to be assigned  a value before the program runs.

code

using System;
class Output
{
   public static void Square(  int x , out int y)
    {
        y = x*x;
    }
   public static void Main()
    {
        int m ; //need not be initialized
        Square ( 10 , out m );
        Console. WriteLine (" m = " +m );
    }
}

output

m =100

parameter array or variable argument lists in c#

c# provides us an advantage by allowing us to declare method with variable number of arguments using what is known as parameter arrays.parameter array are declared using the keyword params 

example

void Function1 ( params int [] x)
{
}



here  x is declared as a parameter array. Parameter array must always be an one-dimensional array.The method Function1 defined above can be invoked in two ways:

  • using int type array as a value parameter.Example:Function(a);  here a is an array of type int
  • using zero or more int type arguments for the parameter array.   Example:Function(10,20)

The second invocation creates an int type array with data   10 and 20 and passes the newly created array as an actual argument to the method.

code

using System;
class params
{
   public static void parray ( params int[] arr )
    {
       Console.WriteLine(" array elements are ");
       foreach ( int i in arr )
           Console.WriteLine  (" " + i);
    }
   public static void Main()
    {
        int [] x = { 11,22,33};
        parray (x);   //call 1
        parray (); call 2
        parray( 100,200);  // call 3
    }
}

output

array elements are : 11 22 33 
array elements are : 
array elements are: 100 200

Note : – A return statement can be used for returning only one value from a function. However, using output parameters, you can return two values from a function.

Leave a Comment

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