Skip to content

Functions

Julius Paffrath edited this page Jul 1, 2026 · 7 revisions

Introduction

A function consists of a name, a list of parameters and a body:

function greetUserWith(name: string)
    print("Hey, " + name + "!")
end

Please note: jask always uses pass by value for parameters.

Global scope

Functions can per default only read the global scope but not write to it. When setting a variable inside a function with the same name as a variable existing in global scope, the variable inside the function gets the precedence:

function testScope()
    print(myVar)
    set myVar to 2
    print(myVar)
end

set myVar to 1
testScope()
print(myVar)

As expected, this will print 121. However, with the keyword global, a function can write to a globally defined variable:

function testScope()
    print(myVar)
    set global myVar to 2
    print(myVar)
end

set myVar to 1
testScope()
print(myVar)

This will print 122.

Overloading

A function can be overloaded:

function greetUserWith(name: string, welcomeMessage: string)
    greetUserWith(name)
    print("\n" + welcomeMessage)
end

Overloading is also possible for different parameter types:

function add(str1: string, str2: string)
    return str1 + str2
end

function add(num1: number, num2: number)
    return num1 + num2
end

Named function calls

Function parameters are passed in the expected order, but, for a more readable code, named calls are also available:

function power(base: number, exponent: number)
    ; implementation goes here...
end

power(2, 4)
power(base = 2, exponent = 4) ; same call but more expressive

Clone this wiki locally