C++ Academy · Lesson

Function Pointers

Pass functions as arguments.

Lesson 1 of 413 steps

Function Pointers is a free C++ Academy lesson on CoddyKit. This is lesson 1 of 4. Any 3 lessons of this learning path are free to read in full — after that, CoddyKit PRO unlocks every lesson, plus hands-on practice with a built-in code editor and a 24/7 AI tutor. It is part of the C++ Academy learning path, and your progress syncs across the web and the CoddyKit app. The C++ Academy course includes 4 lessons in total.

What Is a Function Pointer?

A function pointer stores the address of a function so you can call it indirectly or pass it around like data.

  • The type encodes the signature.
  • It lets functions take behavior as a parameter.
#include <iostream>

int add(int a, int b) { return a + b; }

int main() {
    int (*op)(int, int) = add;
    std::cout << op(3, 4) << '\n';
    return 0;
}

Declaring the Type

The declaration int (*op)(int, int) reads as: op is a pointer to a function taking two ints and returning an int. The parentheses around *op are required.

#include <iostream>

int square(int x) { return x * x; }

int main() {
    int (*fn)(int) = square;
    std::cout << fn(5) << '\n';
    return 0;
}

Calling Through a Pointer

You call a function pointer just like the function itself. You can also dereference it explicitly, but that is rarely needed.

#include <iostream>

void greet() { std::cout << "Hello\n"; }

int main() {
    void (*g)() = greet;
    g();
    (*g)();
    return 0;
}

Passing Functions as Arguments

A function can accept a function pointer parameter, letting the caller plug in custom behavior.

#include <iostream>

int apply(int x, int (*fn)(int)) { return fn(x); }
int doubler(int n) { return n * 2; }

int main() {
    std::cout << apply(10, doubler) << '\n';
    return 0;
}

Choosing at Runtime

Because a function pointer is a variable, you can decide which function to call while the program runs.

#include <iostream>

int add(int a, int b) { return a + b; }
int sub(int a, int b) { return a - b; }

int main() {
    bool wantAdd = true;
    int (*op)(int, int) = wantAdd ? add : sub;
    std::cout << op(8, 3) << '\n';
    return 0;
}

Arrays of Function Pointers

Store several function pointers in an array to build a simple dispatch table indexed by a value.

#include <iostream>

int add(int a, int b) { return a + b; }
int mul(int a, int b) { return a * b; }

int main() {
    int (*ops[2])(int, int) = {add, mul};
    std::cout << ops[0](2, 3) << ' ' << ops[1](2, 3) << '\n';
    return 0;
}

Using a typedef

The pointer syntax is noisy. A typedef or using alias gives the type a readable name.

#include <iostream>

using BinOp = int(*)(int, int);
int add(int a, int b) { return a + b; }

int main() {
    BinOp op = add;
    std::cout << op(6, 7) << '\n';
    return 0;
}

Function Pointers with Algorithms

Standard algorithms accept callables, and a plain function works as one. Here a predicate counts even numbers.

#include <iostream>
#include <vector>
#include <algorithm>

bool isEven(int n) { return n % 2 == 0; }

int main() {
    std::vector<int> v = {1, 2, 3, 4, 5, 6};
    int c = std::count_if(v.begin(), v.end(), isEven);
    std::cout << "Evens: " << c << '\n';
    return 0;
}

Limitation: No State

A plain function pointer cannot carry any captured data. If you need to remember context between calls, you must use globals or move to richer callables.

#include <iostream>

int counter = 0;
int next() { return ++counter; }

int main() {
    int (*fn)() = next;
    std::cout << fn() << ' ' << fn() << ' ' << fn() << '\n';
    return 0;
}

Capture-less Lambdas Convert

A lambda that captures nothing can implicitly convert to a matching function pointer, which is handy for small inline functions.

#include <iostream>

int main() {
    int (*fn)(int) = [](int x) { return x + 100; };
    std::cout << fn(1) << '\n';
    return 0;
}

Null Function Pointers

A function pointer can be nullptr, meaning it points to nothing. Always check before calling to avoid undefined behavior.

#include <iostream>

int main() {
    int (*fn)(int) = nullptr;
    if (fn) {
        std::cout << fn(5) << '\n';
    } else {
        std::cout << "No function set\n";
    }
    return 0;
}

Quick Check

Test your understanding of function pointer limitations.

Recap

You learned about function pointers:

  • they hold a function's address and are called like the function
  • they let you pass behavior as a parameter and build dispatch tables
  • aliases make their type readable
  • they cannot carry state, but capture-less lambdas convert to them

Next, you will meet std::function, which can store any callable including stateful ones.

Free to start

Learn C++ with an AI tutor — free

Write and run real code in your browser, get instant help from a 24/7 AI tutor, and pick up where you left off on the web or in the app.

Courses
51
Lessons
203

Frequently Asked Questions

Is the “Function Pointers” lesson free?

Yes — any 3 lessons of the C++ Academy learning path, including “Function Pointers”, are free to read in full here on the web. After that, CoddyKit PRO unlocks every lesson, plus interactive practice with a built-in code editor and a 24/7 AI tutor. The C++ Academy course includes 4 lessons in total.

What will I learn in “Function Pointers”?

Pass functions as arguments. You practise C++ Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start C++ Academy?

No prior experience is required. C++ Academy on CoddyKit is structured for beginners through advanced learners, so you can start here or from the beginning and move at your own pace. This is lesson 1 of 4.

How long does the “Function Pointers” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this C++ Academy lesson?

Yes. Every C++ Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

All lessons in this course

  1. Function Pointers
  2. std::function
  3. std::bind
  4. Choosing Callables
← Back to C++ Academy