Void Functions
What you'll learn: How to write functions that perform tasks without sending back a result.
The Core Idea
Not all functions need to return a value. A void function (sometimes called a procedure) performs an action—like printing text, modifying data, or updating a display—but doesn't give anything back to the code that called it.
Think of it like asking someone to turn on the lights. They flip the switch (the action happens), but they don't hand you anything afterward. The job is complete once the action is done.
When to Use Void Functions
Use void functions when you need to:
- Display information to the user
- Modify existing data
- Perform a series of steps that don't produce a single result
- Organize repetitive tasks into reusable blocks
The Key Difference
Remember return statements from the previous lesson? Regular functions use return to send a value back:
function calculateSum(a, b)
return a + b
Void functions either have no return statement, or return nothing:
function displayWelcome()
print "Welcome to the program!"
// no return statement needed
When you call this function, it prints the message, then execution continues—but no value comes back to use in an expression.
Why They Matter
Void functions help you organize code into logical, reusable chunks. Instead of writing the same print statements five times, write one void function and call it five times. This makes your code cleaner and easier to maintain.
Key Takeaway: Void functions perform actions without returning values—they're perfect for tasks where the doing matters more than producing a result.