C++ Function Call By Pointer
Last Updated :
30 Nov, 2022
Several ways exist in which data (or variables) could be sent as an argument to a function. Two of the common ones are Passing by Value and Passing by Reference.
Example:
C++
// C++ Program to demonstrate
// Pass by value and
// Pass by reference
#include <iostream>
using namespace std;
// Pass by reference
void swap(int& x, int& y)
{
int temp = x;
x = y;
y = temp;
}
// Pass by value
int sum(int x, int y) { return x + y; }
int main()
{
int x = 1, y = 2;
cout << "Before swap:" << x << " " << y << endl;
swap(x, y);
cout << "After swap:" << x << " " << y << endl;
int c = sum(x, y);
cout << "Sum:" << c << endl;
return 0;
}
OutputBefore swap:1 2
After swap:2 1
Sum:3
Another lesser-known method is Call by Pointer. Passing by a pointer allows a function to modify a variable without creating a copy.
Call by Pointer
When a call-by-pointer method is used for passing arguments to a function, the address of the variable is passed as an argument to the function. The function stores this address of variables in a pointer variable. Hence, the variable inside the function is an alias for the passed variable since both contain the same address. Therefore, any operations performed on the variable inside the function will also be reflected in the calling function.
- This ability to reflect changes could return more than one value by a function.
- Also, a void function could technically return value/s using this method.
The & (address of) operator in the actual parameters denotes values passed by pass-by-pointer in a function.
Function Call by Pointer
Passing the variable address from the calling function and using them as a pointer inside the function is called the call-by-pointer. This method allows clearer visibility of functions in which the value of the passed variables may change. i.e., In pass-by-reference, there is no indication in the function call that could be used to determine that the value of the passed variables may change in it (as the call is identical to call by value). But in call-by-pointer, the address operator & is used in the function call, providing a bit more intel about the nature of the function.
Example:
C++
// C++ program to implement
// pass-by-reference with pointers
#include <iostream>
using namespace std;
void f(int *x)
{
*x = *x - 1;
}
// Driver code
int main()
{
int a = 5;
cout << a << endl;
f(&a);
cout << a << endl;
}
Explanation: Firstly a function is defined with the return datatype void and takes in value as pointers (as denoted by the * asterisk sign in the formal parameters). The function decrements the value of its formal parameter by 1. After which, inside the main function, an integer variable named 'a' is initialized with the value 5. Then this value is displayed. The function is called, and the address of the variable is passed as an argument.
Inside the function, the pointer variable's value is decremented by 1. Upon returning from the function, the variable's value is again displayed, which turns out to be 1 less than the original value.
Similar Reads
C++ Function Call By Value A function is a collection of statements that accept inputs, carry out certain calculations, and output the results. The concept is to group similar or often performed actions into a function so that we may call the function rather than writing the same code again for various inputs. A function is a
4 min read
Function Pointer in C In C, a function pointer is a type of pointer that stores the address of a function, allowing functions to be passed as arguments and invoked dynamically. It is useful in techniques such as callback functions, event-driven programs, and polymorphism (a concept where a function or operator behaves di
6 min read
Function Pointer in C++ Prerequisites: Pointers in C++Function in C++ Pointers are symbolic representations of addresses. They enable programs to simulate call-by-reference as well as to create and manipulate dynamic data structures. Iterating over elements in arrays or other data structures is one of the main use of point
4 min read
Function Pointers and Callbacks in C++ A callback is a function that is passed as an argument to another function. In C++, we cannot directly use the function name to pass them as a callback. We use function pointers to pass callbacks to other functions, store them in data structures, and invoke them later. In this article, we will discu
2 min read
Function Call by Reference in Objective-C Just like other languages in objective-C also a function is used to perform some specific task. In objective-C, we can call a function by value or by reference. So, in this article, we will talk about the call by reference in Objective-C. The call-by-reference is a process of passing arguments to a
3 min read
atexit() function in C/C++ The function pointed by atexit() is automatically called without arguments when the program terminates normally. In case more than one function has been specified by different calls to the atexit() function, all are executed in the order of a stack (i.e. the last function specified is the first to b
3 min read