Ad space (banner)
๐Ÿ”งC Lessons
Lesson 29 / 68

Higher-Order Functions (Passing a Function Pointer as an Argument)

This lesson covers passing a function pointer as an argument in C, so you can treat logic itself as a component. It's for anyone searching "C function pointer usage."

Passing another function's address (a function pointer) as an argument is called a higher-order function. It's like "specifying only the method to use, and leaving the actual work to whoever receives it."

The example's bubbleSort() function takes a function pointer, int (*cmp)(int, int), as an argument, calling cmp() for every comparison. Passing a different function, compareDescending(), achieves a descending sort.

A common early mistake is the syntax for declaring a function pointer. The distinctive form, return-type (*variableName)(parameter-type-list), can feel confusing at first.

In real projects, designing with function pointers as callbacks is an important technique for achieving flexible, swappable logic in C.

๐Ÿ“– Reference code
โœ๏ธ Your code
Type your code, then press "Run"

๐Ÿงช This site can't compile or run C directly, so it checks on the spot whether what you typed matches the reference code (scoring happens entirely in your browser โ€” nothing is sent anywhere).

Ad space (banner)
Ad space (in-article)