Photo by 愚木混株 cdd20 on Unsplash
Return Statement in a Void Function: A Comprehensive Guide
Purpose and Potential of Returns in Void Functions in C/C++
Understanding Void Functions
Void functions in programming are known as operations that do not have a return value. When you want to execute an instruction without needing a result, you make use of a void function.
You may be wondering; but a print statement can output values, even with a void function.
#include <stdio.h>
// Void function with a printf statement
void printMessage() {
printf("Hello, This is a void function!\n");
}
int main() {
printf("This is the main function.\n");
// Call the void function
printMessage();
printf("Back in the main function.\n");
return 0;
}
This is the main function.
Hello, This is a void function!
Back in the main function.
To make things clear, a void function does not have a return value.
A print statement may have an output but it cannot return a value that can be used in other parts of the program.
Therefore, you can refer to void functions as Non-Value Returning functions.
The Return Statement and Why We Use It.
A return statement is used to exit a function and return a value to the calling code. The return value is optional.
#include <stdio.h>
// Define a function that returns the sum of two numbers
int add(int a, int b) {
int sum = a + b;
return sum; // Return the computed sum to the caller
}
int main() {
int num1 = 1;
int num2 = 2;
// Call the 'add' function and store the result in 'result'
int result = add(num1, num2);
printf("The sum of %d and %d is %d\n", num1, num2, result);
return 0;
}
The sum of 1 and 2 is 3
Note that the return type assigned in the function declaration must match the return type you return using the return statement in the function. For example;
Return Statements in Void Functions
You are probably confused with the sub-title; return statement in void functions.
Yes, in the previous paragraph, I only said that void functions do not have a return value. However, you can use the return keyword in a void function to terminate the execution of a void function rather than returning a value.
#include <stdio.h>
// Void function with a return statement
void exitFunction() {
printf("I am inside a void function.\n");
return; // This return statement will exit the function prematurely.
printf("but I cannot be reached.\n");
}
int main() {
printf("Hello, I inside the main function.\n");
// Call the void function
exitFunction();
printf("Back into main function.\n");
return 0;
}
Hello, I inside the main function.
I am inside a void function.
Back into main function.
Void Functions as Control Flows
A void function can also be used as a control flow to return another void function. This automatically returns the control to the caller.
#include <stdio.h>
// Void function that calls another void function
void funcB() {
printf("Inside function B\n");
}
void funcA() {
printf("Inside function A\n");
funcB(); // Call another void function
printf("Back in function A\n");
}
int main() {
printf("Inside main\n");
funcA();
printf("Back in main\n");
return 0;
}
Inside main
Inside function A
Inside function B
Back in function A
Back in main
This was a short article and I hope you learnt something new from it. Please do well to share and drop a comment. Thank you❤️