Functions

Syntax

To declare a function, just use the keyword func.

func greet() {
    puts 'Hello'
}

Print it:

puts greet    // main@greet

Call it:

greet()    // Hello

// todo

Arrow functions

A simple arrow function:

var greet = () => {
    puts 'Hello'
}

You can print greet out:

puts greet
// fn@abcd3458

And call it:

One return expression, no brackets

More example:

The difference between two types of function is at usage.

  • Normal function can only be declared in top-level of code.

  • Arrow function can be declared everywhere.

In JavaScript, a single-param arrow function can be declared without parenthese. But here, this case is not supported.

Variadic function

A variadic function helps you to group multiple parameters to an array.

You also can pass parameters into a function call.

// todo

Notes

You cannot call a non-callable value.

A function name cannot be declared more than one time.

A function variable is immutable, so you cannot modify it.

Last updated

Was this helpful?