Menu
Coddy logo textTech

Void Functions

Part of the Fundamentals section of Coddy's C++ journey — lesson 57 of 74.

In C++, a void function is a function that does not return any value. When you declare a function as void, it indicates that the function performs a task or a set of operations, but it does not produce a result that needs to be returned to the caller. void functions are used when you want to perform actions like printing output, modifying object states, or executing a sequence of statements without returning a specific value.

Here's the basic structure of a void function:

void functionName(parameters) {
    // Code to be executed
}
challenge icon

Challenge

Easy

Create a void function named printNTimes. This function should take two arguments:

  1. A string message.
  2. An integer n.

The function should print the message to the console n times. Use a for loop to repeat the printing.

In the main function, call printNTimes with the inputs message and the input n

Cheat sheet

A void function does not return any value and is used to perform actions without producing a result.

Basic structure of a void function:

void functionName(parameters) {
    // Code to be executed
}

Try it yourself

#include <iostream>
#include <string>

void printNTimes(std::string message, int n) {
    // Write you code here
}

int main() {
    std::string msg;
    int n;
    std::getline(std::cin, msg); 
    std::cin >> n;

    printNTimes(msg, n);
    return 0;
}
quiz iconTest yourself

This lesson includes a short quiz. Start the lesson to answer it and track your progress.

All lessons in Fundamentals