Parameter Passing Techniques in C++
Last Updated :
26 Feb, 2025
In C++, data can be sent to functions when they are called in order to perform operations. This data is called parameters or arguments and there are various parameter passing methods available in C++. In this article, we will discuss various parameter-passing techniques in C++.
Before you see the techniques, first understand the difference between the following terms:
- Formal Parameters: Variables used in parameter list of a function as placeholders. Also called only parameters.
- Actual Parameters: The expressions or values passed in during a function call. Also called arguments.
There are 3 different methods using which we can pass parameters to a function in C++. These are:
1. Pass by Value
In pass by value method, a variable's value is copied and then passed to the function. As the result, any changes to the parameter inside the function will not affect the variable's original value in the caller. This method is simple, easy to understand and implement but it is not preferred for large size of data structures at it involves copying the value.
Example:
C++
#include <iostream>
using namespace std;
// Arguments are pass by value
void change(int a) {
// Modifying arguments
a = 22;
}
int main() {
int x = 5;
// Passing x by value to change()
change(x);
cout << x;
return 0;
}
In this program, when the change function is called with x as the argument, a copy of x is created and passed to the function. Inside the function, the parameter a is modified, but this modification only affects the local copy of the value, not the original variable x, as demonstrated by the output.
2. Pass by Reference
In pass-by-reference method, instead of passing the value of the argument, we pass the reference of an argument to the function. This allows the function to change the value of the original argument. This is useful when you have to pass large size data.
Example
C++
#include <iostream>
using namespace std;
// Arguments are pass by value
void change(int& a) {
// Modifying arguments
a = 22;
}
int main() {
int x = 5;
// Passing x by reference to change()
change(x);
cout << x;
return 0;
}
As we can see, the original value is modified. Just declaring the parameter a as a reference changes it from pass by value to pass by reference.
3. Pass by Pointer
The pass-by-pointer is very similar to the pass-by-reference method. The only difference is that we pass the raw address of the argument as the parameter to the function instead of reference.
Example
C++
#include <iostream>
using namespace std;
// Arguments are pass by value
void change(int* a) {
// Modifying arguments
*a = 22;
}
int main() {
int x = 5;
// Passing address of x to change()
change(&x);
cout << x;
return 0;
}
The original value is modified, but it increased to complexity of the program as we need to be careful of referencing, referencing and passing addresses. So, passing reference is preferred over this method.
Similar Reads
Parameter Passing Techniques in C In C, passing values to a function means providing data to the function when it is called so that the function can use or manipulate that data. Here:Formal Parameters: Variables used in parameter list in a function declaration/definition as placeholders. Also called only parameters.Actual Parameters
3 min read
Function Parameters in Programming Function Parameters are used to declare the input values that a function expects. The parameters of a function play a significant role while defining the function so that whenever we call the function, we ensure that necessary arguments are passed to the function. In this article, we will dive deep
3 min read
Types of Operator Overloading in C++ C++ provides a special function to change the current functionality of some operators within its class which is often called as operator overloading. Operator Overloading is the method by which we can change some specific operators' functions to do different tasks.Syntax: Return_Type classname :: op
4 min read
References in C++ In C++, a reference works as an alias for an existing variable, providing an alternative name for it and allowing you to work with the original data directly.Example:C++#include <iostream> using namespace std; int main() { int x = 10; // ref is a reference to x. int& ref = x; // printing v
5 min read
Formal and Actual Parameters in Programming In programming, parameters are variables or values passed to a function or method during its invocation. They enable the function to receive input data, perform operations, and optionally return a result. What are Formal Parameters?Formal parameters, also known as formal arguments, are placeholders
3 min read