If loops are about doing something over and over, functions are about doing something on demand — whenever you call for it. They're one of the most important building blocks in Verse, so let's break them down in plain English, with real code you can actually picture running.
What's a Function, Really?
Think of a function like a vending machine. You put something in (optional), you press a button (you "call" it), and it gives you something back out (optional). You don't need to know exactly how the inside of the machine works every time — you just need to know what to put in and what you'll get out.
In programming terms: a function is a named, reusable chunk of code that you can run whenever you need it, instead of retyping the same logic over and over.
The Basic Shape of a Verse Function
Here's the simplest possible function in Verse:
SayHello() : void =
Print("Hello!")
Let's break that down piece by piece:
SayHello— this is the name of the function. You get to pick this.()— these parentheses hold any "ingredients" (called parameters) the function needs. This one needs nothing, so it's empty.: void— this tells Verse "this function doesn't hand anything back when it's done." It just does something.=— this kicks off the function's body, i.e. what it actually does.Print("Hello!")— the actual instruction that runs when you call this function.
To actually run it somewhere else in your code, you just write:
SayHello()
That's it. Every time you write SayHello(), Verse jumps into that function, runs the Print line, and comes back.
Giving a Function Ingredients (Parameters)
Most useful functions need some information to work with. These are called parameters, and they go inside the parentheses.
Greet(Name : string) : void =
Print("Hello, {Name}!")
Now when you call it, you feed it a value:
Greet("Alex")
# Prints: Hello, Alex!
Name : string means "this function expects one ingredient called Name, and it has to be text (a string)." If you tried to pass in a number instead of text, Verse would stop you — it's picky about types like that, which actually helps you catch mistakes early.
You can ask for more than one ingredient too:
Greet(FirstName : string, LastName : string) : void =
Print("Hello, {FirstName} {LastName}!")
Greet("Alex", "Rivera")
# Prints: Hello, Alex Rivera!
Getting Something Back (Return Values)
So far, our functions just print stuff — they don't hand anything back. But often you want a function to calculate something and give you the result, like a vending machine that actually drops a snack instead of just making noise.
Double(Number : int) : int =
return Number * 2
Here, : int after the parentheses means "this function will hand back a whole number when it's done." The return keyword is what actually sends that value back out.
You'd use it like this:
Result := Double(5)
Print("{Result}")
# Prints: 10
Result grabs whatever Double(5) handed back — in this case, 10.
A Shortcut: Skipping return
Verse actually lets you skip the word return in simple functions. If the last line of your function is just a value or expression, Verse automatically treats that as the answer to hand back:
Double(Number : int) : int =
Number * 2
This does exactly the same thing as the version above. Less typing, same result.
Functions That Might Fail
This is one of the more unique things about Verse. Because Verse is built with game logic in mind, it has a built-in idea of "this operation might not succeed" — for example, checking if a player has an item, or grabbing something from a list that might be empty.
Functions like this use a special marker called <decides>:
GetFirstPlayer(Players : []player)<decides> : player =
Players[0]
That <decides> tag is Verse's way of saying "heads up — this function might fail instead of giving you an answer." If the array Players is empty, trying to grab Players[0] simply fails instead of crashing your game. You then handle that possibility wherever you call the function, usually with an if:
if (FirstPlayer := GetFirstPlayer(Players)):
Print("Found a player!")
else:
Print("No players found.")
You don't need to master this right away — just know that when you see <decides> on a function, it means "this one might come back empty-handed, so plan for that."
Functions That Take Time (Async)
Some functions don't finish instantly — like waiting a few seconds before spawning an enemy, or waiting for a door to open. These use the <suspends> marker, and you call them with the word async:
WaitThenGreet()<suspends> : void =
Sleep(3.0)
Print("Three seconds later...")
You'd typically run something like this inside an async block elsewhere in your code, so the rest of your game doesn't freeze while it waits.
Why Bother With Functions At All?
A few reasons this matters, especially once your project grows past a few lines:
- You stop repeating yourself. Write the logic once, call it as many times as you want.
- Your code gets easier to read.
Greet("Alex")tells you exactly what's happening without needing to see the details every time. - Bugs get easier to fix. If something's wrong with how greetings work, you only need to fix it in one place — the function — instead of everywhere you copy-pasted the logic.