parameter passing method

The actual parameter in C++ is passed in function-definition in three ways. which is like this.

 


call by value

In this, the values of the parameters of the calling function are copied into the variables declared in the parameters of the function definition i.e. the value of the actual parameter is copied to the formal parameter.

call-by-value

The function definition uses only the values of the actual parameters (original value) copied by the formal parameters.

In a way, the function definition has duplicate values. Therefore, even if the names of both the parameters are same, the values of the actual parameter do not change even if the values of the formal parameter change.

 void value(int,int); // function declaration
    .................
    .................

 value(x,y); // call by reference

 void value (int x, int y) // function definition 
   { 
      ............ 
      ............
   }

Here is the program,

#include<iostream.h>
#include<conio.h>

void value(int,int);  // function declaration

void main()
 {
   clrscr();
   int x,y;                   // variable declaration

   cout<<"Enter two number: ";
   cin>>x>>y;
  
    cout<<"\nFirst number : "<<x;
   cout<<"\nSecond number: "<<y;

   value(x,y);                 // call by value

   cout<<"\nafter call by value";
   cout<<"\nFirst number : "<<x;
   cout<<"\nSecond number: "<<y;

  getch();
}

  void value (int x, int y)         // function definition
   {
      int z;
        z = x;
        x = y;
        y = z;
   }
OUTPUT:-
Enter two number: 2 3 

First number : 2
Second number: 3

after call by value
First number : 2
Second number: 3

 


call by reference

In it, there are reference variables in the formal parameters that mean different variables name. Like the call by value, the values of actual parameters are copied into formal parameters.

call-by-reference

When there are changes in the values of the actual parameters, then the values of the actual parameters also change.

 void value(int&,int&); // function declaration 
    .................
    .................

 value(x,y); // call by reference 

 void value (int &a, int &b) // function definition
   { 
      ............
      ............
   }

Here is the program

#include<iostream.h>
#include<conio.h>

void main()
 {
   clrscr();
   int x,y;                 // variable declaration

   void value(int&,int&);           // function declaration

   cout<<"Enter two number: ";
   cin>>x>>y;

   cout<<"\nFirst number : "<<x;
   cout<<"\nSecond number: "<<y;

   value(x,y);                   // call by reference

   cout<<"\nafter call by reference";
   cout<<"\nFirst number : "<<x;
   cout<<"\nSecond number: "<<y;
  
  getch(); 
 }

 void value (int &a, int &b)   // function definition
  {
      int c;
       c = a;
       a = b;
       b = c;
  }
OUTPUT:- 
Enter two number: 2 3 
First number : 2
Second number: 3

after call by reference
First number : 3
Second number: 2

 


call by pointer

This method of parameter passing is different from the other two. To understand this, it is necessary to understand pointer. In this method, the memory address of values in the function definition is pass,

call-by-pointer

As we know that we use pointer variable to access the value from address, hence in function definition the pointer variable is declared as formal parameters.

In this also, when there are changes in values of formal parameters as well as call by reference, values of actual parameters are also changed.

 void value(int*,int*); // function declaration 
    ................. 
    ................. 

 value(&x,&y); // call by reference 

 void value (int *a, int *b) // function definition 
  { 
     ............ 
     ............ 
  }

Here is the program,

#include<iostream.h>
#include<conio.h>

 void main()
  {
     clrscr();
     int x,y;                  // variable declaration

     void value(int*,int*);           // function declaration

     cout<<"Enter two number: ";
     cin>>x>>y;

     cout<<"\nFirst number : "<<x;
     cout<<"\nSecond number: "<<y;

     value(&x,&y);                  // call by pointer

     cout<<"\nafter call by pointer";
     cout<<"\nFirst number : "<<x;
     cout<<"\nSecond number: "<<y;
   
    getch();
  }

  void value (int *a, int *b)        // function definition   
   {
      int c;
        c  = *a;
        *a = *b;
        *b = c;
   }
OUTPUT:-
Enter two number: 2 3
 First number : 2
Second number: 3 

after call by pointer
First number : 3
 Second number: 2


things to know

The only difference between call by value and call by reference is that where the parameter name (actual, formal) in the call by value is the same, the parameter name in the call by reference is different.


previous- Function and their type | Parameter type

next-String library function and theirs examples