C Notes
Learn the return statement in C: returning values from functions, void functions, multiple return points, returning pointers, and common mistakes. Complete guide with practical examples.
The return statement is how a function communicates its result back to the caller. It serves two purposes: it specifies the value to send back, and it immediately terminates the function's execution. Whether you're computing a calculation, checking a condition, or simply signaling completion, the return statement is your function's final word to the outside world.
Basic Syntax
return expression; // Returns a value (non-void functions)
return; // Returns nothing (void functions only)When a return statement executes:
- The expression is evaluated
- The value is sent back to the caller
- Function execution immediately stops
- Control returns to the calling function
#include <stdio.h>
int add(int a, int b) {
return a + b; // Computes and returns the sum
// Any code after return is unreachable
printf("This never executes\n");
}
int main() {
int result = add(10, 20);
printf("Result: %d\n", result);
return 0;
}Result: 30
Returning Different Data Types
Returning Integers
|-5| = 5 |7| = 7 5! = 120 8! = 40320
Returning Floats/Doubles
Circle area (r=5): 78.54 Average: 86.6 Hypotenuse (3, 4): 5.00
Returning Characters
#include <stdio.h>
char getGrade(int score) {
if (score >= 90) return 'A';
if (score >= 80) return 'B';
if (score >= 70) return 'C';
if (score >= 60) return 'D';
return 'F';
}
char toLower(char ch) {
if (ch >= 'A' && ch <= 'Z')
return ch + 32;
return ch;
}
int main() {
printf("Score 85 → Grade: %c\n", getGrade(85));
printf("Score 42 → Grade: %c\n", getGrade(42));
printf("toLower('G') = %c\n", toLower('G'));
printf("toLower('a') = %c\n", toLower('a'));
return 0;
}Score 85 → Grade: B
Score 42 → Grade: F
toLower('G') = g
toLower('a') = aVoid Functions and return
Void functions don't return a value, but they CAN use return; (without a value) to exit early:
#include <stdio.h>
void printPositive(int n) {
if (n <= 0) {
printf("Not a positive number!\n");
return; // Exit early — skip the rest
}
printf("The number is: %d\n", n);
printf("Its square is: %d\n", n * n);
}
void printDivision(float a, float b) {
if (b == 0) {
printf("Error: Cannot divide by zero!\n");
return; // Guard clause
}
printf("%.2f / %.2f = %.2f\n", a, b, a / b);
}
int main() {
printPositive(5);
printPositive(-3);
printf("\n");
printDivision(10, 3);
printDivision(5, 0);
return 0;
}The number is: 5 Its square is: 25 Not a positive number! 10.00 / 3.00 = 3.33 Error: Cannot divide by zero!
Multiple Return Statements
A function can have multiple return statements — useful for different conditions:
Classify 5: 1 Classify -3: -1 Classify 0: 0 Search 30: index 2 Search 99: index -1 Day 3: Wednesday
Returning Pointers
Functions can return pointers, but be careful — never return a pointer to a local variable (it gets destroyed):
#include <stdio.h>
// SAFE: Returns pointer to static array
char *getGreeting(int hour) {
static char morning[] = "Good Morning!";
static char afternoon[] = "Good Afternoon!";
static char evening[] = "Good Evening!";
if (hour < 12) return morning;
if (hour < 17) return afternoon;
return evening;
}
// SAFE: Returns pointer to one of the input parameters
int *findMax(int *a, int *b) {
return (*a > *b) ? a : b;
}
// DANGEROUS - never do this:
// int *badFunction() {
// int local = 42;
// return &local; // BUG! local is destroyed after return
// }
int main() {
printf("%s\n", getGreeting(10));
printf("%s\n", getGreeting(14));
int x = 30, y = 50;
int *bigger = findMax(&x, &y);
printf("Bigger: %d\n", *bigger);
return 0;
}Good Morning! Good Afternoon! Bigger: 50
Returning Structs
C allows returning entire structures by value:
#include <stdio.h>
typedef struct {
int x;
int y;
} Point;
Point createPoint(int x, int y) {
Point p;
p.x = x;
p.y = y;
return p;
}
Point addPoints(Point a, Point b) {
Point result;
result.x = a.x + b.x;
result.y = a.y + b.y;
return result;
}
typedef struct {
int quotient;
int remainder;
} DivResult;
DivResult divideWithRemainder(int a, int b) {
DivResult result;
result.quotient = a / b;
result.remainder = a % b;
return result;
}
int main() {
Point p1 = createPoint(3, 4);
Point p2 = createPoint(7, 2);
Point sum = addPoints(p1, p2);
printf("P1: (%d, %d)\n", p1.x, p1.y);
printf("P2: (%d, %d)\n", p2.x, p2.y);
printf("Sum: (%d, %d)\n", sum.x, sum.y);
DivResult dr = divideWithRemainder(17, 5);
printf("17 / 5 = %d remainder %d\n", dr.quotient, dr.remainder);
return 0;
}P1: (3, 4) P2: (7, 2) Sum: (10, 6) 17 / 5 = 3 remainder 2
The return Value of main()
The main() function's return value goes to the operating system:
return 0;— program succeededreturn 1;(or any non-zero) — program failed
Common Mistakes
Not Returning a Value (Undefined Behavior)
#include <stdio.h>
int getMax(int a, int b) {
if (a > b) return a;
if (b > a) return b;
// What if a == b? No return statement! Undefined behavior!
// Fix: add return a; (or return b;) at the end
}
int main() {
printf("Max: %d\n", getMax(5, 5)); // Undefined!
return 0;
}Returning a Local Array (Dangling Pointer)
Interview Questions
Q1: What happens if a non-void function doesn't return a value?
It's undefined behavior. The caller may receive garbage values — whatever happens to be in the CPU register used for return values. Compilers usually generate a warning. Always ensure every code path in a non-void function returns a value.
Q2: Can you have multiple return statements in a function?
Yes, absolutely. Multiple returns are common and often make code clearer — especially for guard clauses (early exits on error conditions) and search functions (return immediately when found). Each return immediately exits the function.
Q3: Why should you never return a pointer to a local variable?
Local variables are allocated on the stack and destroyed when the function returns. A pointer to such a variable becomes a "dangling pointer" — it points to freed memory that may be overwritten at any time. Accessing it is undefined behavior.
Q4: What is the return value of main() used for?
The return value of main() is passed to the operating system as the program's exit status. Zero (EXIT_SUCCESS) means the program completed successfully. Non-zero (EXIT_FAILURE) indicates an error. Shell scripts and parent processes can check this value.
Q5: Can a void function use the return statement?
Yes, a void function can use return; (without a value) to exit early. This is useful for guard clauses — checking error conditions at the beginning and returning before the main logic. Using return value; in a void function is a compilation error.
Summary
returnsends a value back to the caller and immediately exits the function- Non-void functions MUST return a value matching their return type
- Void functions can use
return;(no value) for early exit - Multiple return statements are valid and useful for guard clauses
- Functions can return any type: int, float, char, pointers, structs
- Never return a pointer to a local variable — it creates a dangling pointer
main()returns 0 for success, non-zero for failure (to the OS)- Use
staticormallocfor data that must persist after function returns
Exam Focus
Revise definitions, diagrams, examples, and short-answer points for Return Statement in C Programming.
Interview Use
Prepare one clear explanation, one practical example, and one common mistake for this C Programming topic.
Search Terms
c-programming, c programming, programming, functions, return, statement, return statement in c programming
Related C Programming Topics