What are different ways to pass parameters to functions in C++?

There are two methods by which you can pass parameters to functions in C++ :

  1. Call by Value

Value being passed to the function is locally stored by the function parameter in stack memory location. If you change the value of function parameter, it is changed for the current function only. Original value is not changed.

CODE:

void change(int data);  
int main()  
{  
int data = 3;  
change(data);  
cout << "Value of the data is: " << data<< endl;  
return 0;  
}  
void change(int data)  
{  
data = 5;  
}  
//this will print 3
  1. Call by Reference

Original value is changed because we pass address as function parameter. Here, address of the value is passed in the function, so actual and formal arguments share the same address space.

void swap(int *x, int *y)  
{  
 int swap;  
 swap=*x;  
 *x=*y;  
 *y=swap;  
}  
int main()   
{    
 int x=500, y=100;    
 swap(&x, &y);  
 cout<<"Value of x is: "<<x<<endl;  
 cout<<"Value of y is: "<<y<<endl;  
 return 0;  
}    

//x will print 100, y will print 500