🚀 Learn BLE on Linux (BlueZ) – Programming Part 1
Hands-on Bluetooth Low Energy course. Special price: ₹399
Enroll Now on Udemy →Common Function Errors Explained
Chapter 6, Functions: seven mistakes that break C programs, each shown with the real GCC diagnostic, the reason behind it and a corrected version. Part of the free C programming course for embedded learners.
Reading compiler messages is a skill, and the fastest way to learn it is to break programs on purpose. This lecture of the free C programming course walks through the common function errors in C that exam papers love: jumping with goto between functions, using the result of a void function, forgetting return, calling a function with the wrong number of arguments, redeclaring a parameter, putting return inside a conditional expression and nesting one function inside another. For each of these common function errors in C we show the exact message GCC prints, explain the rule behind it, and fix it.
The messages were produced with GCC 13.3 using -std=c11 -Wall -Wextra. Other compilers, including Clang, Keil and IAR, word things differently, but they flag the same underlying problems.
What You Will Learn
- Which common function errors in C are hard compile errors and which only produce warnings.
- How to read the file, line and column that a diagnostic gives you.
- Why a missing
returnvalue or a missing prototype is dangerous even when the program builds. - The fix for each mistake, with a compiled and tested corrected program.
- Which warnings every embedded C programming project should turn on.
Prerequisites
You need the basics of function definition, declaration, parameters and the return statement from the first lectures of this chapter. Use any C compiler with a command line. The commands shown here are plain GCC invocations.
Where each mistake is caught
Hard compile errors
goto across functions, void value used, redeclared parameter, return inside a conditional, wrong argument count against a prototype.
Warnings that hide bugs
Missing return value and calling a function that has no declaration. The program builds but may misbehave.
Pedantic only
Nested functions work as a GCC extension, but standard C forbids them, so portable code must avoid them.
Common Function Errors in C: Jumping Between Functions with goto
A label belongs to exactly one function. A goto can leap anywhere inside the function that contains the label, but never out of the function. Here the label sits in main and the jump sits in a different function.
/* does not compile: a label belongs to one function only */
#include <stdio.h>
void abort_path(void)
{
goto finish;
}
int main(void)
{
abort_path();
finish:
puts("done");
return 0;
}
e1_goto.c:6:5: error: label ‘finish’ used but not defined
e1_goto.c:12:1: warning: label ‘finish’ defined but not used [-Wunused-label]
The first line is the real error: the function abort_path uses a label that it cannot see. The second is a follow-on warning that the label in main is never used. Fix the design instead of the syntax. Let the helper report a status and let the caller decide what to do.
#include <stdio.h>
int check_sensor(int reading)
{
if (reading < 0)
return -1; /* report failure to the caller */
return 0;
}
int main(void)
{
if (check_sensor(-5) != 0) {
puts("sensor fault handled in main");
return 1;
}
puts("all good");
return 0;
}
sensor fault handled in main
The only way to leave one function and resume in another is the setjmp and longjmp pair, an advanced and risky tool that most firmware avoids.
Common Function Errors in C: Using a void Result
A function declared void returns nothing, so there is nothing to put into a variable or an expression.
/* does not compile: a void function has no value to store */
#include <stdio.h>
void ep_log(int v)
{
printf("v=%d\n", v);
}
int main(void)
{
int r = ep_log(4);
printf("%d\n", r);
return 0;
}
e2_void.c:11:13: error: void value not ignored as it ought to be
The wording “void value not ignored as it ought to be” means you tried to use a value that does not exist. Either call the function as a plain statement, or give it a real return type. The corrected program returns the character count that printf itself reports.
#include <stdio.h>
int ep_log(int v)
{
return printf("v=%d\n", v); /* printf returns characters written */
}
int main(void)
{
int r = ep_log(4);
printf("%d\n", r);
return 0;
}
v=4
4
Missing return: A Common Function Error in C That Compiles
This one is dangerous because the program builds. When x is positive the function returns 1, but when it is not, execution reaches the closing brace with no value.
/* compiles with a warning, result is undefined behaviour */
#include <stdio.h>
int pick(int x)
{
if (x > 0)
return 1;
}
int main(void)
{
printf("%d\n", pick(-3));
return 0;
}
e3_noret.c:8:1: warning: control reaches end of non-void function [-Wreturn-type]
Using the result of such a call is undefined behaviour. In practice the caller reads whatever value happens to be in the return register, which could be a leftover from earlier work. It may look correct on one build and fail on the next. The fix is to make every path return something.
#include <stdio.h>
int pick(int x)
{
if (x > 0)
return 1;
return 0; /* every path now returns a value */
}
int main(void)
{
printf("%d\n", pick(-3));
return 0;
}
0
-Werror=return-type to your build flags.
Missing Prototypes: Common Function Errors in C
When the compiler meets a call before it has seen any declaration, old C rules let it guess. The guess is “returns int, takes whatever arguments you pass”. If the real definition later disagrees, you have a bug the compiler may not detect.
/* undefined behaviour: called before any declaration, wrong argument count */
#include <stdio.h>
int main(void)
{
printf("%d\n", volume(2, 3));
return 0;
}
int volume(int a, int b, int c)
{
return a * b * c;
}
e4_implicit.c:6:20: warning: implicit declaration of function ‘volume’ [-Wimplicit-function-declaration]
The function expects three arguments, but the call supplies two. The third is missing, so the function multiplies by whatever garbage sits where the third argument should be. Recent GCC releases treat the implicit declaration as an error, but older compilers and many embedded toolchains still only warn. A prototype turns the silent bug into a loud one.
/* does not compile: the prototype demands three arguments */
#include <stdio.h>
int volume(int, int, int);
int main(void)
{
printf("%d\n", volume(2, 3));
return 0;
}
int volume(int a, int b, int c)
{
return a * b * c;
}
e4b_few.c:8:20: error: too few arguments to function ‘volume’
With the prototype in place the error is reported at compile time. Passing too many arguments is caught the same way.
/* does not compile: one argument too many for the prototype */
#include <stdio.h>
int volume(int, int, int);
int main(void)
{
printf("%d\n", volume(2, 3, 4, 5));
return 0;
}
int volume(int a, int b, int c)
{
return a * b * c;
}
e4c_many.c:8:20: error: too many arguments to function ‘volume’
The cure is to declare each function before its first use, normally in a header file, and to pass exactly the arguments it expects.
#include <stdio.h>
int volume(int a, int b, int c); /* prototype before first use */
int main(void)
{
printf("%d\n", volume(2, 3, 4));
return 0;
}
int volume(int a, int b, int c)
{
return a * b * c;
}
24
Redeclaring a Parameter Inside the Function
Parameters live in the same scope as the function’s outermost local variables, so a local cannot reuse a parameter’s name.
/* does not compile: the parameter x is already a local variable */
int twice(int x)
{
int x = 2;
return x * 2;
}
int main(void)
{
return twice(1);
}
e5_redecl.c:4:9: error: ‘x’ redeclared as different kind of symbol
e5_redecl.c:2:15: warning: unused parameter ‘x’ [-Wunused-parameter]
GCC reports that x is “redeclared as different kind of symbol”. Other compilers say “multiple declaration for x”. Both mean the same. The warning that the parameter is unused appears because the redeclaration hid it. Use a new name.
#include <stdio.h>
int twice(int x)
{
int scale = 2; /* a new name, no clash with the parameter */
return x * scale;
}
int main(void)
{
printf("%d\n", twice(21));
return 0;
}
42
Putting return Inside a Conditional Expression
The conditional operator ? : chooses between two expressions. A return is a statement, so it cannot sit in an operand.
/* does not compile: return is a statement, not an expression */
int bigger(int a, int b)
{
a > b ? return a : return b;
}
int main(void)
{
return bigger(1, 2);
}
e6_ternary.c:4:13: error: expected expression before ‘return’
e6_ternary.c:5:1: warning: control reaches end of non-void function [-Wreturn-type]
Return the whole conditional instead. The operator picks the value; the single return sends it back.
#include <stdio.h>
int bigger(int a, int b)
{
return a > b ? a : b; /* the conditional yields the value */
}
int main(void)
{
printf("%d\n", bigger(4, 9));
return 0;
}
9
Defining One Function Inside Another
Standard C does not allow nested function definitions. GCC accepts them as an extension unless you ask for strict standard behaviour, which is why this example uses -pedantic-errors.
/* does not compile in standard C: functions cannot be nested */
#include <stdio.h>
int main(void)
{
int outer(void)
{
return 7;
}
printf("%d\n", outer());
return 0;
}
e7_nested.c:6:5: error: ISO C forbids nested functions [-Wpedantic]
Even when a compiler tolerates nesting, the code is not portable and will fail on Clang and on most embedded toolchains. Move the helper to file scope and mark it static if only this file should see it.
#include <stdio.h>
static int outer(void) /* defined at file scope */
{
return 7;
}
int main(void)
{
printf("%d\n", outer());
return 0;
}
7
Real-World Use Cases
- Build-flag policy: teams add
-Wall -Wextra -Werror=return-type -Werror=implicit-function-declarationso these mistakes never reach a release. - Header discipline: every driver exposes its prototypes in a header, which makes argument-count errors compile-time failures.
- Static analysis: tools such as cppcheck or the compiler’s own analyser catch the same classes of mistake in code you inherit.
Common Function Errors in C: Quick Reference
| Message fragment | Cause | Fix |
|---|---|---|
| label used but not defined | goto into another function | Return a status, jump only within one function |
| void value not ignored | Using a void result | Change the return type or call it as a statement |
| control reaches end of non-void function | A path without return | Return a value on every path |
| implicit declaration of function | Call before declaration | Add a prototype or include the header |
| too few or too many arguments | Call disagrees with the prototype | Match the parameter list |
| redeclared as different kind of symbol | Local reuses a parameter name | Rename the local |
| expected expression before return | return inside ? : | Write return condition ? a : b |
| ISO C forbids nested functions | Function inside a function | Move it to file scope |
Best Practices, Performance and Security
Best practices
- Fix the first error in the list first; later messages are often side effects.
- Keep one prototype per function in a header and include it in both the caller and the definition file.
- Make warnings fatal for return-type and implicit-declaration problems.
- Prefer a status return value over any attempt to jump out of a function.
Performance: none of these fixes cost anything at run time; prototypes and return values are free. Security: a call with missing arguments reads stale register or stack contents, which can leak data or let attacker-influenced values drive logic. Missing return values behave the same way. Strict warnings are the cheapest security control available.
Key Takeaways and Conclusion
- Labels are private to a function, so goto cannot cross function boundaries.
- A void function has no value to use.
- Every non-void function must return on every path.
- Declare before use, and match the parameter list exactly.
- Do not redeclare parameters, nest functions or put return inside an expression.
These common function errors in C are the backbone of many output-prediction and error-spotting questions, and they also appear in real code reviews. If you can read the diagnostic, name the rule and apply the fix, you are already thinking like a firmware engineer. In the next lectures we put these ideas to work on programming exercises.
Frequently Asked Questions
Why can’t I use goto to jump to a label in another function?
Labels have function scope, so the compiler cannot find the label from outside the function that defines it.
What happens if a C function has no return statement on some path?
The program compiles with a warning, and using the result is undefined behaviour. The caller reads an unpredictable value.
Is implicit function declaration still allowed in C?
It was removed from the language in C99, and recent compilers make it an error. Some older or embedded toolchains still accept it with a warning.
Why does C complain when I redeclare a parameter?
Parameters and the outermost locals share one scope, so the name would refer to two different objects.
Can I nest functions in C?
Not in standard C. GCC offers it as an extension, but portable embedded C programming code should define every function at file scope.
How do I make the compiler catch these common function errors in C early?
Compile with -Wall -Wextra and turn the important warnings into errors, such as -Werror=return-type.
Practice What You Just Learned
The next lectures turn these rules into complete exercise solutions: Armstrong numbers, persistence, perfect numbers and more. Everything stays free in our embedded C course.

2 Comments