🚀 Learn BLE on Linux (BlueZ) – Programming Part 1
Hands-on Bluetooth Low Energy course. Special price: ₹399
Enroll Now on Udemy →C Function Call: What Really Happens
A function definition describes what a function can do, but nothing happens until the program performs a C function call. In this lecture of our free C programming course you will learn how to write a call, how actual arguments reach formal arguments, how control jumps out and comes back, and why changing a parameter never changes the caller’s variable. These ideas are essential for embedded C programming, where one wrong assumption about a call can corrupt a hardware register value or overflow a tiny stack.
What You Will Learn
- How to write a function call and use its return value
- The difference between actual arguments and formal arguments
- The roles of the calling function and the called function
- How control flows into a function and back to the next statement
- Why C passes arguments by value
- How nested calls use the stack in embedded systems
Prerequisites
Complete Lecture 4 first. You should be able to read a function definition and understand parameters, local variables and return values.
Writing a Function Call
A call consists of the function name followed by parentheses that contain the arguments, separated by commas. The values you write in the call are the actual arguments. Even when a function takes no arguments, the empty parentheses are mandatory; without them you merely mention the function’s name and nothing runs.
The function that contains the call is the calling function, and the function being run is the called function. In the earlier examples main was the caller and helpers such as clamp_value were the callee. Any function can be a caller, including a function that is itself currently being called.
Using the Returned Value
A call that returns a value is an expression, so it may appear on the right side of an assignment, inside another expression, or as an argument to another call. A call can also stand alone as a statement, in which case the returned value is simply discarded, which is acceptable for functions like printf but risky for functions that report errors.
| Call style | Example | Effect |
|---|---|---|
| Assign result | level = read_level(); | Result stored in a variable |
| Inside expression | total = read_level() * 2; | Result used immediately |
| As an argument | show(read_level()); | Result passed to another function |
| Statement only | led_toggle(); | No value needed |
Actual Arguments Versus Formal Arguments
The values in the call are actual arguments; the names in the definition are formal arguments. At call time the first actual argument initializes the first formal argument, the second initializes the second, and so on. The count and the types should match the prototype; if they do not, the compiler reports an error or converts types automatically, which may change values.
How Control Moves During a Call
When the processor reaches a call, it remembers where to come back to, jumps to the first instruction of the called function, runs its body, and then resumes at the statement right after the call. If the program calls two helper functions one after another, control makes two separate round trips.
Example: Tracing the Order of Execution
#include <stdio.h>
void init_stage(void)
{
printf(" [init] running\n");
}
int scale_stage(int input, int factor)
{
printf(" [scale] %d x %d\n", input, factor);
return input * factor;
}
int main(void)
{
printf("main: start\n");
init_stage();
printf("main: back from init\n");
int out = scale_stage(7, 6);
printf("main: back from scale, result=%d\n", out);
return 0;
}
gcc -Wall -o ep_trace ep_trace.c
./ep_trace
main: start
[init] running
main: back from init
[scale] 7 x 6
main: back from scale, result=42
The indented lines prove that the helpers run between the lines printed by main. Each call completes fully before the next statement in the caller executes.
Call by Value: The Caller’s Variable Stays Safe
C passes arguments by value. The function receives a copy, so modifying a parameter changes only the copy. The following program proves it.
#include <stdio.h>
void try_to_change(int x)
{
x = x + 100;
printf("inside : %d\n", x);
}
int main(void)
{
int level = 5;
try_to_change(level);
printf("outside: %d\n", level);
return 0;
}
inside : 105
outside: 5
The value of level in main never changed. If a function must modify the caller’s variable, you pass its address using a pointer, a topic coming in a later chapter. Hardware register access in firmware relies on exactly that technique.
Nested Calls and the Stack
A called function can call another function, and so on. Each active call keeps a frame on the stack holding its return address, parameters and local variables. When a function returns, its frame is released. The deeper the chain of calls, the more stack the program uses at its peak.
Type Conversion During a Call
When a prototype is in scope, the compiler converts each actual argument to the type of the matching formal argument. Passing the integer 3 to a double parameter quietly becomes 3.0, which is usually harmless. The opposite direction is dangerous: passing 3.9 to an int parameter truncates it to 3, and passing a large int to a char parameter keeps only the low byte. Compilers with -Wall -Wextra -Wconversion flag many of these cases, so turn those warnings on while learning.
Calling Functions from Other Functions
Real programs rarely call everything from main. A top-level function such as run_cycle may call read_sensor, then filter_value, then update_display. Each of those may call further helpers. This layered approach is how large firmware stays understandable: every level hides details from the level above it.
#include <stdio.h>
static int read_sensor(void) { return 812; }
static int filter_value(int raw) { return raw / 4; }
static void show(int v) { printf("level = %d\n", v); }
static void run_cycle(void)
{
int raw = read_sensor();
int clean = filter_value(raw);
show(clean);
}
int main(void)
{
run_cycle();
return 0;
}
level = 203
Here main knows only about run_cycle. If the sensor changes, only read_sensor needs editing. That separation is exactly what the advantages listed in Lecture 1 promised.
Common Mistakes with a C Function Call
- Omitting parentheses. Writing
led_toggle;does nothing useful. - Wrong number of arguments. The compiler flags it when a prototype is visible.
- Expecting the caller’s variable to change. Pass-by-value copies the data.
- Ignoring error codes. Silent failures cause hard-to-find bugs.
- Order of evaluation assumptions. C does not guarantee the order in which arguments are evaluated, so avoid side effects like
f(i++, i++).
Best Practices, Performance and Security
Do this
- Check return values from functions that can fail.
- Pass small values by value and large structures by pointer to avoid slow copies.
- Keep call chains shallow in interrupt context.
- Validate every argument that comes from outside the program, such as UART data.
Summary and Key Takeaways
- A call is the function name plus parentheses with actual arguments.
- Actual arguments are copied into formal arguments.
- Control returns to the statement after the call when the function finishes.
- C is call by value, so the callee cannot change the caller’s variables directly.
- Nested calls consume stack, a precious resource in embedded C programming.
You have now covered the complete function story for this chapter section. The final lecture gives you a quiz and interview questions to test everything.
Frequently Asked Questions
What is the difference between calling and defining a function?
Defining writes the body once; calling runs it, as many times as you like.
Why are parentheses needed even with no arguments?
They are what make the expression a call instead of just a reference to the function name.
Does C support call by reference?
Not directly. You simulate it by passing a pointer by value.
Can I ignore a function’s return value?
Yes, but do so only when failure does not matter.
What is the calling function?
The function that contains the call statement.
How deep can calls be nested?
Until the stack is exhausted, which on small MCUs can happen after only a few levels.
Is this free embedded C course enough for interviews?
It builds the fundamentals. Practice the quiz and interview questions in the next lecture.
Test Your Embedded C Skills
The next lecture is a quiz and interview question set that checks everything from this chapter section.

2 Comments