Javatpoint Logo
Javatpoint Logo

Return from void functions in C++

A function is said to be a void function when it returns nothing. It is used when the function's main purpose is to perform some action or task without producing a result that needs to be returned to the calling code. These execute the set of instructions without the need to manage return values. This simplicity can make the code easier to understand, especially for functions that perform actions without complex computations. There is no need to allocate memory for a return value. These functions are less reusable than functions that return values. Void functions provide limited information to the calling code. If the calling code needs feedback or a result from the function, the void function does not fulfil that requirement. Error handling in void functions might be more challenging because they do not provide a direct way to communicate errors to the calling code. So, other mechanisms, such as exceptions or global variables, are used to handle errors.

Example:

Let us take a program to illustrate the return from a void function in C++.

Output:

Return from void functions in C++

Explanation:

In this program, there are two functions: one is printMessage, and the other is the main function. Here, the printMessage function is a void function that returns nothing. This function will print some messages. After that, the main function will call the void function, and after executing the function and displaying the message, the program returns 0. Here, the void function will just act as a print function.

Different cases while using the void functions

If the void functions are classified, they can be classified as below.

1. Void function can return:

This function is a normal function with no return type.

Example:

Let us take an example of a simple recursive void function in C++.

Output:

Return from void functions in C++

Explanation:

This function is a recursive void function named countdown that counts down from a given number n to 1. Printing each value along the way. The base case is when n becomes 0 or less, at which point "Blastoff!" is printed. After that, the program calls this function in the main function with an initial value of 5. The using namespace std; directive allows the use of cout and endl without explicitly specifying the std:: namespace.

3. Void function which can return another void function

Let us take an example of a void function that can return another void function in C++.

Output:

Return from void functions in C++

Explanation:

This C++ program defines a void function named greetUser, which takes a constant reference to a string as a parameter and prints a greeting message to the console. Another void function, performGreeting, is introduced as a driver function that returns the greetUser function. In the main function, performGreeting is called twice with different user names ("Ram" and "Shyam"). The output of the program would be greetings for both "Ram" and "Shyam", affirming the functionality of the void functions in action.

3. A void can return a void value

Output:

Return from void functions in C++

Explanation:

In this C++ program, there is a void function named calculateAndPrintSum that takes two integer parameters a and b. Inside the function, it calculates the sum of a and b, prints the result, and then attempts to "return" a void value by using (void)"javaTpoint";. However, it is important to note that this usage is not standard or meaningful in C++. The (void)"javaTpoint"; part doesn't provide a valid void return value and doesn't affect the program's behavior. The main function calls calculateAndPrintSum twice with different values.







Youtube For Videos Join Our Youtube Channel: Join Now

Feedback


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA