C++ Passing Arguments to a Function

A function in C++ may needs data from calling function to perform it's task. It accepts data as function arguments which is passed by calling function. These variables to receive data from calling function are called the formal parameters of the function. A function can be called by passing zero or more parameters as per function declaration.

In C++, parameter(arguments) refers to data which is passed to function while calling function. The formal parameters are similar to local variables inside the function scope and are created when control enters into the function and gets destroyed upon exit.

A Function in C++ can be called in two ways.
  • Call by value
  • Call by reference

First of all we should understand the meaning of actual and formal parameters.

  • Formal parameter : This is the argument which is used inside body of function definition. It's is limited to the scope of function, it is visible to statements in function's body only. It gets created when control enters function and gets destroyed when control exits from function.
  • Actual parameter : This is the argument which is used while calling a function.
  • A function can have many arguments of same or different data types.
  • The data type of the actual parameter must match with the data type of formal parameter.
  • The count of formal parameter and actual parameter must be same.

Call by value

In call by value method a copy of actual arguments is passed into formal arguments in the function definition. Any change in the formal parameters of the function have no effect on the value of actual argument. Call by value is the default method of passing parameters in C++. Different memories are allocated for the formal and actual parameters.

C++ Program to Pass Arguments to Function as Call by Value
#include <iostream>
using namespace std;
  
void getDoubleValue(int F){
   F = F*2;
   cout << "F(Formal Parameter) = " << F;
}
 
int main(){
   int A;
   cout << "Enter a number\n";
   cin >> A;
   // Calling function using call by value
   getDoubleValue(A);
   /* Any change in the value of formal parameter(F)
   will not effect the value of actual parameter(A) */
   cout << "\nA(Actual Parameter) = " << A;

   return 0;
}
Output
Enter a number
5
F(Formal Parameter) = 10
A(Actual Parameter) = 5

Above program shows that, their is no change in the value of A(Actual parameter) even if the value of F(Formal parameter) changed inside function.


Call by reference

In call by reference method the address of the variable is passed to the formal arguments of a function. Any change in the formal parameters of the function will effect the value of actual argument. Same memory location is accessed by the formal and actual parameters.

C++ Program to Pass Arguments to Function as Call by Reference
#include <iostream>
using namespace std;
  
void getIncrementValue(int *ptr){
   *ptr = *ptr + 1;
   cout << "F(Formal Parameter) = " << *ptr;
}
 
int main(){
   int A;
   cout << "Enter a number\n";
   cin >> A;
   // Calling function using call by reference
   // by passing address of A
   getIncrementValue(&A);
   /* Any change in the value of formal parameter(F)
   will effect the value of actual parameter(A) */
   cout << "\nA(Actual Parameter) = " << A;

   return 0;
}
Output
Enter a number
5
F(Formal Parameter) = 6
A(Actual Parameter) = 6
In above program, we passed the address of variable A to function getIncrementValue, which incremented the value stored at location pointed by pointer ptr. Here, any change in formal parameters of function will change the value of actual parameter also because both are pointing to same memory location.