- C Programming Tutorial
- C - Home
- Basics of C
- C - Introduction
- C - Features
- C - Basics
- C - History
- C - Structure of C Program
- C - Program Structure
- C - Hello World
- C - Compilation Process
- C - Comments
- C - Tokens
- C - Keywords
- C - Identifiers
- C - User Input
- C - Basic Syntax
- C - Data Types
- C - Variables
- C - Integer Promotions
- C - Type Conversion
- C - Type Casting
- C - Booleans
- Constants and Literals in C
- C - Constants
- C - Literals
- C - Escape sequences
- C - Format Specifiers
- Operators in C
- C - Operators
- C - Arithmetic Operators
- C - Relational Operators
- C - Logical Operators
- C - Bitwise Operators
- C - Assignment Operators
- C - Unary Operators
- C - Increment and Decrement Operators
- C - Ternary Operator
- C - sizeof Operator
- C - Operator Precedence
- C - Misc Operators
- Decision Making in C
- C - Decision Making
- C - if statement
- C - if...else statement
- C - nested if statements
- C - switch statement
- C - nested switch statements
- Loops in C
- C - Loops
- C - While loop
- C - For loop
- C - Do...while loop
- C - Nested loop
- C - Infinite loop
- C - Break Statement
- C - Continue Statement
- C - goto Statement
- Functions in C
- C - Functions
- C - Main Function
- C - Function call by Value
- C - Function call by reference
- C - Nested Functions
- C - Variadic Functions
- C - User-Defined Functions
- C - Callback Function
- C - Return Statement
- C - Recursion
- Scope Rules in C
- C - Scope Rules
- C - Static Variables
- C - Global Variables
- Arrays in C
- C - Arrays
- C - Properties of Array
- C - Multi-Dimensional Arrays
- C - Passing Arrays to Function
- C - Return Array from Function
- C - Variable Length Arrays
- Pointers in C
- C - Pointers
- C - Pointers and Arrays
- C - Applications of Pointers
- C - Pointer Arithmetics
- C - Array of Pointers
- C - Pointer to Pointer
- C - Passing Pointers to Functions
- C - Return Pointer from Functions
- C - Function Pointers
- C - Pointer to an Array
- C - Pointers to Structures
- C - Chain of Pointers
- C - Pointer vs Array
- C - Character Pointers and Functions
- C - NULL Pointer
- C - void Pointer
- C - Dangling Pointers
- C - Dereference Pointer
- C - Near, Far and Huge Pointers
- C - Initialization of Pointer Arrays
- C - Pointers vs. Multi-dimensional Arrays
- Strings in C
- C - Strings
- C - Array of Strings
- C - Special Characters
- C Structures and Unions
- C - Structures
- C - Structures and Functions
- C - Arrays of Structures
- C - Self-Referential Structures
- C - Lookup Tables
- C - Dot (.) Operator
- C - Enumeration (or enum)
- C - Structure Padding and Packing
- C - Nested Structures
- C - Anonymous Structure and Union
- C - Unions
- C - Bit Fields
- C - Typedef
- File Handling in C
- C - Input & Output
- C - File I/O (File Handling)
- C Preprocessors
- C - Preprocessors
- C - Pragmas
- C - Preprocessor Operators
- C - Macros
- C - Header Files
- Memory Management in C
- C - Memory Management
- C - Memory Address
- C - Storage Classes
- Miscellaneous Topics
- C - Error Handling
- C - Variable Arguments
- C - Command Execution
- C - Math Functions
- C - String Functions
- C - Static Keyword
- C - Random Number Generation
- C - Command Line Arguments
C Programming - C return statement
![]() Share with a Friend |
C Programming - C return statement
C return Statement
The return statement in C is used to terminate the execution of a function and optionally return a value to the function caller. It is an essential part of functions in C, especially in functions that have a return type other than void.
Syntax of the return Statement
C
return [expression];
- expression: The value or result that the function returns. This can be any valid expression of the same type as the return type of the function. If the function has a void return type, no expression is needed.
Types of Return Statements
- Returning a Value (Non-void Functions)
- A function that is designed to return a value must use the return statement to send a value back to the caller.
- The type of the value must match the declared return type of the function.
- No Return Value (Void Functions)
- For functions that do not return a value (void functions), the return statement can still be used to exit the function early.
Examples of return Statement in C
Example 1: Returning a Value from a Function
C
#include <stdio.h>
// Function that returns the sum of two integers
int add(int a, int b) {
return a + b; // Returns the sum of a and b
}
int main() {
int result = add(5, 3); // Calling the add function
printf("Sum: %d\n", result); // Printing the result
return 0;
}
Explanation:
- The add function returns the sum of two integers (a + b) using the return statement.
- The value returned by the function (8 in this case) is captured in the result variable in main() and printed.
Output:
Sum: 8
Example 2: Early Exit from a Function (Void Function)
C
#include <stdio.h>
// Void function that prints a message and exits early
void printMessage(int num) {
if (num < 0) {
printf("Negative number detected. Exiting function.\n");
return; // Early exit if the number is negative
}
printf("Number is: %d\n", num);
}
int main() {
printMessage(10); // Prints the number
printMessage(-5); // Exits early due to negative number
return 0;
}
Explanation:
- The printMessage function checks if the input number is negative. If it is, it uses the return statement to exit the function early.
- If the number is non-negative, it prints the number.
Output:
Number is: 10
Negative number detected. Exiting function.
Key Points to Remember About return in C
- Return Type: If the function has a non-void return type, the return statement must return a value of that type. If the function's return type is void, the return statement does not need to return any value.
- Early Return: The return statement can be used at any point in a function to exit early. This is particularly useful in cases like error handling or conditional exits.
- Optional for Void Functions: For functions with a void return type, the return statement is optional, but it can still be used to exit the function early.
- Function Termination: When a return statement is executed, it not only returns the specified value (if applicable) but also terminates the function's execution, and control is transferred back to the calling function.
- No Return in Main: In C, the main() function may return an integer value to indicate the exit status of the program. The return value is typically 0 for successful execution, or a non-zero value to indicate an error. If no return statement is present in main(), it is implicitly assumed to return 0.
Conclusion
The return statement is crucial in C for terminating a function and passing back control to the calling code. In functions with return values, it allows returning a computed value, while in void functions, it can be used to exit the function early. Understanding the return statement is fundamental to working with functions in C.
