Back to: C Tutorials For Beginners and Professionals
Return Statement in C Language with Examples
In this article, I am going to discuss Return Statements in C Language with examples. Please read our previous articles, where we discussed Continue Statement in C. At the end of this article, you will understand what a Return Statement in C is and when and how to use the Return statement in C with examples.
Return Statement in C Language:
In the C programming language, the return statement is used to exit a function and return control to the calling function. This statement can also return a value to the calling function. The basic syntax of the return statement in C is as follows:
return [expression];
Here’s a breakdown of how it works:
- Exiting the Function: When a return statement is executed, the current function is terminated immediately. If the return statement is in the main function, it terminates the program.
- Returning a Value: The expression in the return statement is optional. If provided, it is evaluated, and its value is returned to the function caller. This is useful for functions that compute a value that needs to be used by the calling function.
- Data Type: The type of the returned value must match the return type specified in the function declaration. For instance, if a function is declared to return an int, the expression in the return statement must be evaluated as an int.
- No Return Value: For functions declared with a void return type, the return statement can be used without an expression to exit the function. In this case, it simply serves to end the function’s execution.
- Control Flow: The return statement can be placed anywhere within a function, but it’s typically used at the end or within conditional statements to return different values based on certain conditions.
Usage in Functions Returning a Value:
In functions declared to return a value (other than void), the return statement is used to specify the value to be returned. For example, in a function returning an int, you might have:
int add(int a, int b) { return a + b; }
Usage in void Functions:
In functions declared as void (meaning they do not return a value), the return statement can be used to exit the function early without an expression. For example:
void displayMessage() { printf("Hello, World!"); return; // Optional here, used for early exit }
Exit Functions Early:
The return statement can be used to exit a function early, often in conditional statements. For example:
int divide(int numerator, int denominator) { if (denominator == 0) { printf("Error: Division by zero.\n"); return 0; // Early exit on error } return numerator / denominator; }
Return Statement Examples in C
In the C programming language, the return statement exits a function and optionally returns a value to the caller. Here are some examples to illustrate its use:
Returning an Integer:
int sum(int a, int b) { return a + b; }
Returning a Character:
char getGrade(int marks) { if (marks >= 90) return 'A'; else if (marks >= 80) return 'B'; else if (marks >= 70) return 'C'; else return 'F'; }
Returning a Float:
float calculateArea(float radius) { return 3.14 * radius * radius; }
Returning from a Void Function (No Value Returned):
void printMessage() { printf("Hello, World!"); return; // Optional in void functions }
Returning a Pointer:
int* getArray() { static int arr[5] = {1, 2, 3, 4, 5}; return arr; }
Using Return to Exit Early:
void divide(int a, int b) { if (b == 0) { printf("Cannot divide by zero.\n"); return; } printf("Result: %d\n", a / b); }
Returning a Struct:
typedef struct { int x; int y; } Point; Point createPoint(int x, int y) { Point p = {x, y}; return p; }
Returning a Boolean (Using stdbool.h):
#include <stdbool.h> bool isEven(int num) { return (num % 2 == 0); }
Each of these examples showcases different uses of the return statement, such as returning various data types, using return for early exit from a function, or returning complex data structures like pointers and structs.
In the next article, I will discuss the Goto Statement in C Language with Examples. Here, in this article, I try to explain Return Statements in C Language with Examples. I hope you enjoy this article. I would like to have your feedback. Please post your feedback, questions, or comments about this article.