Table of contents | |
Introduction | |
Syntax | |
Sample Problems | |
Conclusion |
In programming, a function is a self-contained block of code that performs a specific task. C++ provides two types of functions: void functions and value-returning functions. Void functions, as the name suggests, do not return any value after execution. In this article, we will discuss the concept of returning from void functions in C++ and its syntax with examples.
The syntax for a void function is as follows:
void functionName(parameters) {
// code to be executed
}
Here, 'functionName' is the name of the function and 'parameters' are the input arguments (if any) passed to the function. The function body is enclosed in curly braces '{}' and contains the code to be executed.
As mentioned earlier, a void function does not return any value after execution. However, we can still use the 'return' statement to exit the function before its natural end. This is useful in situations where we want to terminate the function based on a certain condition.
For example, let's say we have a void function that performs some calculations. We want to terminate the function if the input parameter is negative. We can achieve this using the 'return' statement as follows:
void performCalculations(int x) {
if (x < 0) {
return;
}
// code to perform calculations
}
In the above example, if the value of 'x' is negative, the 'return' statement is executed, and the function terminates without performing any calculations.
Example 1: Simple Void Function with Return Statement
#include <iostream>
using namespace std;
void printMessage() {
cout << "Hello, World!" << endl;
return;
}
int main() {
printMessage();
return 0;
}
Hello, World!
In the above example, we have defined a simple void function 'printMessage()' that prints "Hello, World!" to the console. We have used the 'return' statement to exit the function after printing the message. In the 'main()' function, we have called the 'printMessage()' function, which executes the code inside the function and returns to the main() function.
Example 2: Void Function with Conditional Return Statement
#include <iostream>
using namespace std;
void calculateAverage(int a, int b, int c) {
if (a < 0 || b < 0 || c < 0) {
cout << "Invalid input" << endl;
return;
}
double avg = (a + b + c) / 3.0;
cout << "Average: " << avg << endl;
}
int main() {
calculateAverage(10, 20, 30);
calculateAverage(50, -10, 20);
return 0;
}
Average: 20
Invalid input
In the above example, we have defined a void function 'calculateAverage()' that calculates the average of three numbers. If any of the input parameters are negative, the function prints "Invalid input" to the console and exits using the 'return' statement. If all inputs are valid, the function calculates the average and prints it to the console. In the main() function, we have called the 'calculateAverage()' function with different inputs to test its functionality.
1. Write a void function 'printOddNumbers()' that prints all odd numbers between 1 and 10 to the console.
void printOddNumbers() {
for (int i = 1; i <= 10; i++) {
if (i % 2 !=0) {
cout << i << " ";
}
}
}
2. Write a void function 'printMultiplicationTable(int n)' that prints the multiplication table of a given number 'n' up to 10 to the console.
void printMultiplicationTable(int n) {
for (int i = 1; i <= 10; i++) {
cout << n << " x " << i << " = " << (n * i) << endl;
}
}
In this article, we have discussed the concept of returning from void functions in C++. We have seen that we can use the 'return' statement to exit a void function before its natural end. We have provided two examples and two sample problems to illustrate the use of returning from void functions. Understanding this concept is crucial for writing efficient and error-free code in C++.
70 videos|45 docs|15 tests
|
|
Explore Courses for Software Development exam
|