Return Values
The return statement is how a function sends a result back to whoever called it. It also immediately ends the function — no statement after return in the same call will execute.
The return Statement
Returning a value
int square(int n) {
return n * n; // computes n * n and sends it back, then exits
}The type of the expression after return should match the function's declared return type (or be convertible to it). The value produced by the call expression, e.g. square(4), is the value passed to return.
A Function Can Only Return One Value
Unlike some languages that let you write return a, b; to hand back multiple values at once, C's return statement carries exactly one value.
void Functions
A function declared to return void produces no value at all. It can still use a bare return; to exit early, or it can simply let control reach the closing } of its body, which ends the function just as effectively.
Early return in a void function
#include <stdio.h>
void printPositive(int n) {
if (n <= 0) {
return; // bare return: exit early, nothing to print
}
printf("%d is positive\n", n);
// falling off the end here also returns, implicitly
}Guard Clauses: Returning Early
A very common and readable pattern is to check for invalid or trivial input at the top of a function and return immediately, rather than nesting the "real" logic inside a big if block.
Guard clause style
#include <stdio.h>
double safeDivide(double numerator, double denominator, int *ok) {
if (denominator == 0.0) {
*ok = 0; // signal failure through an output parameter
return 0.0; // return early; skip the real computation
}
*ok = 1;
return numerator / denominator;
}
int main(void) {
int success;
double result = safeDivide(10.0, 0.0, &success);
if (!success) {
printf("Cannot divide by zero\n");
} else {
printf("Result: %.2f\n", result);
}
return 0;
}Every Path Must Return (for non-void functions)
If a function's return type is not void, every possible path through the function should end with a return that produces a value. Forgetting one is a mistake compilers usually warn about, but C will still let you compile it.
A function that forgets to return on one path
int sign(int n) {
if (n > 0) {
return 1;
} else if (n < 0) {
return -1;
}
// BUG: no return here for the n == 0 case!
}return expr;— exits the function immediately and yieldsexprto the caller.return;— valid only invoidfunctions; exits with no value.Falling off the closing
}of avoidfunction is equivalent to a barereturn;.Falling off the closing
}of a non-void function is undefined behavior if the result is used.