In this tutorial, we will provide a detailed overview of how to pass arguments to a function in C++, including passing values, references, and pointers, each with a program example.
By the end of this tutorial, you will have a clear understanding of the different methods for passing arguments to functions in C++.
What are Arguments in C++?
In C++, arguments are pieces of information passed to a function when it is called. Arguments are also referred to as parameters or actual parameters.
They are the values that are specified when a function is called.
The values are then passed to the function, and the function can use them to perform its specified task.
For example, if a function requires two values, the programmer can pass two arguments when calling the function. The function receives the arguments, which can then use them in its code.
So let’s say you have a function that adds two numbers and want to add the numbers 3
and 5
; you would call the function like this: add(3,5)
.
Here, the arguments are 3
and 5
, and they are passed to the function to be added together.
C++ Functions – Pass by Values
Pass by value to functions in C++ is a way of passing arguments to a function wherein the argument is copied into a new variable within the function’s scope.
The function operates on the copied data rather than the original argument.
This means the original argument remains unchanged, regardless of what happens to the argument within the function.
Pass by value is the default method of argument passing in C++ and is used when the argument is a primitive data type (e.g., int, float, char, etc.).
Example 1: Passing Arguments To Functions by Value
//Passing Arguments To Functions By Value #include <iostream> using namespace std; // function to pass parameters by value void PassbyValue(int x, int y) { x = 50; // assign value 50 to x y = 30; // assign value 30 to y } int main() { int x = 10; // declaring and initializing x with 10 int y = 20; // declaring and initializing y with 20 PassbyValue(x,y); // call passbyvalue() function and pass x and y as parameters cout<<"Value of X: "<<x<<endl; // print the value of x cout<<"Value of Y: "<<y<<endl; // print the value of y return 0; }
OUTPUT OF THE PROGRAM
Value of X: 10
Value of Y: 20
Here,
A function named PassbyValue()
is defined, which takes two integer arguments, x
and y
.
In the main()
function, two integer variables, x
and y
, are declared and initialized with 10 and 20, respectively. Then, the PassbyValue()
function is called, and the two variables are passed as arguments to the function.
Inside the PassbyValue()
function, the values of the arguments are changed to 50 and 30, respectively. But, since the arguments are passed by value, the changes made to the arguments inside the function are not reflected outside the function.
Finally, the value of x
and y
are printed, and it can be seen that the values are still 1
0 and 20. This shows that the parameters have been passed by value to the function.
C++ Functions – Pass by Reference
Pass by reference to function in C++ is a method of passing arguments to a function by reference instead of by value.
This means that the formal parameter declared in the function definition acts as an alias for the argument passed to the function, which is an object in memory.
Any changes made to the parameter inside the function are reflected in the argument used to call the function.
This allows functions to modify their arguments directly rather than making a copy of the argument and modifying the copy.
Example 2: Passing Arguments To Functions by Reference
//Passing Arguments To Functions By Reference #include <iostream> using namespace std; // function to pass parameters by reference void PassbyReference(int &x, int &y) { x = 50; // assign value 50 to x y = 30; // assign value 30 to y } int main() { int a = 10; // declaring and initializing a with 10 int b = 20; // declaring and initializing b with 20 PassbyReference(a,b); // call passbyreference() function and pass a and b as parameters cout<<"Value of X: "<<a<<endl; // print the value of x cout<<"Value of Y: "<<b<<endl; // print the value of y return 0; }
OUTPUT OF THE PROGRAM
Value of X: 50
Value of Y: 30
Here,
Two integer variables, x
and y
, are declared and initialized with values 10 and 20, respectively.
A function named PassbyReference()
is declared and defined with two parameters, x
and y
, which are passed by reference. Inside the function, the values of x
and y
are assigned 50 and 30, respectively.
The PassbyReference()
is called inside the main()
function with x
and y
as parameters.
Once the PassbyReference()
function is executed, the values of x
and y
are changed to 50 and 30, respectively.
Example 1 passes parameters by value, where the value of the parameter is passed to the function, and any changes made to the parameter inside the function are local to the function and do not affect the original value of the parameter.
In contrast, Example 2 passes parameters by reference, where the address of the parameter is passed to the function, so any changes made to the parameter inside the function are reflected in the parameter’s original value.
C++ Functions – Pass by Pointer
Passing by pointer to a function in C++ means passing the address of an argument to a function instead of passing the argument itself.
This allows the function to modify the value of the argument. When a function is called by passing a pointer to it, the argument is said to be passed by reference.
The pointer is the reference to the argument, and the argument itself is not copied. This can be useful when the argument is large, such as a structure or an array.
It also makes it easier to pass a pointer to a function with multiple parameters, as the address of the argument can be passed in a single pointer.
Example 3: Passing Arguments To Functions by Pointers
//Passing Arguments To Functions By Pointers #include <iostream> using namespace std; // Function definition for PassbyPointers // This function takes two integer pointers as input arguments void PassbyPointers(int *a, int *b) { *a=115; *b=214; } int main() { // Declare and initialize two integer variables x and y int x = 25; int y = 45; // Call the PassbyPointers function and pass the addresses of x and y as input arguments PassbyPointers(&x,&y); // Print the value of x and y after the PassbyPointers function has been called cout<<"Value of X: "<<x<<endl; cout<<"Value of Y: "<<y<<endl; return 0; }
OUTPUT OF THE PROGRAM
Value of X: 115
Value of Y: 214
Here,
The program begins by declaring and initializing two integer variables, x
and y
, with the values 25 and 45.
A function PassbyPointers
is then defined, which takes two integer pointers as input arguments. This function sets the values of the two pointers to 115 and 214, respectively.
The main function calls the PassbyPointers
function and passes the addresses of x
and y
as input arguments. This will cause the values of x
and y
to be changed within the PassbyPointers
function.
Finally, the program prints the value of x
and y
after the PassbyPointers
function has been called.