1. Intro
By default, C++ functions receive arguments by value: the function gets a copy, and changes made inside the function do not affect the caller's original variable. Passing a pointer to a function is one way to work around this — since the function receives the address of the original variable, it can dereference that pointer to read and modify the caller's actual data.
Cricket analogy: Handing a scorer a photocopy of the scoresheet lets them scribble on the copy without changing the real scoresheet, but handing them the actual scoresheet's location lets their edits show up on Sachin Tendulkar's real innings record.
2. Syntax
void modify(int* ptr) {
*ptr = *ptr + 10; // modifies the original variable
}
int main() {
int x = 5;
modify(&x); // pass the address of x
}3. Explanation
When a function parameter is a plain type like int val, C++ copies the caller's argument into val; any change to val inside the function is local and disappears when the function returns — the caller's original variable is untouched. This is pass by value. When a function parameter is instead a pointer, like int* ptr, the caller passes the *address* of its variable (using &x). The function still receives a copy — but the copy is of the address, not the value. Dereferencing that address with *ptr inside the function reaches the exact same memory as the caller's original variable, so *ptr = ... really does change it. This is how C++ (and C before it) simulates "pass by reference" using pointers. Modern C++ also offers true reference parameters (int& ref) which achieve the same effect with cleaner syntax and no need to dereference, but understanding the pointer-based mechanism is essential since it underlies references and is still widely used, especially for optional (nullable) parameters.
Cricket analogy: Passing int val to a scoring function is like giving a fan a copy of Rohit Sharma's run total to circle — circling it changes nothing real; passing int* ptr via &runs gives the function the actual scoreboard address, so *ptr = 50 truly updates the real total, the same effect achieved more cleanly with a reference like int& runs.
4. Example
#include <iostream>
using namespace std;
void addTenByValue(int val) {
val = val + 10; // only changes the local copy
}
void addTenByPointer(int* ptr) {
*ptr = *ptr + 10; // changes the caller's original variable
}
int main() {
int a = 5;
addTenByValue(a);
cout << "After addTenByValue: a = " << a << endl;
int b = 5;
addTenByPointer(&b);
cout << "After addTenByPointer: b = " << b << endl;
return 0;
}5. Output
After addTenByValue: a = 5
After addTenByPointer: b = 156. Key Takeaways
- Pass-by-value gives a function a copy of the argument; changes inside the function do not affect the caller's variable.
- Passing a pointer gives the function the address of the caller's variable, so dereferencing it (
*ptr = ...) modifies the original. - This pointer-based technique is how C++ simulates pass-by-reference.
- True reference parameters (
int& ref) achieve the same effect with simpler syntax, but pointers remain useful, e.g. for optional/nullable arguments.
Practice what you learned
1. When a function parameter is passed by value (e.g. `void f(int val)`), what happens to the caller's original variable?
2. How does passing a pointer allow a function to modify the caller's variable?
3. In `void f(int* ptr) { *ptr = 20; }` called as `f(&x);`, what happens to `x`?
4. What C++ language feature provides a similar effect to pass-by-pointer but with cleaner syntax?
5. Why might pointers still be preferred over references for some function parameters?
Was this page helpful?
You May Also Like
Pointers in C++
Learn what a pointer is, how to declare one, and how the `&` and `*` operators let you access and manipulate memory addresses directly.
Function Parameters and Arguments in C++
Learn the difference between function parameters and arguments in C++ and understand pass-by-value semantics with a hands-on example.
Passing Arrays to Functions in C++
Understand how arrays decay to pointers when passed to C++ functions, why size must be passed separately, and how this differs from pass-by-value.
Related Reading
Related Study Notes in Programming
Browse all study notesApache Spark Study Notes
Programming · 30 topics
ProgrammingApache Flink Study Notes
Programming · 30 topics
ProgrammingHadoop Study Notes
Programming · 30 topics
ProgrammingSnowflake Study Notes
Programming · 30 topics
ProgrammingApache Airflow Study Notes
Programming · 30 topics
Programmingdbt (Data Build Tool) Study Notes
Programming · 30 topics