I
Insight Horizon Media

How do you return a value from a void function in C++?

Author

Daniel Johnson

Published Feb 13, 2026

How do you return a value from a void function in C++?

Return from void functions in C++

  1. A void function can do return. We can simply write return statement in a void fun().
  2. A void fun() can return another void function.
  3. A void() can return a void value. A void() cannot return a value that can be used.

What should a void function return in C?

In computer programming, when void is used as a function return type, it indicates that the function does not return a value. When void appears in a pointer declaration, it specifies that the pointer is universal. When used in a function’s parameter list, void indicates that the function takes no parameters.

Can we use return in a void method?

Any method declared void doesn’t return a value. It does not need to contain a return statement, but it may do so. The data type of the return value must match the method’s declared return type; you can’t return an integer value from a method declared to return a boolean.

Can you return 0 in a void function C++?

The reason for the error/warning message is because a void function, by definition, does not return a value. When you include the return (0) statement, you are saying that the function returns a value of 0. Use return; instead of return(0); to exit a void function.

What is void return type in C++?

When used as a function return type, the void keyword specifies that the function doesn’t return a value. When used for a function’s parameter list, void specifies that the function takes no parameters. When used in the declaration of a pointer, void specifies that the pointer is “universal.”

What does void main mean in C++?

The void main() indicates that the main() function will not return any value, but the int main() indicates that the main() can return integer type data. When our program is simple, and it is not going to terminate before reaching the last line of the code, or the code is error free, then we can use the void main().

How do you declare a void function in C++?

Function Declaration The syntax for declaring the function prototype is: type function-name (formal parameter type list); type : the type of the value returned by the function. When no value is returned, the type is “void”.

How do I return to main function in C++?

The normal exit of program is represented by zero return value. If the code has errors, fault etc., it will be terminated by non-zero value. In C++ language, the main() function can be left without return value. By default, it will return zero.

What is a value-returning function in C++?

With value-returning functions, the actions result in the return of a value, and that value can be used in an expression. For example, let’s write a function that returns the smallest of three input values.

What is void * C++?

void pointer in C / C++ A void pointer is a pointer that has no associated data type with it. A void pointer can hold address of any type and can be typcasted to any type.

What is void main function in C?