***Welcome to ashrafedu.blogspot.com * * * This website is maintained by ASHRAF***

Monday, April 26, 2021

Parameter passing techniques in C++

In programming, argument refers to the variable passed to the function. There are three methods to pass the arguments into the function in C++ language,

                                    call by value

                                   call by pointer and

                                   call by reference.

 

1. call by value 

The call by value method of passing arguments to a function copies the actual value of an argument into the formal parameter of the function. 

In call by value method, the value of the actual parameter cannot be modified by the formal parameter.

In call by value, different memory is allocated for actual and formal parameters since the value of the actual parameter is copied into the formal parameter.

The actual parameter is the argument which is used in the function call whereas formal parameter is the argument which is used in the function definition.

Example:

#include <iostream.h>

 // function declaration

void swap(int x, int y);

int main () {

   int a = 100;

   int b = 200;

    cout << "Before swap, value of a :" << a << endl;

   cout << "Before swap, value of b :" << b << endl;

    // calling a function to swap the values.

   swap(a, b);

    cout << "After swap, value of a :" << a << endl;

   cout << "After swap, value of b :" << b << endl;

    return 0;

}

// function definition to swap the values.

void swap(int x, int y) {

   int temp;

   temp = x; /* save the value of x */

   x = y;    /* put y into x */

   y = temp; /* put x into y */

    }

output:

Before swap, value of a :100

Before swap, value of b :200

After swap, value of a :100

After swap, value of b :200

2. call by pointer

The call by pointer method of passing arguments to a function copies the address of an argument into the formal parameter. Inside the function, the address is used to access the actual argument used in the call. This means that changes made to the parameter affect the passed argument.

Example:

#include <iostream.h>

// function declaration

void swap(int *x, int *y);

int main () {

   int a = 100;

   int b = 200;

    cout << "Before swap, value of a :" << a << endl;

   cout << "Before swap, value of b :" << b << endl;

   swap(&a, &b);

   cout << "After swap, value of a :" << a << endl;

   cout << "After swap, value of b :" << b << endl;

    return 0;

}

// function definition to swap the values.

void swap(int *x, int *y) {

   int temp;

   temp = *x; /* save the value at address x */

   *x = *y; /* put y into x */

   *y = temp; /* put x into y */

 }

output:

Before swap, value of a :100

Before swap, value of b :200

After swap, value of a :200

After swap, value of b :100

3. call by reference

The call by reference method of passing arguments to a function copies the reference of an argument into the formal parameter. Inside the function, the reference is used to access the actual argument used in the call. This means that changes made to the parameter affect the passed argument.

To pass the value by reference, argument reference is passed to the functions just like any other value.

Example:

#include <iostream.h>

// function declaration

void swap(int &x, int &y);

int main () {

   int a = 100;

   int b = 200;

    cout << "Before swap, value of a :" << a << endl;

   cout << "Before swap, value of b :" << b << endl;

   /* calling a function to swap the values using variable reference.*/

   swap(a, b);

   cout << "After swap, value of a :" << a << endl;

   cout << "After swap, value of b :" << b << endl;

    return 0;

}

// function definition to swap the values.

void swap(int &x, int &y) {

   int temp;

   temp = x; /* save the value at address x */

   x = y;    /* put y into x */

   y = temp; /* put x into y */

  }

Output:

Before swap, value of a :100

Before swap, value of b :200

After swap, value of a :200

After swap, value of b :100

Difference between call by value and call by reference



No comments:

Post a Comment