A function communicates its result to the caller through the return statement. This section covers how return works, what a function’s return type means, and what happens when there are several return statements or none.
What return does
Syntax
return expression; /* in a function that returns a value */return; /* in a void function */
The return statement does two things:
It ends the function immediately; any statements after it are skipped.
It sends the value of the expression back to the caller, converted to the function’s return type.
The parentheses in return (x); are optional; return x; means the same.
Return types
Return type
Meaning
Example
int, float, char, …
The function returns one value of that type
float average(int a, int b)
void
The function returns nothing
void print_line(void)
A function can return only one value. (Returning several results needs pointers, arrays or structures, covered in later units.) The value of a call can be used anywhere a value of that type is allowed: x = max(a, b);, printf("%d", max(a, b));, if (max(a, b) > 10).
Several return statements
A function may contain more than one return, but only one of them executes in any single call.
Returning a list of values
return (2, 3); does not return two values. The comma here is the comma operator from Unit 2: the left operand is evaluated and discarded, and the value of the right operand, 3, is returned.
Functions that return nothing, and missing returns
A void function ends when it reaches its closing brace or a plain return;. In a function with a non-void return type, every path must end in a return with a value; if the caller uses the result of a call that ended without one, the behaviour is undefined.
Key takeaways
return ends the function and passes one value back to the caller.
The value is converted to the declared return type; void means no value.
Multiple return statements are allowed; one executes per call.
Every function needs an explicit return type; there is no default int in modern C.
Practice questions
Answer on paper first, then check. The explanation matters more than the letter you chose.
Question 6 · short answer
Write a function is_even that returns 1 if its integer argument is even and 0 otherwise, and trace it for 10.
Show worked answer
int is_even(int n) { if (n % 2 == 0) return 1; return 0; } (or simply return n % 2 == 0;). Trace for 10: 10 % 2 is 0, the condition is true, so return 1; executes and the function ends; the final return 0; is not reached. The call is_even(10) has the value 1.
MCA Online notes are made for reading on screen. Open https://mcastudy.online/mcs-201/functions/the-return-statement/ to read this page.