Return Statement in C: A return statement is used to terminate a function. All the user defined functions as well as main functions will have a return statement at the end of the function if the function has some data type other than void.

Syntax: Return variable or constant or expression;
Return 0;
Working of return statement in C?
- Ends Execution: When a return statement is encountered, the function immediately stops executing.
- Returns Value (Optional): If an expression is provided, its value is evaluated and sent back to the calling function.
- Void Functions: In functions that don’t return a value (void), the return statement is used without an expression, simply to terminate the function.
Why use return in C?
- Breaking out of Functions: To exit a function early under certain conditions.
- Returning Results: To send calculated or processed values back to the main program.
- Error Handling: To signal errors or exceptional situations.
Return Data Types:
- Any Data Type: C functions can return various data types: int, float, double, char, pointers, structures, etc. The return type must be declared in the function’s definition.
- void: If a function doesn’t need to return a value, its return type is void.
Calculating the Square with “return”:
int square(int num) {
return num * num; // Returns the square of num
}
int main() {
int result = square(5);
printf("The square of 5 is: %d\n", result); // Output: The square of 5 is: 25
}
FAQs related to the topic:
What happens if I don’t use a return statement in a non-void function in C?
If you omit a return statement in a non-void function, the behavior is undefined.
This means the compiler might return a garbage value, or your program might crash.
It’s crucial to always have a return statement in functions that are supposed to return a value.Can I have multiple return statements in a single C function?
Yes! It’s perfectly valid to have multiple return statements within a function.
However, the function will stop executing immediately upon encountering the first return statement it reaches.
This is often used in conditional logic (e.g., within if and else blocks) to return different values based on specific criteria.Can I return multiple values from a C function?
Not directly. C functions can only return a single value. If you need to return multiple values, you can use alternative techniques like:
Pointers: Pass pointers to variables as arguments and modify them within the function.
Structures: Create a structure to hold multiple values and return the structure.
Arrays: Return an array containing multiple values.What’s the difference between return 0; and return 1; in the main function?
In the main function, the integer value returned (return 0; or return 1;) typically indicates the program’s exit status to the operating system.
return 0; signifies successful execution.
return 1; (or any non-zero value) usually signals an error or abnormal termination.