🚀 Learn BLE on Linux (BlueZ) – Programming Part 1
Hands-on Bluetooth Low Energy course. Special price: ₹399
Enroll Now on Udemy →Calling Functions Without Declarations
Lecture 17 of our free C programming course: what happens with extra, missing or mismatched arguments, and how to stay safe in embedded C programming.
Welcome back to our free C programming course. So far we have learned that a prototype describes a function to the compiler. This lecture asks the opposite question: what if the prototype is missing, or the call does not match it? The answers explain many strange bugs seen in legacy code and show why embedded C programming teams enforce strict warnings. This free C tutorial also demonstrates how automatic type conversion behaves when a prototype is present.
What You Will Learn
- What classic compilers did when a declaration was absent
- Extra actual arguments versus missing actual arguments
- How type mismatches are converted when a prototype exists
- Why these situations are undefined behavior in the language rules
- How to make the compiler stop you with the right flags
- A debugging checklist for argument related bugs
Prerequisites
You should understand actual and formal arguments, prototypes, and the way void appears in parameter lists. If you need a refresher, read lectures 14 to 16 of this free embedded C course.
Setting the Scene
Imagine a function defined to accept three integers, and a caller that has never seen its prototype. In very early C, the compiler would simply compile the call as written, passing whatever arguments appear in the call. It had nothing to compare them to. The function then reads its three parameters from wherever the calling convention says they live, regardless of how many values the caller supplied. Two things can go wrong, and old textbooks describe both.
Case A: More Actual Arguments Than Formal
When the caller supplies extra arguments, the surplus values are never read by the function. On many older machines the result looked harmless: the first values matched the parameters in order, and the rest were just ignored. This seems comforting, but it is a trap. A supposedly harmless extra argument usually means the caller and the function disagree about the design, so the real intent of the code is lost.
Consider a function that should receive a lower bound, an upper bound and a value. If the caller passes four numbers because the function once had an extra parameter, the arguments after the third are dropped without any message, and nobody notices the stale call.
Case B: Fewer Actual Arguments Than Formal
This case is worse. The function still reads all of its parameters, but the caller provided fewer values, so the missing parameter picks up whatever bytes were sitting in that register or stack slot. That leftover is effectively random, which is why books call it a garbage value. The program may seem to work during testing, because stale data can happen to look reasonable, and then fail in the field.
Important: According to the language rules, calling a function with the wrong number of arguments is undefined behavior. Garbage values are only one possible symptom. Crashes, silent corruption or apparently correct output are all allowed, and they can change after a compiler upgrade.
Case C: Type Mismatch
Now suppose the number of arguments is right but a type is wrong. What happens depends on whether the compiler has seen a prototype.
| Situation | Result |
|---|---|
| Prototype visible, conversion is legal | Compiler converts the value to the parameter type |
| Prototype visible, conversion not possible | Compile time error |
| No prototype | Only basic promotions apply, so the callee may read wrong bits |
With a prototype in scope, mismatch becomes a managed event rather than a gamble. Look at a program that passes a fractional value to an integer parameter and an integer to a floating parameter.
#include <stdio.h>
void show_int(int n);
void show_float(float f);
int main(void)
{
show_int(7.9); /* double converted to int, fraction dropped */
show_float(3); /* int converted to float */
return 0;
}
void show_int(int n)
{
printf("int parameter holds %d\n", n);
}
void show_float(float f)
{
printf("float parameter holds %.2f\n", f);
}
int parameter holds 7
float parameter holds 3.00
The compiler converted both values silently and correctly because the prototypes told it what each parameter expects. Notice that 7.9 became 7: the fraction was truncated, not rounded. Conversions can lose information, and warnings such as -Wconversion help you notice them.
Converting Between Sizes in Embedded Code
Silent narrowing is a common source of hardware bugs. For example, passing a 32 bit value to an 8 bit parameter discards the upper bits.
#include <stdio.h>
#include <stdint.h>
void set_pwm(uint8_t duty);
int main(void)
{
uint32_t request = 300;
set_pwm((uint8_t)request); /* explicit cast shows intent */
return 0;
}
void set_pwm(uint8_t duty)
{
printf("duty register gets %u\n", (unsigned)duty);
}
duty register gets 44
The value 300 does not fit in eight bits. Only the low byte remains, which is 44. A cast documents that you know about the narrowing, but the better fix is to clamp the value before the call. This is a standard lesson in C programming for embedded systems.
Why Modern Compilers Refuse Missing Declarations
Because the old behavior caused so many hard bugs, the C99 standard removed implicit function declarations. Current gcc and clang releases warn by default, and the newest versions treat a call to an undeclared function as an error. You can enforce the same strictness on any toolchain with a few flags.
gcc -std=c11 -Wall -Wextra -Werror=implicit-function-declaration \
-Wstrict-prototypes -Wconversion -c main.c
With these flags, the three dangerous cases all become build time messages. A wrong argument count is a hard error whenever the prototype is visible. A missing prototype is flagged. A narrowing conversion is reported so you can decide whether to clamp or cast.
Real World Use Case: A Refactoring Accident
A team has a function send_frame(id, length). Later a developer adds a third parameter for a timeout and updates the definition, but forgets one old call site in a rarely used file. If the header with the prototype is not included in that file, the old call compiles, the timeout parameter receives leftover data, and the device sometimes waits for a random time. Including the header and compiling with strict flags would have flagged the call immediately. This kind of failure is exactly why we repeat the habit of prototypes throughout this free embedded C course.
Debugging Checklist
- Confirm that every function has a prototype visible at the call site
- Count the arguments in the call and in the definition
- Compare types one by one and look for narrowing conversions
- Compile with the strict flags shown above and read every warning
- Check that the header was actually included in the file that calls the function
Common Mistakes
Trusting a program because it works: Code relying on leftover values may pass tests and still be wrong.
Ignoring compiler warnings: The warning usually names the exact line of the mismatch.
Assuming float and double are interchangeable: Without a prototype, a float argument is promoted to double, which may not match what the function expects.
Forgetting header changes: After editing a prototype, rebuild every file that includes it.
Best Practices
- Keep one prototype per function in a header
- Include that header in the defining file so the compiler checks the definition too
- Enable warnings for conversions and implicit declarations
- Clamp values before narrowing instead of relying on truncation
- Run static analysis on firmware as a second safety net
Summary and Key Takeaways
- Without a prototype the compiler cannot check argument count or types
- Extra arguments are ignored and missing arguments hold garbage, and both are undefined behavior by the standard
- With a prototype, compatible types are converted and incompatible calls are rejected
- Conversion can lose information, such as dropping a fraction or high bits
- Strict compiler flags turn silent bugs into visible messages
You now understand the risks around absent declarations. In the next part of our free C programming course we look at one more surprising rule: the order in which function arguments are evaluated.
Frequently Asked Questions
What happens if I call a function with too few arguments in C?
The behavior is undefined. In practice the missing parameter often holds leftover data, so results look random.
What happens if I pass too many arguments?
With a prototype the compiler rejects it. Without one, extra values are usually ignored, but the behavior is still undefined.
Does C convert argument types automatically?
Yes, when a prototype is visible the compiler converts each argument to the parameter type if a legal conversion exists.
Is calling an undeclared function allowed today?
No. Modern standards removed it, and recent compilers warn or give errors.
How do I catch narrowing conversions?
Compile with -Wconversion and review each warning.
Which course covers embedded C programming for beginners for free?
This free C tutorial series on EmbeddedPathashala walks from basics to firmware style code step by step.
Master Embedded C Step by Step
Continue with the next lecture of our free C programming course.

2 Comments