Version: v0.5.0 - Beta.  We welcome contributors & feedback.  THanks!

Functions

Functions 

A function is a block of code that can be re-used anywhere else in the program.

When you call a function, you can pass in zero or more arguments, which are variables that are contained within that function call.

// A function with no arguments
function sayHi() {
    print('Hi!');
}

// With a 'message' argument
function saySomething(message) {
    print(message ~ '!');
}

// Call the functions
sayHi();                //= prints 'Hi!'
saySomething('Hello');  //= prints 'Hello!'
saySomething();         // ✖ ERROR - Needs one argument.

Default Arguments

Arguments can have default values if they aren’t passed in.

// The 'message' argument has a default value
function saySomething(to, message = 'Hello') {
    print(message ~ ', ' ~ to ~ '!');
}

saySomething('Taylor');
//= 'Hello, Taylor!'

saySomething('Taylor', 'Hey');
//= 'Hey, Taylor!'

Template Functions 

Template functions let you include multi-line strings directly in your script.

These are mainly used for blocks of HTML, CSS, and JavaScript.

They support extra syntax, like double-braces {{ ... }} for embedding THT expressions.

See Templates for more info.

template html(userName) {

    <h1>My Web Page</h1>

    <p>Hello {{ userName }}!</p>

}

Scope 

Variables can only be accessed inside the block that they are defined in.

A block is pretty much anything with a set of curly braces: e.g. functions, if/else blocks, and for loops.

Function Scope

Variables in one function are not available in other functions.

function doSomething() {
    let myVar = 123;
}

function doSomethingElse() {
    // ✖ ERROR - in a different function
    print(myVar);
}

Top-Level Scope

Variables in the top-most scope (i.e. outside of all functions) are not available inside functions.

// in the top-level scope
let myVar = 123;

function doSomething() {
    // ✖ ERROR - inside a function
    print(myVar);
}

Block Scope

Variables in inner blocks are not available in outer blocks.

function doSomething() {

    if (true) {
        // inner scope
        let myVar = 123;
    }

    // ✖ ERROR - outer scope
    print(myVar);
}