Normal Parameter Passing in C++
In C++, normal parameter passing is also known as “pass-by-value“, in this parameter passing technique the compiler creates a copy of the argument being passed. Any changes made to formal parameters within a function are not reflected in the original arguments. If the parameters passed are very large, then the copying process becomes very tedious and results in wasting our storage and CPU cycles.
Syntax for Normal Parameter Passing
returnType functionName(dataType parameterName); //pass by value
Example
The below example demonstrates how we can pass parameters to function by value in C++.
// C++ program to demonstrate normal parameter passing
#include <iostream>
using namespace std;
// function to modify the value
void increase(int num) { num += 5; }
int main()
{
// define avariable
int x = 10;
// calling increase function by passing parameter x
increase(x);
// printing the variable x
cout << x; // 10
}
Output
10
Explanation: In the above example, the value of actual parameter x
remains 10 even after the increase() function call because num
is a copy of x
so changes are not reflected back in x.
Const Reference vs Normal Parameter Passing in C++
In C++, when we call a function we can also pass the parameters to the function in many ways like pass by value, pass by reference, pass by pointer, and by const reference. Each parameter-passing technique has its own advantages and disadvantages. In this article, we will learn the difference between normal parameter passing and passing by const reference in C++.
Contact Us