What is a function in C? The call stack and function pointers for callbacks
Atlasbot · 16/8/2026 · 10 min read
Functions are a familiar concept to anyone who has written code — but in embedded programming, two aspects of functions that are often overlooked are the ones that cause errors the most: the call stack works, and functions can also be passed as a value via function pointers.
Key concepts
A function takes input parameters, performs calculations, and may return a value:
int16_t clamp(int16_t value, int16_t min, int16_t max) {
if (value < min) return min;
if (value > max) return max;
return value;
}
There are two ways to pass parameters: pass by value (function receives a copy, editing in the function does not affect the original variable — like the example above) and pass by pointer (function receives address, can directly edit the original variable of the caller, and avoid copying large data — see Pointer article).
Call stack — where function calls are "stacked"
Each time a function is called, a new stack frame is pushed to the top of the stack, containing: the return address, parameters, and local variables of that function. When the function ends, this frame is popped from the stack, and the program returns to the correct location.
In short: Each function call consumes a certain amount of RAM stack until the function returns — calling the function too deeply (uncontrolled recursion, or declaring a large local array in the function) can overflow the stack, one of the most difficult errors to debug on a microcontroller because it often causes "hangs" for unknown reasons.
How it works
main() calls read_sensor()
→ Push stack frame of read_sensor()
read_sensor() calls convert_to_celsius()
→ Push stack frame of convert_to_celsius()
← Pop khi convert_to_celsius() return
← Pop khi read_sensor() return
main() continues to run
Function pointer — used when you need to "pass a behavior"
A function pointer stores the address of a function, allowing calling that function indirectly — the foundational mechanism of all embedded callbacks, for example registering an interrupt handler function:
typedef void (*InterruptHandler)(void);
void on_button_press(void) {
// Handled when the button is pressed
}
InterruptHandler handler = on_button_press;
handler(); // Call on_button_press() indirectly
In fact, this is exactly how HAL libraries register callback functions for interrupts or timers: you write a function according to the "signature" required by the library, pass in the function address, and the hardware will automatically call it back at the right time when the event occurs — no different from the way JavaScript or Python pass functions as callbacks, the only difference is that in C it must be declared explicitly via function pointers.
Technical details
Formulas
Quickly estimate how much stack space a sequence of nested function calls will consume — useful for checking if there's a risk of stack overflow before actually running it on a small RAM chip:
Stack uses ≈ Σ (local variable + parameter + return address) of each nested function
Example: 3 nested functions, each using ~16 bytes (local variable + overhead)
main() → read_sensor() → convert_to_celsius()
The stack uses ≈ 3 × 16 = 48 bytes at its deepest point
This number increases rapidly if there is recursion or large local array declarations inside the function — a uint8_t buffer[200]; local array declaration in a function takes up almost the entire 2KB of RAM of an 8-bit MCU in just 1 call.
Parameters
The default stack size varies significantly between platforms — exceeding this limit usually does not result in a compile error, just a "hang" at runtime:
| Platform | Typical stack capacity |
|---|---|
| Arduino Uno (shared with heap in 2KB RAM) | Several hundred bytes are actually available |
| STM32 (configurable in linker script) | Usually 1-2KB (depending on project) |
| ESP32 (each FreeRTOS task has its own stack) | Usually 4-8KB per task |
Register / Bit / Byte
On the ARM Cortex-M architecture, the "top" location of the call stack is stored in a dedicated CPU register called Stack Pointer (SP, which is register R13) — each time a function is called, the hardware automatically decrements the SP value (the stack "grows down" to a lower address) to make room for a new stack frame; When the function returns, SP increases itself again to "free" that area. A stack overflow is essentially when the SP drops below the RAM area for the stack, overriding the memory area used for other purposes (global variables, heap) — this is why the consequence of a stack overflow is often corrupted program data in a place completely unrelated to the function just called.
Practical applications
Function pointers are the basis for implementing state machines (state machines) that control robot behavior — each state (eg: IDLE, MOVING, CHARGING, EMERGENCY_STOP) corresponds to a separate processing function, stored in an array of function pointers looked up by the current state index. The main loop only needs to call state_handlers[current_state]() every cycle, no need for a long chain of if/else or switch — adding a new state only requires writing 1 more function and 1 line in the array, without having to edit the existing dispatch logic.
Practical example
#include <stdio.h>
void level_3(int depth) {
int local_var = depth;
printf("level_3: dia chi bien cuc bo = %p\n", (void*)&local_var);
}
void level_2(int depth) {
int local_var = depth;
printf("level_2: dia chi bien cuc bo = %p\n", (void*)&local_var);
level_3(depth + 1);
}
void level_1(int depth) {
int local_var = depth;
printf("level_1: dia chi bien cuc bo = %p\n", (void*)&local_var);
level_2(depth + 1);
}
int main() {
level_1(1);
return 0;
}
Output (illustrative address, decreasing according to function call depth):
level_1: dia chi bien cuc bo = 0x7ffee2a1b8ac
level_2: dia chi bien cuc bo = 0x7ffee2a1b88c
level_3: dia chi bien cuc bo = 0x7ffee2a1b86c
The local variable address gradually decreases with each layer of function calls — a visual illustration of the "growing down" stack mentioned in the Register/Bit/Byte section. The deeper the function call, the closer the address gets to the bottom of the RAM area available for the stack.
Common mistakes
- Stack overflow due to recursion without clear stop condition, or recursion too deep — each recursive call consumes an additional stack frame, easily exceeding a few hundred bytes to several KB of RAM for the stack — deep recursion should be avoided on microcontrollers with limited resources, loops are preferred.
- Declaring a large array/struct as a local variable in a function — local variables are on the stack, an array of several hundred bytes declared locally can take up most of the stack in just one function call — with large data, priority is given to
static/global declaration or static allocation outside the function. - Call a function pointer of the wrong type (signature mismatch) — assigning a function with parameters/return types different from the declared function pointer type is undefined behavior, can be compiled but runs incorrectly or crashes.
- Forgot
returnin a function that is notvoid— some compilers only warn but do not report errors, the program still runs but the return value is unknown garbage.
Conclusion
Embedded C functions are more than just code organizers — the call stack determines the practical limits on function call depth and local variable size on a limited RAM system, and function pointers are the foundational mechanism for all interrupt callbacks and state machines. The next article in the category is about Memory in C — a complete picture of the memory areas (stack, heap, static, flash) in which the functions and variables just learned are located.
Get new articles and lessons by email
When a new technical article or course module is published, Atlas Robotics sends you a short email. No spam.
Unsubscribe any time with the link in each email. See our Privacy policy.