Python 11. Recursion & High-Speed Function Pointers
A function pointer stores the memory address of an executable function in the code text segment. Function pointers enable dynamic dispatch, strategy patterns, and custom comparator callbacks for standard library algorithms like qsort().
"A regular pointer points to a data record on a shelf; a function pointer points to a button on the wall that executes a specific action when pushed."
Deep Dive: How It Works
Function Pointers in Memory: Functions reside in the executable code segment of process RAM.
Comparator Signature: int (*cmp)(const void *a, const void *b) for standard library qsort.
Recursion Mechanics: Each recursive call adds a new stack frame; base cases are mandatory.
Syntax Blueprint
int (*operation)(int, int) = add; int result = operation(10, 20);
Declare function pointer matching signature and assign target function address.
Core Rules to Remember



Common Beginner Traps & How to Fix Them
Declaring function pointer without parentheses around (*ptrName): int *ptr(int, int).Why it happens: Without parentheses, this declares a function returning an int pointer rather than a pointer to a function.
How to fix: Always write int (*ptr)(int, int).
Live Interactive Example
Hit Run Code to see it liveYour Turn: Micro Challenge
No pressure! Edit the starter code below and test your solution with instant feedback.
Inspect Sorted Median with qsort
Print the sorted median element at index 2 with "Median: %d\n".
Finished reading and practicing?
Mark this lesson as completed to update your course progress.