🚀 Learn BLE on Linux (BlueZ) – Programming Part 1
Hands-on Bluetooth Low Energy course. Special price: ₹399
Enroll Now on Udemy →Return Statement in C
Master both forms of the return statement in C, early exits, multiple returns, return type conversion and the bugs that make embedded functions hand back garbage. Part of our free embedded C course.
The return statement in C is how a function ends its work and, when needed, hands a result back to its caller. In embedded C programming it also decides whether an error gets noticed or silently lost. This lecture of our free C programming course covers both forms, the kinds of values you may return, how multiple exits behave, and the compiler warnings that save you hours on real hardware. It is written for anyone who wants a practical free C tutorial for C programming for embedded systems.
What You Will Learn
By the end of this lecture
- Use both forms of the return statement in C: with and without a value.
- List what a return can carry: a constant, a variable, an expression or another call.
- Use early returns as guard clauses without breaking the function’s logic.
- Predict what
return counter++;actually gives back. - Explain implicit conversion of the returned value to the declared return type.
- Spot a missing return and a returned local address using compiler warnings.
Prerequisites
Complete the earlier lectures on the C function call and the function prototype first. You should be able to write a function with parameters, call it, and compile with gcc -Wall -Wextra. No hardware is required; the examples simulate registers and sensors with ordinary integers.
The Two Forms of the Return Statement in C
The keyword return can appear anywhere in a function body and always does the same fundamental thing: control leaves the function immediately and goes back to the caller. It comes in two shapes.
| Form | Syntax | Used in | Effect |
|---|---|---|---|
| Without a value | return; | Functions declared void | Leaves the function early, nothing is sent back |
| With a value | return expression; | Functions with a non-void type | Leaves the function and sends the value to the caller |
Return statement in C without a value
A void function reaches its end when the closing brace is hit, so it does not need a return at all. The bare return; exists for the other case: leaving early when continuing would be pointless or harmful. A register write helper is a typical example, because writing to a register number that does not exist must be refused.
static void write_reg(unsigned int reg, unsigned int val)
{
if (reg > 7) {
printf("reg %u invalid, write skipped\n", reg);
return;
}
printf("REG[%u] <- 0x%02X\n", reg, val);
}
Everything after the return; inside that path is skipped, so the invalid write never happens. This pattern, checking a precondition and leaving at once, is called a guard clause.
Return statement in C with a value
The second form sends a value back. The caller sees that value as the result of the call expression, exactly as described in the lecture on function calls. The thing you return can be any expression that produces a compatible value, and the parentheses around it are optional.
| Kind of value | Example |
|---|---|
| Constant | return 0; |
| Variable | return count; |
| Arithmetic expression | return (lo + hi) / 2; |
| Conditional expression | return ready ? 1 : 0; |
| Another function call | return checksum8(buf, len); |
| Comparison result | return (reg & (1u << bit)) != 0; |
Multiple Return Statements and Early Exit
A function may contain as many return statements as it needs. The first one that executes ends the function, and nothing below it runs on that call. This is not a trick; it is how most real functions are written, because it lets you handle special cases first and keep the main path flat and readable.
static int classify_battery(int mv)
{
if (mv < 3000)
return 0;
if (mv < 3600)
return 1;
if (mv < 4100)
return 2;
return 3;
}
Returning from inside a loop
Another everyday use of the return statement in C is leaving a search loop the moment the answer is found. The function below reports the index of the first negative sample, or -1 when there is none. Using -1 as a “not found” marker works because a valid index can never be negative.
static int find_first_negative(const int *samples, int count)
{
for (int i = 0; i < count; i++) {
if (samples[i] < 0)
return i;
}
return -1;
}
One exit or many?
Some coding standards prefer a single exit at the end of each function. MISRA C:2012, for instance, lists that as an advisory rule. Other teams value guard clauses because they reduce nesting. Follow your project’s standard; if you have a free choice, use early returns for error cases and keep the happy path at the left margin.
What Does return counter++ Give Back?
The returned expression is evaluated first, then handed back. With the post-increment operator, the value of the expression is the old value, while the variable itself changes afterwards. A ticket dispenser makes this clear:
static int next_ticket(int *counter)
{
return (*counter)++;
}
Starting with the counter at 100, the call returns 100 and leaves the counter at 101. If you want the new value, use ++*counter instead. Be careful with a by-value variable: in return v++; the increment applies to the local copy that disappears on return, so it has no lasting effect.
Complete Return Statement in C Demo
Combine everything into one file named ep_return_demo.c.
#include <stdio.h>
static void write_reg(unsigned int reg, unsigned int val)
{
if (reg > 7) {
printf("reg %u invalid, write skipped\n", reg);
return;
}
printf("REG[%u] <- 0x%02X\n", reg, val);
}
static int classify_battery(int mv)
{
if (mv < 3000)
return 0;
if (mv < 3600)
return 1;
if (mv < 4100)
return 2;
return 3;
}
static int find_first_negative(const int *samples, int count)
{
for (int i = 0; i < count; i++) {
if (samples[i] < 0)
return i;
}
return -1;
}
static int is_bit_set(unsigned int reg, int bit)
{
return (reg & (1u << bit)) != 0;
}
static int next_ticket(int *counter)
{
return (*counter)++;
}
int main(void)
{
int data[] = { 12, 40, -3, 8 };
int ticket = 100;
write_reg(3, 0xA5);
write_reg(9, 0x01);
printf("battery class for 3300 mV = %d\n", classify_battery(3300));
printf("first negative at index %d\n", find_first_negative(data, 4));
printf("bit 2 of 0x14 = %d\n", is_bit_set(0x14, 2));
printf("ticket = %d\n", next_ticket(&ticket));
printf("counter now = %d\n", ticket);
return 0;
}
Build and run the return demo
$ gcc -Wall -Wextra -o ep_return_demo ep_return_demo.c
$ ./ep_return_demo
REG[3] <- 0xA5
reg 9 invalid, write skipped
battery class for 3300 mV = 1
first negative at index 2
bit 2 of 0x14 = 1
ticket = 100
counter now = 101
Each line maps to one idea: the guard clause refuses register 9, three early returns pick battery class 1, the loop returns index 2, the comparison returns 1 because bit 2 of 0x14 is set, and the post-increment returns the old counter value.
Return Type Conversion
The value in a return statement is converted to the function’s declared return type, as if it were assigned to a variable of that type. Converting a float to an int cuts off the fractional part, so a function declared int half(float x) that returns x / 2 gives 3 when called with 7.0, not 3.5. Declare the return type that matches what you want the caller to receive, and never rely on silent conversion for precision.
Common Mistakes with the Return Statement in C
Missing return in a non-void function
If some path reaches the closing brace of an int function without returning, the caller gets an indeterminate value, and using it is undefined behaviour. The compiler warns you:
missing_ret.c:5:1: warning: control reaches end of non-void function [-Wreturn-type]
Returning the address of a local variable
Local variables live in the function’s stack frame, which is gone after the return. A pointer to one dangles immediately.
local_ret.c:4:12: warning: function returns address of local variable [-Wreturn-local-addr]
Return a value, take a pointer to a buffer from the caller, or use a static or global object when the data must outlive the call.
Other pitfalls
- Writing
return;in a function that should return a value. - Returning a value from a
voidfunction, which most compilers reject or warn about. - Mixing error codes and valid results so a legitimate value looks like an error.
- Leaving unreachable code after a return and assuming it runs.
Best Practices, Performance and Security
Best practices
- Document what each return value means, especially error codes.
- Check preconditions first with guard clauses and return early.
- Use distinct, named values such as an enum rather than raw numbers.
- Always compile with
-Wall -Wextraso missing returns are caught.
Performance considerations
Returning a small integer is practically free; on ARM it travels in a register. Returning a large struct by value can copy many bytes, so for big records pass a pointer to caller-owned storage instead. Early returns also save time by skipping unnecessary work.
Security considerations
A function that fails but returns an ambiguous value can leak into later decisions, for example treating a failed length read as a valid zero. Return unambiguous error codes and make callers check them. Never return pointers to stack memory; they invite memory corruption.
Summary and Key Takeaways
Remember these points
- The return statement in C has two forms:
return;for void functions andreturn expression;for others. - The first return reached ends the function; later statements are skipped.
- Parentheses around the returned value are optional.
- The value is converted to the declared return type.
- A missing return or a returned local address is a bug that compilers can warn about.
Conclusion
Return looks simple, but it carries a lot of meaning in firmware: it is both the output of your function and the signal that tells callers whether to continue. You learned both forms, how guard clauses keep code flat, what post-increment does inside a return, why types matter, and which warnings expose the classic mistakes. Next in this free embedded C course, you will test your understanding with a complete quiz and a set of interview questions covering calls, prototypes and returns. If you are building skills for C programming for embedded systems, that practice set is the best way to lock in this chapter.
Frequently Asked Questions
What does the return statement in C do?
It ends the current function immediately and, if given a value, sends that value back to the caller.
Can a void function use return?
Yes, in the form return; with no value, usually to leave the function early.
Can a function have more than one return statement?
Yes. The first one executed ends the function, and the rest are skipped for that call.
Are parentheses required around the returned value?
No. Both return x; and return (x); are valid.
What happens if I forget to return a value?
If control reaches the end of a non-void function and the caller uses the result, the behaviour is undefined. Compilers warn with -Wall.
Why is returning the address of a local variable wrong?
The local variable disappears when the function returns, so the pointer refers to memory that is no longer valid.
What does return x++ return?
The value x had before the increment. The increment is applied afterwards.
Should I use one return or many?
Follow your project’s coding standard. Many teams prefer early returns for errors because they reduce nesting.
Is this lecture part of the free C programming course?
Yes, it is part of the free C programming course on EmbeddedPathashala, aimed at embedded C programming.
Ready to Test Yourself?
The next lecture is a complete quiz with output-prediction questions, coding challenges and interview questions on calls, prototypes and return. Perfect practice for your free embedded C course journey.

2 Comments