🚀 Learn BLE on Linux (BlueZ) – Programming Part 1
Hands-on Bluetooth Low Energy course. Special price: ₹399
Enroll Now on Udemy →Formal and Actual Arguments
Understand actual and formal arguments in C, how values are copied into a function, and why changing a parameter never touches the caller’s variable. A lecture from our free C tutorial for embedded programmers.
Functions become powerful only when they can receive data, and actual and formal arguments in C are the two halves of that exchange. In this lecture of our free C programming course you will learn what each half is, how a call connects them, and what the compiler checks when you get the count or type wrong. These ideas sit at the heart of embedded C programming, because every driver call such as setting a timer period or writing a register value passes data this way.
If you are working through this free embedded C course in order, you already know how to return a value from a function. Now we study the opposite direction: how information travels into the function when it is called.
What You Will Learn
- The difference between actual and formal arguments in C, and which one lives in the call and which in the definition
- Which kinds of expressions are allowed as actual arguments
- How formal arguments differ from ordinary local variables
- Why C copies argument values, and what that means for the caller’s variables
- What the compiler does when the number or type of arguments does not match
- Why argument names in the call and in the definition can be identical or different
Prerequisites
- Basic function syntax: definition, call and prototype
- Understanding of local variables and the life of a block
- The ability to compile and run small programs with gcc
Actual and Formal Arguments in C: Two Sides of a Call
Think of a function call as a small delivery. The caller packs some values, and the function unpacks them into named boxes. The packed values are the actual arguments, sometimes called actual parameters, because they are the real data sent at that moment. The named boxes in the function header are the formal arguments, also called formal or dummy parameters, because they exist only to hold whatever arrives. One call site supplies actual arguments; one function definition declares formal arguments.
What counts as an actual argument
An actual argument is any expression that produces a value. That is a generous rule, as the table shows.
| Form | Example call | Meaning |
|---|---|---|
| Variable | apply_gain(adc, gain) | the current contents are sent |
| Constant | apply_gain(25, 3) | a fixed literal is sent |
| Expression | apply_gain(adc + 2, gain - 1) | the expression is evaluated first |
| Function call | apply_gain(5, offset_sum(adc, gain)) | the inner call runs first and its return value is sent |
Worked Example: Actual and Formal Arguments in C
Here is an original program that exercises every form. The function scales a sample by a gain, which is a very common step when you convert raw ADC counts into meaningful units.
#include <stdio.h>
static int offset_sum(int a, int b)
{
return a + b;
}
static int apply_gain(int sample, int gain)
{
int result;
result = sample * gain;
return result;
}
int main(void)
{
int adc = 12;
int gain = 4;
printf("%d\n", apply_gain(adc, gain));
printf("%d\n", apply_gain(25, 3));
printf("%d\n", apply_gain(adc + 2, gain - 1));
printf("%d\n", apply_gain(5, offset_sum(adc, gain)));
return 0;
}
48
75
42
80
Trace each call and the formal arguments sample and gain are initialised from the actual arguments, in order:
| Call | sample receives | gain receives | result returned |
|---|---|---|---|
| 1st: variables | 12 | 4 | 48 |
| 2nd: constants | 25 | 3 | 75 |
| 3rd: expressions | 14 | 3 | 42 |
| 4th: constant and nested call | 5 | 16 | 80 |
In the fourth call the second actual argument is itself a call. The function offset_sum runs first, returns 16, and only then does apply_gain begin with gain set to 16.
How Formal Arguments Differ From Ordinary Locals
A formal argument behaves like a local variable. It is created when the call starts and disappears when the function returns, so its value cannot be seen from other functions. Two details separate it from the variables you declare in the body.
| Property | Formal argument | Local variable in the body |
|---|---|---|
| Where it is declared | inside the parentheses of the header | at the start of the function block |
| How it gets its first value | automatically, from the matching actual argument | only through statements you write, otherwise indeterminate |
| Lifetime | from call start to function end | from block entry to function end |
In the example above, result is an ordinary local that we assign ourselves, while sample and gain arrive pre-filled. This is also why an uninitialised local is dangerous but a formal argument is not.
Pass by Value: Actual and Formal Arguments in C
C passes arguments by value. The function receives copies, so modifying a formal argument changes the copy and nothing else. The next program proves it.
#include <stdio.h>
static void bump(int level)
{
level = level + 100;
printf("inside bump : level = %d\n", level);
}
static void show_pair(int a, int b)
{
printf("a = %d b = %d\n", a, b);
}
int main(void)
{
int level = 7;
int a = 6, b = 3;
bump(level);
printf("back in main: level = %d\n", level);
show_pair(a, b);
show_pair(15, 4);
show_pair(a + b, a - b);
return 0;
}
inside bump : level = 107
back in main: level = 7
a = 6 b = 3
a = 15 b = 4
a = 9 b = 3
Same names or different names
Notice that main and show_pair both use the names a and b, and also that bump reuses the name level. This is legal because each function has its own scope. Matching names do not link the variables in any way; the only connection is the position of the argument in the list. Many teams still choose different names, since it makes it obvious which side of the call you are reading.
Matching Actual and Formal Arguments in C
The actual arguments must line up with the formal arguments in three ways: the same count, the same order and compatible types. With a prototype in scope, the compiler checks all three. Try giving three values to a function that wants two:
error: too many arguments to function 'apply_gain'
10 | printf("%d\n", apply_gain(1, 2, 3));
Type mismatches behave differently. When the types are compatible, the compiler converts the value to the type of the formal argument. For example, apply_gain(3.9, 2) sends 3.9 to an int parameter, so sample becomes 3 and the call returns 6. No error appears, which is exactly why you should review conversions yourself.
| Mismatch | Result with a prototype |
|---|---|
| Too many or too few arguments | compile error |
| Wrong order of two ints | compiles, but the logic is wrong |
| double sent to int parameter | fraction discarded silently |
| No prototype visible | risky behaviour; always declare functions first |
Real-World Uses in C Programming for Embedded Systems
Actual and formal arguments in C appear in almost every hardware helper: a delay routine takes a count, a PWM helper takes a duty percentage, a UART routine takes a byte. Because of pass by value, you can freely adjust the parameter inside such helpers, for example to count a delay down to zero, without worrying that the caller’s variable changes. That is a safe, useful property of the language.
Common Mistakes and Troubleshooting
| Mistake | Symptom | Fix |
|---|---|---|
| Expecting the function to change the caller’s variable | the variable keeps its old value | return the new value and assign it, or use pointers later |
| Swapping the order of two actual arguments | valid code, wrong numbers | read the prototype and use descriptive names |
| Passing a double to an int parameter | silent truncation | convert deliberately and document it |
| Calling without a visible prototype | undefined or odd behaviour | include the header or declare the function above the call |
| Using side effects like i++ twice in one call | order of evaluation is not defined | compute values into variables first |
Best Practices
- Choose parameter names that carry units or meaning, such as
delay_msorduty_percent. - Keep the argument list short; more than four usually signals a design problem.
- Always keep a prototype visible before the first call.
- Never rely on the evaluation order of the actual arguments.
- Treat the incoming copy as yours, but do not overwrite it if later code needs the original input.
Performance: small scalar arguments are usually passed in registers, so the cost is tiny. Security: validate arguments that come from outside the system, such as a length received over a bus, before using them inside the function.
Summary and Key Takeaways
- Actual arguments are the values in the call; formal arguments are the named parameters in the definition.
- An actual argument may be a variable, a constant, an expression or another call.
- Formal arguments act like locals that are initialised automatically from the actual ones.
- C passes by value, so changing a formal argument never affects the caller.
- Count, order and type must match, and the compiler converts compatible types silently.
Conclusion
Understanding actual and formal arguments in C removes a huge amount of confusion. Once you picture the call as copying values into new boxes, behaviour that surprises beginners becomes obvious: why the caller’s variable is unchanged, why names do not need to match, and why a wrongly ordered call still compiles. Practise by writing small helper functions with two or three parameters and tracing the values on paper before running them. In the next lecture we classify functions by whether they take arguments and whether they return a value, a classification that organises everything you have learned so far in this free C programming course.
Frequently Asked Questions
What is the difference between actual and formal arguments?
Actual arguments are the values written in the function call. Formal arguments are the parameter names declared in the function definition that receive those values.
Can the actual and formal argument names be the same?
Yes. Each function has its own scope, so identical names are allowed and do not link the variables.
Does changing a formal argument change the original variable?
No. C passes copies, so only the function’s own parameter changes.
What happens if I pass the wrong number of arguments?
With a prototype in scope the compiler stops with an error such as too many arguments to function.
Can an actual argument be a function call?
Yes. The inner call is executed first and its return value is copied into the formal argument.
How do I let a function modify the caller’s data?
You need pointers, which a later chapter of this course explains. For now, return the new value and assign it.
Continue Your Free Embedded C Course
Next we group functions into four types based on arguments and return values. Move on to the next lecture or jump back to the course index.

2 Comments