Facebook Page Twitter Page LinkedIn Page
× C++ Q&A


When the argument(s) or parameter(s) of a function is passed by value, then the function takes only the value of parameter(s) and any changes made to the parameter(s) inside the function have no effect on the parameter(s). By default, C++ uses pass by value method which means that code within a function will not change the parameter(s) used to pass in the function.

Example:

In the below example, the function called Square is created which returns square of the passed argument. The function modify the parameter as square of itself before returning it. In the main() function, when the passed argument is printed after calling the function Square, the value of the parameter remains unaffected because it was passed by value.

#include <iostream>
using namespace std;
int Square(int x); 

int main (){
    int x = 5;
    cout<<"Initial value of x: "<<x<<"\n";
    Square(x);
    cout<<"Value of x, after passed by value: "<<x<<"\n";
    return 0;
}

int Square(int x){
    x = x*x;
    return x; 
}

The output of the above code will be:

Initial value of x: 5
Value of x, after passed by value: 5