Function Returning void

In C++, a function with the void return type does not return any value to the calling function. These functions are commonly used to perform operations like printing results, updating data, or logging information, where returning a value is not required.

Syntax

The syntax of a function that returns void is give below:

</>
Copy
void function_name(parameter_list) {
    // Function body
}
void
Specifies that the function does not return a value.
function_name
The name of the function, which should follow naming conventions.
parameter_list
Optional, specifies the input parameters the function accepts. These parameters allow data to be passed to the function when it is called.

Examples

Example 1: Basic Function Returning void

In this example, we shall define a function printMessage() with void return type.

Program

</>
Copy
#include <iostream>
using namespace std;

void printMessage() {
    cout << "Hello, World!" << endl;
}

int main() {
    printMessage(); // Call the void function
    return 0;
}
void printMessage()
This is a function declaration and definition. The function has the following components: void: Specifies that the function does not return any value. printMessage: The name of the function. The function does not accept any inputs. The function body prints a message to output.
int main()
The main function is the entry point of the program. It calls the printMessage() function and then returns 0 specifying that the code execution is successful.

Output

Hello, World!

Example 2: Function with Parameters Returning void

In this program, we will show how to write a C++ function with a void return type that accepts a parameter.

Program

</>
Copy
#include <iostream>
using namespace std;

void greetUser(string name) {
    cout << "Hello, " << name << "!" << endl;
}

int main() {
    greetUser("Alice"); // Pass a string to the function
    return 0;
}
void greetUser(string name)
This is a function declaration and definition. The function has the following components: void: Specifies that the function does not return any value. greetUser: The name of the function. string name: A parameter of type string that allows the caller to pass a name to the function. The function body contains:
</>
Copy
cout << "Hello, " << name << "!" << endl;
This statement prints a greeting message to the console that includes the name passed as an argument.
int main()
The main function is the entry point of the program. It calls the greetUser function with the argument "Alice":
</>
Copy
greetUser("Alice");
This executes the greetUser function, printing Hello, Alice! to the console. Finally, it returns 0, indicating that the program executed successfully.

Output

Hello, Alice!

Example 3: Function returning void Performing Calculations

In this program, we will show how to write a C++ function with a void return type that performs some calculations in its body.

Program

</>
Copy
#include <iostream>
using namespace std;

void displaySum(int a, int b) {
    cout << "The sum is: " << (a + b) << endl;
}

int main() {
    displaySum(10, 20); // Call the function with two integers
    return 0;
}
void displaySum(int a, int b)
This is a function declaration and definition. The function has the following components: void: Specifies that the function does not return any value. displaySum: The name of the function. int a, int b: Parameters of type int that allow the caller to pass two integers to the function. The function body contains:
</>
Copy
cout << "The sum is: " << (a + b) << endl;
This statement calculates the sum of a and b, and prints the result to the console in the format The sum is: [result].
int main()
The main function is the entry point of the program. It calls the displaySum function with the arguments 10 and 20:
</>
Copy
displaySum(10, 20);
This executes the displaySum function, which calculates the sum of 10 and 20 and prints The sum is: 30 to the console. Finally, it returns 0, indicating that the program executed successfully.

Output

The sum is: 30

Example 4: void Function for Logging

In this program, we will show how to write a C++ function with a void return type that performs logging operations.

Program

</>
Copy
#include <iostream>
using namespace std;

void logMessage(string message, int level) {
    cout << "[Level " << level << "]: " << message << endl;
}

int main() {
    logMessage("Initialization complete", 1);
    logMessage("An error occurred", 3);
    return 0;
}
void logMessage(string message, int level)
This is a function declaration and definition. The function has the following components: void: Specifies that the function does not return any value. logMessage: The name of the function. string message: A parameter of type string that allows the caller to pass a message to be logged. int level: A parameter of type int that indicates the log level. The function body contains:
</>
Copy
cout << "[Level " << level << "]: " << message << endl;
This statement constructs a formatted log message that includes the log level and message, and then prints it to the console.
int main()
The main function is the entry point of the program. It calls the logMessage function twice with different arguments:
</>
Copy
logMessage("Initialization complete", 1);
logMessage("An error occurred", 3);
These calls execute the logMessage function, which prints the following output:
</>
Copy
[Level 1]: Initialization complete
[Level 3]: An error occurred
Finally, it returns 0, indicating that the program executed successfully.

Output

[Level 1]: Initialization complete
[Level 3]: An error occurred