Different types of Parameter passing techniques in C++ with Examples

Different types of parameter passing techniques
Parameter Passing techniques

 

 Parameter passing is the technique in which the parameters are passed on from the calling function to the called function.

In this article, we'll be looking at 3 different types of Parameter passing techniques in C++. They are -

  1. Pass by Value
  2. Pass by Address
  3. Pass by Reference


Pass by Value / Call by Value - In pass by value method, a copy of the parameters is passed from the calling function to the called function. Any changes made to the formal parameters are not considered in the actual parameters.

Example -

//Different types of parameter passing in C++ by Vishruth Codes
//Pass by value
#include<iostream>
using namespace std;

//Function to swap two numbers
void swap(int r, int s)
{
    r=r+s;
    s=r-s;
    r=r-s;
    cout<<"\nIn swap function, the value of a & b is: "<<r<<" "<<s;
    cout<<"\nThe address of variables within swap function is: "<<&r<<" "<<&s;
}


int main()
{
    int a,b;
    cout<<"\nEnter the value of a & b: ";
    cin>>a>>b;
    swap(a,b);
    cout<<"\n\nAfter calling swap, the value of a & b in main() is: "<<a<<" "<<b;
    cout<<"\nThe address of variables within main() function is: "<<&a<<" "<<&b;
    return 0;
}

Output - 

Pass by value in c++
Pass by Value


Use - This method is used when we have to return a value from a function. 



Pass by Reference / Call by Reference - In pass by reference method, references of the actual parameters are passed on to the formal parameters. Any changes made to the formal parameters are reflected in the actual parameters.
💡Note: C programming language does not support references.

Example - 
//Different types of parameter passing in C++ by Vishruth Codes
//Pass by reference
#include<iostream>
using namespace std;

//Function to swap two numbers
void swap(int &r, int &s)
{
    r=r+s;
    s=r-s;
    r=r-s;
    cout<<"\nIn swap function, the value of a & b is: "<<r<<" "<<s;
    cout<<"\nThe address of variables within swap function is: "<<&r<<" "<<&s;
}


int main()
{
    int a,b;
    cout<<"\nEnter the value of a & b: ";
    cin>>a>>b;
    swap(a,b);
    cout<<"\n\nAfter calling swap, the value of a & b in main() is: "<<a<<" "<<b;
    cout<<"\nThe address of variables within main() function is: "<<&a<<" "<<&b;
    return 0;
}

 Output - 

Pass by reference in c++
Pass by Reference


Use - This method is used when we want the changes to be reflected in the actual parameters i.e. parameters in the calling function.



💡Note: If the addresses of formal parameters are the same as the addresses of their corresponding actual parameters, then any changes made to the formal parameters will also reflect in the actual parameters. 
For Example, if you compare the output of pass by value with pass by reference, you can observe that the method in which the changes made to the formal parameters aren't reflected in the actual parameters is pass by value, and their parameters' address does not match, whereas the one in which they do, i.e. pass by reference, the changes are also reflected.



Pass by Address / Call by Address - In pass by address method, the formal parameters must be pointers, and addresses of the actual parameters are passed on to the formal parameters. Any changes made to the formal parameters are reflected in the actual parameters.

Example - 

//Different types of parameter passing in C++ by Vishruth Codes
//Pass by address
#include<iostream>
using namespace std;

//Function to swap two numbers
void swap(int *r, int *s)
{
    *r=*r+*s;
    *s=*r-*s;
    *r=*r-*s;
    cout<<"\nIn swap function, the value of a & b is: "<<*r<<" "<<*s;
    cout<<"\nThe address of variables within swap function is: "<<&r<<" "<<&s;
}


int main()
{
    int a,b;
    cout<<"\nEnter the value of a & b: ";
    cin>>a>>b;
    swap(&a,&b);
    cout<<"\n\nAfter calling swap, the value of a & b in main() is: "<<a<<" "<<b;
    cout<<"\nThe address of variables within main() function is: "<<&a<<" "<<&b;
    return 0;
}

Output - 
Pass by address in c++
Pass by Address

Use - This method is used when we want the changes to be reflected in the actual parameters i.e. parameters in the calling function.



Thanks for reading. If you found any errors or have suggestions to improve the article, please mention them in the comments.

Post a Comment

0 Comments