100% Free Forever
AI-Powered Learning
Industry Expert Content
Certificates & Badges
Learn At Your Own Pace
C++

Pointers and Functions in C++

Learn how passing a pointer to a function lets it modify the caller's original variable, unlike ordinary pass-by-value arguments.

PointersIntermediate7 min readJul 7, 2026
Analogies

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

cpp
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

cpp
#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

text
After addTenByValue: a = 5
After addTenByPointer: b = 15

6. 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

Was this page helpful?

Topics covered

#CStudyNotes#Programming#PointersAndFunctionsInC#Pointers#Functions#Syntax#Explanation#StudyNotes#SkillVeris#ExamPrep