Cpp Function Call By Pointer
## C++ Function Call by Pointer
In C++, there are three primary ways to pass arguments to a function: **Call by Value**, **Call by Reference**, and **Call by Pointer**.
This tutorial focuses on the **Call by Pointer** method. This approach copies the address of an argument into the formal parameter of the function. Inside the function, this address is used to access and manipulate the actual argument used in the call. This means that any changes made to the parameter inside the function will directly affect the original argument.
---
### How Call by Pointer Works
When passing arguments by pointer, you pass the memory address of the variables (using the address-of operator `&`) to the function. The function parameters must be declared as pointer types (using the asterisk operator `*`).
Inside the function, you must dereference the pointers to access or modify the values stored at those memory addresses.
### Syntax and Declaration
To define a function that accepts pointers as parameters, use the following syntax:
```cpp
void functionName(type *parameter1, type *parameter2) {
// Access or modify values using dereferencing: *parameter1
}
```
When calling the function, you must pass the addresses of the variables:
```cpp
functionName(&variable1, &variable2);
```
---
### Code Example: Swapping Two Numbers
The following example demonstrates how to use pointers to swap the values of two integer variables.
#### 1. Function Definition
First, we define the `swap` function. Its parameters are declared as integer pointers (`int *x` and `int *y`).
```cpp
// Function definition to swap two values using pointers
void swap(int *x, int *y) {
int temp;
temp = *x; // Save the value at address x
*x = *y; // Assign the value at address y to address x
*y = temp; // Assign the saved value to address y
}
```
#### 2. Complete Program
Here is the complete C++ program showing how to declare, call, and verify the `swap` function.
```cpp
#include
using namespace std;
// Function declaration (prototype)
void swap(int *x, int *y);
int main () {
// Local variable declarations
int a = 100;
int b = 200;
cout << "Before swap, value of a: " << a << endl;
cout << "Before swap, value of b: " << b << endl;
/*
* Call the function to swap the values.
* &a represents a pointer to a (the memory address of a).
* &b represents a pointer to b (the memory address of b).
*/
swap(&a, &b);
cout << "After swap, value of a: " << a << endl;
cout << "After swap, value of b: " << b << endl;
return 0;
}
// Function definition
void swap(int *x, int *y) {
int temp;
temp = *x; // Save the value at address x
*x = *y; // Assign the value at address y to address x
*y = temp; // Assign the saved value to address y
}
```
#### Output
When the above code is compiled and executed, it produces the following output:
```text
Before swap, value of a: 100
Before swap, value of b: 200
After swap, value of a: 200
After swap, value of b: 100
```
---
### Key Considerations and Best Practices
While "Call by Pointer" is a powerful tool inherited from C, modern C++ developers should keep the following points in mind:
* **Null Safety:** Pointers can be `nullptr`. Inside a function that accepts pointers, it is highly recommended to check if the pointers are null before dereferencing them to avoid segmentation faults:
```cpp
void safeSwap(int *x, int *y) {
if (x != nullptr && y != nullptr) {
int temp = *x;
*x = *y;
*y = temp;
}
}
```
* **Syntax Overhead:** Working with pointers requires explicit address-of (`&`) operators at the call site and dereference (`*`) operators inside the function body, which can make the code more verbose.
* **Pointer vs. Reference:** In modern C++, **Call by Reference** (`void swap(int &x, int &y)`) is generally preferred over Call by Pointer. References cannot be null, do not require dereferencing syntax, and provide a cleaner, safer interface while achieving the same performance benefits. Use pointers only when you explicitly need to support null values or perform pointer arithmetic.
YouTip