Skip to main content

Functions in Verse: A Beginner's Guide

 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.

Popular posts from this blog

C++ vcpkg Manifest Mode + CMake

 If you've ever tried to install a C++ library and felt like you were assembling furniture without instructions, this article is for you. We're going to talk about vcpkg manifest mode and how it works with CMake , and I'm going to explain it like you're five years old (in a good way — no judgment here). First, Let's Talk About the Problem In most programming languages, adding a library is easy. Python has pip install requests . JavaScript has npm install express . You type one command, and boom, the library shows up in your project. C++ never really had that. For decades, if you wanted to use a library like fmt or nlohmann/json , you had to: Download the source code yourself Figure out how to compile it Tell your compiler where to find the headers Tell your linker where to find the compiled binaries Cry a little vcpkg is Microsoft's answer to this mess. It's a package manager for C++ — like pip or npm , but for C++ libraries. And manifest mode...

How to Set Up a Linux Web Server and Host an HTML Page Easily

Setting up a web server on Linux means spending a fair amount of time in the terminal — Linux leans heavily on the command line rather than clicking through menus, so you'll be typing out instructions more often than not.  If you're new to this, it can feel a little intimidating at first, but the good news is you don't need to become a Linux wizard overnight. A handful of core commands will get you surprisingly far. A few you'll lean on constantly: cd — move between directories ls — see what's in the current directory mkdir — create a new folder nano or vim — edit files right there in the terminal sudo — run something with administrator privileges Get comfortable with these and you'll be able to navigate around, tweak configuration files, and install software without much trouble. You don't need to memorize everything — you just need to be confident enough to follow along with clear instructions, which is exactly what this guide aims to give you....

How to Check if Someone is Connected to Your Machine in Linux

Picture this: you glance at your system monitor and notice your CPU is humming along even though you're not running anything demanding. Or maybe your internet feels sluggish for no obvious reason. A small, uneasy thought creeps in — is someone else on my machine right now? For Linux users, this isn't something you have to wonder about. Linux ships with a powerful set of built-in tools that let you see exactly who's connected, who's logged in, and what your network is doing at any given moment. You don't need to be a security expert to use them — you just need to know where to look. This guide walks you through the practical, no-nonsense steps to check for unauthorized connections on your Linux system, with real commands you can run right now. Why Monitoring Network Connections Matters Every device on a network — including your own Linux machine — communicates using an IP address. When another device or user connects to your system, that connection shows up as a trac...