Void Functions and Side Effects
You'll learn: How functions can do useful work without returning a value by performing actions or changing things around them.
What's a Void Function?
Not every function needs to give you something back. A void function (sometimes called a "procedure") is one that performs an action but doesn't return a value. Instead of computing and returning a result, it does something — like printing text to the screen, modifying a variable, or saving data.
Think of it like asking someone to turn on the lights. They don't hand you anything back, but the room gets brighter. The action itself is the point.
What Are Side Effects?
When a function changes something outside itself — like modifying a variable that was passed to it, printing to the console, or altering program state — that's called a side effect. The function "reaches out" and affects the world beyond its own boundaries.
Example scenarios:
- A function that prints a welcome message (no return value needed)
- A function that modifies a variable passed as a parameter
- A function that writes to a file or updates a database
Why Use Void Functions?
Sometimes you want side effects! Not every function's purpose is calculation. Many functions exist purely to do things: display information, organize code, or change state. Void functions make your intent clear: "This function performs an action, not a calculation."
Key Takeaway: Void functions don't return values — they perform actions or modify external state through side effects, making them essential for tasks where the doing matters more than the result.