Prerequisites

Before starting this tutorial, make sure you have Simplex installed on your system. If you haven't installed it yet, follow the installation guide.

Your First Program

Let's start with the classic "Hello, World!" program. Create a new file called hello.sx with the following content:

hello.sx
fn main() {
    print("Hello, World!")
}

Running the Program

Open your terminal and run:

Terminal
$ sxc run hello.sx
Hello, World!

Understanding the Code

Let's break down what we wrote:

  • fn main() - Declares the main function, the entry point of every Simplex program
  • { } - Curly braces define the function body
  • print("...") - A built-in function that outputs text to the console
  • "Hello, World!" - A string literal enclosed in double quotes

No Semicolons

Notice there's no semicolon at the end of the line. Simplex uses newlines as statement separators, making code cleaner and more readable.

Variables

Let's make our program more dynamic by using variables:

variables.sx
fn main() {
    let name = "Simplex"
    let version = 1

    print("Welcome to {name} v{version}!")
}

Key concepts:

  • let - Declares an immutable variable (cannot be changed)
  • {name} - String interpolation: embed variables directly in strings
  • Type inference: Simplex figures out that name is a String and version is an i64

Mutable Variables

If you need to change a variable's value, use var instead:

mutable.sx
fn main() {
    var counter = 0

    counter += 1
    print("Counter: {counter}")

    counter += 1
    print("Counter: {counter}")
}

Functions

Let's create a function that greets a person:

functions.sx
fn greet(name: String) -> String {
    "Hello, {name}!"
}

fn main() {
    let message = greet("Developer")
    print(message)
}

Function anatomy:

  • fn greet - Function declaration with name
  • (name: String) - Parameter with explicit type annotation
  • -> String - Return type
  • No return keyword needed - the last expression is returned automatically

Try It Yourself

Create a function called add that takes two i64 numbers and returns their sum. Then call it from main and print the result.

Control Flow

Simplex supports standard control flow constructs:

control-flow.sx
fn main() {
    let temperature = 22

    // If expressions
    let status = if temperature > 30 {
        "hot"
    } else if temperature > 20 {
        "comfortable"
    } else {
        "cold"
    }

    print("It's {status} today")

    // For loops
    for i in 1..5 {
        print("Count: {i}")
    }
}

Summary

In this tutorial, you learned:

  • How to create and run a Simplex program
  • Variables with let (immutable) and var (mutable)
  • String interpolation with {variable}
  • Function definitions with parameters and return types
  • Basic control flow with if expressions and for loops

In the next tutorial, we'll dive deeper into Simplex's type system and learn about structs, enums, and pattern matching.