← Back to C++ Course | Chapter 7: Pointers & References | Lesson 6 of 11

C++ Pointers & Functions

Passing pointers to functions lets them work directly on the original data, like handing over an address instead of a copy. A function pointer even lets you pass a function around.
Syntax
cpp
void function_name(data_type *param) {
  *param = new_value;
}

function_name(&variable_name);

Passing Pointers to Functions

Passing pointers to functions lets the function reach into the caller's memory and modify the original variables directly, a technique known as passing by pointer.

This is how functions simulate 'output parameters' in a language that otherwise passes arguments by value.

Example: Passing Pointers to Functions

cpp
// Include std::cout and std::cin
#include <iostream>

void setValue(int *ptr, int value) {
	*ptr = value;
}

// Program execution starts in main()
int main() {
	// Declare num and set it to 0
	int num = 0;
	setValue(&num, 42);
	// Print to the console with cout
	std::cout << num << std::endl;
	// Return 0 to signal that the program finished successfully
	return 0;
}

Returning Pointers from Functions

A function can return a pointer, but you should never return a pointer to a local variable created inside that function, since local variables live on the stack and are destroyed the moment the function returns.

Only return pointers to static memory, heap-allocated memory, or memory owned by the caller.

Example: Returning Pointers from Functions

cpp
#include <iostream>

int *makeHeapValue(int value) {
	int *ptr = new int(value); // never return a pointer to a local variable
	return ptr;
}

int main() {
	int *result = makeHeapValue(10);
	std::cout << *result << std::endl;
	delete result;
	return 0;
}

Constant Pointers in Functions

You can protect a function's parameters from modification by declaring the pointer as const, telling both the compiler and future readers that the pointed-to data is read-only inside that function.

This catches accidental writes at compile time instead of at runtime.

Example: Constant Pointers in Functions

cpp
#include <iostream>

void printValue(const int *ptr) {
	std::cout << *ptr << std::endl;
	// *ptr = 5; // compile error: cannot modify through a const pointer
}

int main() {
	int num = 7;
	printValue(&num);
	return 0;
}

Function Pointers

Because functions occupy addresses in memory just like variables, C++ lets you declare pointers to functions and call them indirectly at runtime, choosing which function to invoke based on program logic rather than hardcoding the call.

This is the low-level mechanism behind dynamic dispatch.

Example: Function Pointers

cpp
// Include std::cout and std::cin
#include <iostream>

int square(int x) {
	// Send x * x back to the caller
	return x * x;
}

// Program execution starts in main()
int main() {
	int (*funcPtr)(int) = square;
	// Print to the console with cout
	std::cout << funcPtr(5) << std::endl;
	// Return 0 to signal that the program finished successfully
	return 0;
}

Callback Functions with Function Pointers

You can pass function pointers as arguments to other functions, letting a general-purpose function (like a sorter or event handler) execute custom behavior supplied by the caller.

This callback pattern predates std::function and lambdas but still appears throughout C-style C++ APIs.

Example: Callback Functions with Function Pointers

cpp
// Include std::cout and std::cin
#include <iostream>

void runTwice(void (*callback)()) {
	callback();
	callback();
}

void sayHi() {
	// Print to the console with cout
	std::cout << "Hi!" << std::endl;
}

// Program execution starts in main()
int main() {
	runTwice(sayHi);
	// Return 0 to signal that the program finished successfully
	return 0;
}
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
Common Mistakes
  1. Returning a pointer to a local variable, which is destroyed when the function returns, leaving a dangling pointer.
  2. Dereferencing a pointer parameter without checking for nullptr first.
  3. Writing int *f(int) when you mean a function pointer, which declares a function returning a pointer; a function pointer needs int (*f)(int).

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.