C Academy · Lesson

Passing Functions

Callbacks as arguments.

Lesson 2 of 413 steps

Passing Functions is a free C Academy lesson on CoddyKit. This is lesson 2 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser 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.

Functions as Arguments

Because a function pointer is just a value, you can pass a function into another function. The received function is called a callback.

This lets one routine customize part of its behavior.

#include <stdio.h>

int apply(int x, int (*f)(int)) {
    return f(x);
}

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

int main(void) {
    printf("%d\n", apply(6, square));
    return 0;
}

The Callback Parameter

The callback parameter is declared just like a function pointer variable. Inside, you call it like any function.

#include <stdio.h>

void run_twice(void (*action)(void)) {
    action();
    action();
}

void beep(void) { printf("beep\n"); }

int main(void) {
    run_twice(beep);
    return 0;
}

Swapping Behavior

Pass different callbacks to get different results from the same higher-order function.

#include <stdio.h>

int combine(int a, int b, int (*op)(int, int)) {
    return op(a, b);
}

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

int main(void) {
    printf("%d\n", combine(3, 4, add));
    printf("%d\n", combine(3, 4, mul));
    return 0;
}

Mapping Over an Array

A map function applies a callback to every element of an array in place.

#include <stdio.h>

void map(int *a, int n, int (*f)(int)) {
    for (int i = 0; i < n; i++) a[i] = f(a[i]);
}

int inc(int x) { return x + 1; }

int main(void) {
    int a[] = {1, 2, 3};
    map(a, 3, inc);
    for (int i = 0; i < 3; i++) printf("%d ", a[i]);
    printf("\n");
    return 0;
}

Filtering with a Predicate

A predicate is a callback that returns true or false. Use it to count or select matching elements.

#include <stdio.h>

int count_if(int *a, int n, int (*pred)(int)) {
    int c = 0;
    for (int i = 0; i < n; i++) if (pred(a[i])) c++;
    return c;
}

int is_even(int x) { return x % 2 == 0; }

int main(void) {
    int a[] = {1,2,3,4,5,6};
    printf("%d\n", count_if(a, 6, is_even));
    return 0;
}

Reducing to One Value

A reduce function folds an array into a single value using a binary callback and a starting accumulator.

#include <stdio.h>

int reduce(int *a, int n, int init, int (*op)(int,int)) {
    int acc = init;
    for (int i = 0; i < n; i++) acc = op(acc, a[i]);
    return acc;
}

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

int main(void) {
    int a[] = {1,2,3,4};
    printf("%d\n", reduce(a, 4, 0, add));
    return 0;
}

Passing a Comparison

Sorting and searching often take a comparison callback so the same algorithm works for any ordering.

#include <stdio.h>

int max_by(int a, int b, int (*greater)(int,int)) {
    return greater(a, b) ? a : b;
}

int gt(int a, int b) { return a > b; }

int main(void) {
    printf("%d\n", max_by(3, 8, gt));
    return 0;
}

typedef for Callback Types

Naming the callback type with typedef makes function signatures readable.

#include <stdio.h>

typedef int (*IntFn)(int);

int apply(int x, IntFn f) { return f(x); }

int neg(int x) { return -x; }

int main(void) {
    printf("%d\n", apply(5, neg));
    return 0;
}

Default Callback Fallback

If a callback may be NULL, provide a default behavior so the caller can opt out.

#include <stdio.h>

int apply(int x, int (*f)(int)) {
    if (f == NULL) return x;
    return f(x);
}

int dbl(int x) { return x * 2; }

int main(void) {
    printf("%d\n", apply(9, NULL));
    printf("%d\n", apply(9, dbl));
    return 0;
}

Callbacks Decouple Code

Callbacks let a general algorithm stay independent of the specific operation. The library provides the loop; you provide the behavior.

#include <stdio.h>

void for_each(int *a, int n, void (*visit)(int)) {
    for (int i = 0; i < n; i++) visit(a[i]);
}

void show(int x) { printf("[%d]", x); }

int main(void) {
    int a[] = {7, 8, 9};
    for_each(a, 3, show);
    printf("\n");
    return 0;
}

Choosing Callback at Runtime

Combine condition selection with passing: pick a callback then hand it to a higher-order function.

#include <stdio.h>

int apply(int x, int (*f)(int)) { return f(x); }
int sq(int x) { return x * x; }
int cube(int x) { return x * x * x; }

int main(void) {
    int power = 3;
    int (*f)(int) = (power == 2) ? sq : cube;
    printf("%d\n", apply(2, f));
    return 0;
}

Quick Check

Test your understanding of passing functions.

Recap

You learned to pass functions as callbacks:

  • Declare a callback parameter like a function pointer.
  • Build higher-order functions such as map, filter, reduce, and for_each.
  • Use typedef to name callback types.
  • Guard against NULL callbacks with a default.
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
39
Lessons
144

Frequently Asked Questions

Is the “Passing Functions” lesson free?

Yes — the full text of “Passing Functions” is free to read here on the web. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the C Academy course, upgrade to CoddyKit PRO. The C Academy course includes 4 lessons in total.

What will I learn in “Passing Functions”?

Callbacks 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 2 of 4.

How long does the “Passing Functions” 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. Declaring Function Pointers
  2. Passing Functions
  3. qsort with Comparators
  4. Function Pointer Tables
← Back to C Academy