Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Lära Void Return Type | Function Return Values Specification
Practice
Projects
Quizzes & Challenges
Quizzes
Challenges
/
C++ Functions

bookVoid Return Type

The void return type indicates that a function does not return a value. Such a function performs its task but doesn’t produce a result to be used elsewhere in the program. For example, consider a function that prints the values of a 1D dynamic array.

main.cpp

main.cpp

copy
1234567891011121314151617181920212223
#include <iostream> // Function to print values of a 1D dynamic array void print_array(const int* arr, const int size) { for (int i = 0; i < size; ++i) std::cout << arr[i] << " "; std::cout << std::endl; } int main() { // Example 1D dynamic array int size = 5; int* dynamic_array = new int[size] { 1, 2, 3, 4, 5 }; // Call the function to print the array values print_array(dynamic_array, size); // Deallocate the dynamically allocated memory delete[] dynamic_array; }

The purpose of this function is to print the array, and it doesn’t return any meaningful result, so a void return type is appropriate. However, you can still use the return statement in a void function to end its execution early under certain conditions.

main.cpp

main.cpp

copy
123456789101112131415161718
#include <iostream> void display_division(double a, double b) { if (b == 0) return; std::cout << "displayDivision was called: " << a / b << std::endl; } int main() { // Call the function to print the division result display_division(15, 8); // Now second argument is zero display_division(15, 0); }
question mark

Which of the following statements is true about a function with a void return type?

Select the correct answer

Var allt tydligt?

Hur kan vi förbättra det?

Tack för dina kommentarer!

Avsnitt 3. Kapitel 4

Fråga AI

expand

Fråga AI

ChatGPT

Fråga vad du vill eller prova någon av de föreslagna frågorna för att starta vårt samtal

bookVoid Return Type

Svep för att visa menyn

The void return type indicates that a function does not return a value. Such a function performs its task but doesn’t produce a result to be used elsewhere in the program. For example, consider a function that prints the values of a 1D dynamic array.

main.cpp

main.cpp

copy
1234567891011121314151617181920212223
#include <iostream> // Function to print values of a 1D dynamic array void print_array(const int* arr, const int size) { for (int i = 0; i < size; ++i) std::cout << arr[i] << " "; std::cout << std::endl; } int main() { // Example 1D dynamic array int size = 5; int* dynamic_array = new int[size] { 1, 2, 3, 4, 5 }; // Call the function to print the array values print_array(dynamic_array, size); // Deallocate the dynamically allocated memory delete[] dynamic_array; }

The purpose of this function is to print the array, and it doesn’t return any meaningful result, so a void return type is appropriate. However, you can still use the return statement in a void function to end its execution early under certain conditions.

main.cpp

main.cpp

copy
123456789101112131415161718
#include <iostream> void display_division(double a, double b) { if (b == 0) return; std::cout << "displayDivision was called: " << a / b << std::endl; } int main() { // Call the function to print the division result display_division(15, 8); // Now second argument is zero display_division(15, 0); }
question mark

Which of the following statements is true about a function with a void return type?

Select the correct answer

Var allt tydligt?

Hur kan vi förbättra det?

Tack för dina kommentarer!

Avsnitt 3. Kapitel 4
some-alt