- C Programming Tutorial
- C - Home
- C - Overview
- C - Features
- C - History
- C - Environment Setup
- 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 - Constants
- C - Literals
- C - Escape sequences
- C - Storage Classes
- C - Operators
- C - Decision Making
- C - if statement
- C - if...else statement
- C - nested if statements
- C - switch statement
- C - nested switch statements
- 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
- C - Functions
- C - Main Functions
- C - Return Statement
- C - Recursion
- C - Scope Rules
- C - Arrays
- C - Properties of Array
- C - Multi-Dimensional Arrays
- C - Passing Arrays to Function
- C - Return Array from Function
- C - Variable Length Arrays
- C - Pointers
- C - Pointer Arithmetics
- C - Passing Pointers to Functions
- C - Strings
- C - Array of Strings
- C - Structures
- C - Structures and Functions
- C - Arrays of Structures
- C - Pointers to Structures
- C - Self-Referential Structures
- C - Nested Structures
- C - Unions
- C - Bit Fields
- C - Typedef
- C - Input & Output
- C - File I/O
- C - Preprocessors
- C - Header Files
- C - Type Casting
- C - Error Handling
- C - Variable Arguments
- C - Memory Management
- C - Command Line Arguments
- C Programming Resources
- C - Questions & Answers
- C - Quick Guide
- C - Useful Resources
- C - Discussion
Return Statement in C
The return statement appears as the last statement in a C function. It returns the program flow control back to the calling function. The return statement in C terminates the execution of a function and returns control to the calling function. Every function in C should have return as its last statement. The return type of a function and the type of expression returned by it must match.
The return statement can also return a value to the calling function. Execution resumes in the calling function at the point immediately following the call. The calling function receives the value returned by the called function in a variable whose type should match with the function’ return type.
The following main() function shows return as its last statement −
int main(){ // function body; return 0; }
The main() function returning 0 indicates the successful completion of the function. To indicate failure of the function, a non−zero expression is returned.
Void return type
A function's return type can be void. In such a case, return statement is optional. It may be omitted, or return without any expression is used.
#include <stdio.h> /* function declaration */ void test(){ return; } int main() { test(); printf("end"); return 0; }
Each function in the program must have a forward declaration of its prototype. By default, each function returns an integer. However, function of other return types without prototype is not accepted.
Return type mismatch
int main(){ test(5); printf("end"); return 0; } float test(int a) { return 1.1 ; }
Output
Error: C:\Users\mlath\test.c|12|error: conflicting types for 'test'
This is because, function without prototype is assumed as of int type, which conflicts with the definition.
The same error occurs if the return type of a function in the prototype doesn't match with the type of return expression, an error is reported as below −
float test(int); int main(){ test(5); printf("end"); return 0; } float test(float a){ return 1.1 ; }
Multiple return
A function can be defined with more than one arguments, but can return only one value. You can however use multiple conditional return statements as shown below −
int test(int); int main() { test(5); printf("end"); return 0; } int test(int a){ if (a<3) return 1; else return 0; }
Function returns an array
It is not possible to return an entire array as an argument to a function. However, you can return a pointer to an array by specifying the array's name without an index.
Example
The following program shows how to pass an array to a function that returns an array after performing a certain process.
#include <stdio.h> int* test(int *); int main(){ int a[] = {1,2,3,4}; int i; int *b = test(a); for (i=0; i<4; i++){ printf("%d\n", b[i]); } return 0; } int * test(int*a){ int i; for (i=0; i<4; i++){ a[i] = 2*a[i]; } return a; }
Output
2 4 6 8
function can only return a single value using return statement. To return multiple values, we use pointers or structures
exit()
Unlike the return statement, the exit() function is also used to terminate the execution of the program without transferring the control back to the calling function. It is used inside a function when the program has finished its execution or when an unrecoverable error occurs. It is a standard way of handling exception errors in C. When exit() is called, the program control does not return to the point where exit() was invoked. Instead, the control is handed back to the operating system.
The exit() function is library function defined in stdlib.h header file.
Syntax
void exit(int status);
exit() is typically called from the main() function or any other function to terminate the entire program.
It is included in the <stdlib.h> header file.
Since it results in termination of the program, exit() does not return a value directly to the caller function. Instead, it terminates the program and returns a status code. It is an integer that represents the exit status of the program, indicating success or failure.