×

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

Virtual class in C++

Introduction to Virtual Base class Virtual base classes can be utilized in virtual inheritance as a mechanism for examining many "instances" of a certain class while searching through multiple inheritances in...

6 minutes read.

Virtual Functions and Runtime Polymorphism in C++

In this tutorial, we will explore more on virtual functions and runtime polymorphism in the most useful language C++. A virtual function is a member function with the keyword virtual used...

6 minutes read.

How to make a password program in C++

Before understanding the password program, one must know about a password and why it is required. Password: A password is a word that permits access to somewhere or something. A password...

4 minutes read.

Array program in C++

What is an Array? An array is a set of identically typed elements that are organized into contiguous memory locations and each element can be independently accessed using an index. We can...

16 minutes read.

C++ Program to Print Fibonacci Triangle

Fibonacci Triangle Program in CPP Definition: Fibonacci Triangle as the name suggests is the same as the Fibonacci number series where the next element is the sum of the previous two elements....

3 minutes read.

Depth First Search Program to Traverse a Graph in C++

Depth First Search (DFS) is a technique that is used for transversing the graph. The process of Depth First Search (DFS) starts from the root node and then next comes...

6 minutes read.

Roadmap to C++ Programming

Introduction There are so many programming languages available in the market, but among them, C++ is something that never lost its charm. It has a powerful impact on the programming world....

4 minutes read.

Type difference of Character literals in C VS C++

Character literals in C: In C, a character literal is represented by a single character enclosed in single quotes, such as 'a' or 'b'. It is of type int. This means...

5 minutes read.

C++ Type Casting

The type casting of variables in the C++ programming language will be covered in this section. The term "type casting" describes how software converts one data type to another. There...

9 minutes read.

gmtime() function in C/C++

C++ language is used to make high-performance applications that can work efficiently. It is one of the world's most popular languages. It is an object-oriented and high-level programming language, which...

4 minutes read.

CPP Templates

C++ provides a powerful feature called template, which allows the definition of generic classes and generic functions. Generic programming is a technique where different algorithms work in communion by using...

4 minutes read.

C++ Friend Functions

In C++, friends are special functions that are not a part of a class yet have access to its private and protected members. The friend keyword is used to declare...

4 minutes read.

RTTI (Run-Time Type Information) in C++

In C++, RTTI or Run-Time Type Information reveals information about the data type of an object at runtime and only works with classes that have at least one virtual function....

3 minutes read.

Differences Between C Structures and C++ Structures

The below table clearly describes the differences between C and C++ programming languages Structures concepts where the core principle concepts like static members, data handling, pointers, access modifiers, the importance...

3 minutes read.

C++ For loop

C++ loop Statement C++ Loop statement allows us to repeat the execution of a statement or group of statements multiple times. The statement(s) repeat execution within loop until the condition of loop...

2 minutes read.

Divide by Zero Exception in C++

We use exception handling method to handle the divide by zero exception. Dividing a number with zero is generally mathematical error. We have to exception handling method to overcome this...

2 minutes read.

Pointer to Object in C++

What is a pointer? A pointer in C++ is used to point the variable by storing the address of the variable. In C++, to print the address of the variable, we...

4 minutes read.

Assertions in C/C++

Assertions are the statements used to check presumptions which is made by the programmers. Example: Assertion is used to verify whether the malloc returned by the pointer is NULL or not. For...

4 minutes read.

C++ Call by Value

In this article, we will discuss C++ Call by Value with their syntax, examples, use cases, advantages, and disadvantages. C++ Function A function is a set of statements that executes a task....

5 minutes read.

C++ Data Abstraction

Object-oriented programming (OOP) provides a number of characteristics that enable programmers to design programmes based on a variety of ideas, reducing errors and increasing program flexibility. The abstraction of data...

7 minutes read.