×

How to call a void function in C++

Generally, any function has two types:

1. Void function: It doesn't return any value.

2. Non-void function: It returns some value.

Program to call a void function in C++

#include <iostream>
using namespace std;
 void check()
{
    cout <<"The void function has nothing to be returned, this is just a statement. \n";
}
int main()
{
    check();
    return 0;
}

Output:

How to call a void function in C++?

Generally, the return statement is replaced by the print statement in a void function. Instead of returning a value, we simply print that particular value in the function itself.

For example, let’s see this program:

#include <iostream>
using namespace std;
 
string check()
{
    return "The void function has nothing to be returned, this is just a statement";
}


int main()
{
    cout<<check()<<endl;
    return 0;
}

Output:

How to call a void function in C++?

Here, we can observe the output, which is the same in both cases. But the thing here is we used a non-void function. You can see the changes in the following:

void check() à string check()

Here void is replaced by string since we must return a value which is of string data type.

check() à  cout<<check()<<endl;

In the first case, we just called the function, but in the second case, we reached that function in a print statement (to show the output on the console). We can also observe that initially, we wrote the print statement in the void function, but in the second program, we wrote a print statement in the main function.

It's also important to know that, sometimes, the main function can be represented as a void as well as a non-void function in C, but in C++, it's not supported.

Reason:  The reasoning as to why a return value is preferred from the main function is that the runtime library of C++ (in cases of both UNIX and Windows environments) uses the return value of the main function() as the exit code for the program, or better to say, the current process being executed.

Example:

#include <iostream>
using namespace std;
 
int main()
{
    cout<<"Main function which is of non-void type"<<endl;
    return 0;
}

Output:

How to call a void function in C++?
#include <iostream>
using namespace std;
 
void main()
{
  cout<<"Main function which is of void type"<<endl;
}

Output:

How to call a void function in C++?

As usual, an error has occurred.

1)In C++, a void function can contain a return statement as well.

Example:

#include <iostream>
using namespace std;
void fun()
{
	cout << "Hello";
	return;
}
int main()
{
	fun();
	return 0;
}

Output:

Hello

2) Any void function can return another void function. Let’s see an example to understand better:

Example:

#include <iostream>
using namespace std;


void fun1()
{
	cout<<"I am function 1"<<endl;
}


void fun2()
{
	// fun2 Returning fun1 which is a void function
	return fun2();
}


int main()
{
	// Calling void function
	fun1();
	return 0;
}

Output:

How to call a void function in C++?

Related Topics

Dynamic Memory Allocation in C++

In some programming situations, the number of data items changes as the program is running, which is known as dynamic data or input. Consider a real-world situation where a program...

3 minutes read.

Top best IDEs for C/C++ Developers in 2024

Nothing in the current digital world is conceivable without programming. Everything needs programming, from the cell phones in our pockets to self-driving cars. Programming is also necessary for the mouse...

9 minutes read.

Palindrome using For loop in C++

A palindrome is a word, number, phrase, or other sequence of letters that reads the same backward as forward, such as 101 or MOM. Like other programming languages, C++ also allows...

6 minutes read.

C++ Continue

In C++, the continue statement is a useful tool for avoiding specific scenarios without breaking the loop. It is employed inside loops to move directly to the following iteration and...

4 minutes read.

C++ Range-based For Loop

In C++ language, the range-based for loop was added, which is far superior than the ordinary For loop. The implementation of a range-based for loop doesn't really need much code. It's a...

4 minutes read.

How to Handle Divide by Zero Exception in C++

If you are a programmer or interested in coding then it is obvious that you face some illogical test cases. Suppose, you have written one program that calculates the factorial...

6 minutes read.

Binary Search in C++

The binary search in the C++ programming language will be discussed. By continually halves the array and then seeking specified items from a half array; binary search is a technique...

8 minutes read.

C++ this pointer

'this' is a pointer that points to the object for which this function was called. The 'this' pointer holds the memory address of the current object. The 'this' pointer is implicitly passed to...

2 minutes read.

INT_MAX and INT_MIN in C/C++

In competitive programming, assigning a variable that maximum or minimum value a data type can carry is frequently necessary. Still, it might be challenging to recall such a significant, exact...

3 minutes read.

Object in C++

In this article, we will learn about Object in C++. In short, an object is a stateful entity with behaviour. Data is referred to as state, and functionality is referred to...

3 minutes read.

Constructor Overloading in C++

A Constructor is a class member function that is used to initialize the class's objects. Constructors have no return type and are called automatically when an object is formed. Constructor Characteristics Constructors...

3 minutes read.

C++ Enumeration

C++ Enumeration In C++, Enum is a special data type that contains some fixed sets of components that have various applications in the programming. Enum works fine with fixed constant sets...

3 minutes read.

Preventing Object Copy in C++

C++ is an object-oriented programming language that provides the ability to create objects, define class and pass objects to functions. When passing an object to a function or returning an...

7 minutes read.

Hospital Management Project in C++

The following capabilities are required to build a hospital management project: These are as follows: hospitals' names, contact information, and lists of doctors and patients. Activities Supported Hospital Data Print Patients' data to...

4 minutes read.

C++ Fork

A new process known as a "child process" is created with the fork system function and runs concurrently with the process that invoked fork() (parent process). Both processes will carry...

4 minutes read.

List back () function in C++ STL

The list::back () function of the C++ STL returns a direct reference to the last element in the list container. This function varies from list::end (), which just returns an...

2 minutes read.

Armstrong Number using While Loop in C++

What is while Loop? A while loop or while statement repeats all code of its body as long as a specific condition is satisfied. The loop ends if or when the...

4 minutes read.

wcscpy(), wcslen(), wcscmp() Functions in C++

There are many built-in functions in C++ programming language which differentiate it from C programming language in most hardware-coded languages. We will now closely look into the applications of three...

4 minutes read.

Fast Input and Output in C++

In competitive programming, it's critical to read input as quickly as possible in order to save time. "Warning: Big I / O data, be aware of certain languages (but most...

3 minutes read.

Unary Operators in C++

Unary operators in C++ Unary operator: is operations that function to produce a new value on a single operand. a) unary minus: A minus operator modifies the argument's symbol. A positive number...

3 minutes read.