A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://www.geeksforgeeks.org/cpp/parameter-passing-techniques-in-cpp/ below:

Parameter Passing Techniques in C++

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:

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.



RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4