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 theprintMessage()
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 typestring
that allows the caller to pass a name to the function. The function body contains:</>Copy
This statement prints a greeting message to the console that includes the name passed as an argument.cout << "Hello, " << name << "!" << endl;
- int main()
-
The
main
function is the entry point of the program. It calls thegreetUser
function with the argument"Alice"
:</>Copy
This executes thegreetUser("Alice");
greetUser
function, printingHello, Alice!
to the console. Finally, it returns0
, 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 typeint
that allow the caller to pass two integers to the function. The function body contains:</>Copy
This statement calculates the sum ofcout << "The sum is: " << (a + b) << endl;
a
andb
, and prints the result to the console in the formatThe sum is: [result]
. - int main()
-
The
main
function is the entry point of the program. It calls thedisplaySum
function with the arguments10
and20
:</>Copy
This executes thedisplaySum(10, 20);
displaySum
function, which calculates the sum of10
and20
and printsThe sum is: 30
to the console. Finally, it returns0
, 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 typestring
that allows the caller to pass a message to be logged.int level
: A parameter of typeint
that indicates the log level. The function body contains:</>Copy
This statement constructs a formatted log message that includes the log level and message, and then prints it to the console.cout << "[Level " << level << "]: " << message << endl;
- int main()
-
The
main
function is the entry point of the program. It calls thelogMessage
function twice with different arguments:</>Copy
These calls execute thelogMessage("Initialization complete", 1); logMessage("An error occurred", 3);
logMessage
function, which prints the following output:</>Copy
Finally, it returns[Level 1]: Initialization complete [Level 3]: An error occurred
0
, indicating that the program executed successfully.
Output
[Level 1]: Initialization complete
[Level 3]: An error occurred