The return statement in C concludes the execution of a function and redirects control back to the calling function. The return statement may or may not provide a value based on the function's return type. For example, functions with return types int and void return integer values and no values, respectively.
In the C programming language, the return statement is specifically designed to send back a singular value from a function. It is crucial that the data type of the returned value is explicitly stated in the function's definition or declaration.
Syntax:
It has the following syntax:
return return_value;
Methods of Return Functions:
Return statements serve multiple purposes and can be utilized in a variety of ways. Here are a few examples:
1. Methods that don't return values
When a function in C has a return type that is not void, the omission of the return statement is not allowed. Only functions with void return types can be used without including a return statement.
A. Functions with a void return type should refrain from using return statements.
It is not necessary to include a return statement when working with void functions since they do not return any value.
Syntax:
It has the following syntax:
void function()
{
//statements .
}
Example:
Let's consider a basic example to grasp the functionality of the return statement in the C programming language.
#include <stdio.h>
void printSum(int a, int b) {
int sum = a + b;
printf("The sum of %d and %d is: %d\n", a, b, sum);
// No return statement here
}
int main() {
int num1 = 5;
int num2 = 7;
printSum(num1, num2); // Function call
return 0;
}
Output:
The sum of 5 and 7 is: 12
Utilizing the return statement in a function that has a void return type.
Since the term void signifies emptiness, there is usually no need to provide a return value. Nevertheless, as demonstrated in the example below, it is possible to include a return statement within void functions. Nonetheless, such functions are incapable of producing any output.
Syntax:
It has the following syntax:
void function()
{
return;
}
This particular syntax functions as a jump statement that disrupts the function's sequence and terminates it. It acts as a substitute for the "break statement" which is applicable within functions.
Example:
#include <stdio.h>
void checkPosOrNeg(int number) {
if (number == 0) {
printf("The number is zero. Exiting the function.\n");
return; // Exit the function early
}
if (number > 0) {
printf("The number %d is positive.\n", number);
} else {
printf("The number %d is negative.\n", number);
}
}
int main() {
int num1 = 10;
int num2 = -5;
int num3 = 0;
checkPosOrNeg (num1);
checkPosOrNeg (num2);
checkPosOrNeg (num3);
return 0;
}
Output:
The number 10 is positive.
The number -5 is negative.
The number is zero. Exiting the function.
However, issues may arise if the return statement attempts to provide a value within a function that has a void return type.
Wrong Syntax:
void function()
{
return value;
}
Example:
#include <stdio.h>
// void method
void Print()
{
printf("Welcome to Logic Practice");
// Error: Trying to return a value from a void function
return 10;
}
// Driver method
int main()
{
// Calling print
Print();
return 0;
}
Output:
error: void function 'Print' should not return a value [-Wreturn-type]
return 10;
^~~~~~
1 error generated.
2. Procedures that return values
The output from a function with a return type that is not void, as declared and defined in the function, needs to be placed immediately after the return statement.
Syntax:
It has the following syntax:
return-type function()
{
return value;
}
Example:
#include <stdio.h>
// non-void return type function
// to calculate the factorial of a number
int factorial(int n)
{
if (n == 0 || n == 1)
return 1;
else
return n * factorial(n - 1);
}
// Driver method
int main()
{
int num = 5;
int result = factorial(num);
printf("The factorial of %d is %d\n", num, result);
return 0;
}
Output:
The factorial of 5 is 120
Conclusion:
In essence, the C return statement is vital for explaining the behavior of functions with different return types. The following information about the return statement in C is important to remember:
- The return command is employed to terminate a function and hand control back to the caller. It is relevant for both void functions and functions with non-void return types.
- In a void function, the return statement should not include a return value. However, it can be utilized to halt the function's execution or prematurely exit the function under certain conditions.