What are void functions?

A void function is a function that performs a task but doesn't return a value to the caller. The keyword void means "nothing" or "no value." These functions are also called non-value returning functions.

void function_name(parameters)
{
    // perform some action
    // no return statement needed (or return; with no value)
}

Void functions are perfect for tasks like:

  • Displaying information to the user
  • Modifying global variables
  • Performing calculations without needing to send results back
  • Organizing code into logical chunks

Side effects and void functions

Side effects are changes that a function makes outside of itself - anything beyond just calculating and returning a value. Common side effects include:

  • Printing output to the screen
  • Modifying global variables
  • Writing to files
  • Playing sounds
  • Updating the display

Void functions are perfect for creating side effects because they're designed to "do something" rather than "calculate something." When you need a function to perform an action that changes the state of your program or interacts with the outside world, void functions are the natural choice.

For example, std::cout statements are side effects - they change what appears on the screen rather than computing a value to return.

Basic void function example

#include <iostream>

void sayHello()
{
    std::cout << "Hello, World!" << std::endl;
}

int main()
{
    sayHello();  // Call the function
    sayHello();  // Call it again

    return 0;
}

Output:

Hello, World!
Hello, World!

Notice that:

  • The function has void as its return type
  • There's no return statement with a value
  • We call the function but don't assign its result to anything

Void functions with parameters

We will dive deeper into parameters and arguments in an upcoming lesson.

Void functions can accept parameters to customize their behavior:

#include <iostream>

void displayCalculation(int a, int b)
{
    int sum = a + b;

    std::cout << "First number: " << a << std::endl;
    std::cout << "Second number: " << b << std::endl;
    std::cout << "Sum: " << sum << std::endl;
}

int main()
{
    displayCalculation(12, 8);

    return 0;
}

Output:

First number: 12
Second number: 8
Sum: 20

The return statement in void functions

Void functions can use return statements, but they cannot return a value:

#include <iostream>

void checkAge(int age)
{
    if (age < 0)
    {
        std::cout << "Invalid age!" << std::endl;
        return;  // Exit the function early
    }

    if (age < 13)
        std::cout << "You are a child." << std::endl;
    else if (age < 20)
        std::cout << "You are a teenager." << std::endl;
    else
        std::cout << "You are an adult." << std::endl;
}

int main()
{
    checkAge(-5);   // Will exit early
    checkAge(10);   // Will complete normally
    checkAge(25);   // Will complete normally

    return 0;
}

Output:

Invalid age!
You are a child.
You are an adult.

The return; statement (with no value) immediately exits the function, which is useful for error handling or early termination.

Summary

Void functions are essential for organizing code and performing actions that don't need to return values:

  • Use void as the return type for functions that don't return values
  • Void functions can have parameters to customize their behavior
  • Use return; (without a value) to exit early from void functions
  • Void functions are perfect for displaying output, organizing code, and performing actions
  • Call void functions as statements, not in expressions

Void functions help make your code more modular, readable, and maintainable by breaking complex tasks into smaller, focused functions.