Skip to content

Functions

Sxtanna edited this page Jul 8, 2020 · 1 revision

Function definitions are slightly more complicated with between 3 and 6 values.

Syntax

fun 'name'{(arguments)}{: 'return type'} {
  'function body'
  {=> 'return expression'}
}

  1. fun
  • All functions start with the fun keyword.
  1. name
  • Function names must follow lowerCamelCase naming standards.
  1. arguments (optional)
  • Functions do not have to accept arguments, in which case parentheses can be omitted.
  • Arguments of the same type that follow each other can have their type omitted until the last value.
  1. return type (optional)
  • Functions do not have to return a value, in which case the type can be omitted.
  1. function body
  • Function bodies can be entirely empty or contain any number of properties, expressions, and control flow.
  1. return expression
  • Return expressions must be the final declaration of a function that returns a value, starting with =>

Examples

function without arguments or a return value:

fun pushHelloWorld {
  push "Hello World"
}

function with both arguments and a return value:

fun addTwoNumbers(arg0: Int, arg1: Int): Int {
  => arg0 + arg1
}

// can also be written as

fun addTwoNumbers(arg0, arg1: Int): Int {
  => arg0 + arg1
}