Free preview.You're sampling one lesson — enroll free to unlock all 10 lessons and track your progress.
Enroll free lesson
Closures and Higher-Order Functions
Closures and Higher-Order Functions
In this lesson — part of Advanced Features — you'll learn closures and higher-order functions in C and why it matters in real work.
Why it matters
Functions that take or return functions unlock concise, composable code.
Key ideas
- Functions as values
- map / filter / reduce
- Callbacks
- Composition
In practice
Here's how it looks in idiomatic C:
#include <stdio.h>
int square(int x) { return x * x; }
int is_even(int x) { return x % 2 == 0; }
int main(void) {
int src[] = { 1, 2, 3, 4, 5, 6 };
int out[6], k = 0;
for (int i = 0; i < 6; i++) /* filter */
if (is_even(src[i])) out[k++] = square(src[i]); /* then map */
for (int i = 0; i < k; i++) printf("%d ", out[i]);
putchar('\n');
return 0;
}
C note: C has no built-in map/filter, but functions are first-class via function pointers, so you pass int (*f)(int) to a loop to get the same effect.
Try it yourself
Exercise: In C, use map and filter to get the squares of the even numbers.
Recap
You now understand closures and higher-order functions and can apply it in C. Mark this lesson complete and continue to the next one.
