The content on this page is written in Chinese, and then traslated into English by machine. More accurate traslations are welcome at: https://github.com/wa-lang/man/tree/master/en

Ending's law: "Any application that can be compiled to WebAssembly, will be compiled to WebAssembly eventually."

2.3. Function declaration

The function declaration starts with the keyword func, and the general syntax is as follows:

func FunctionName (parameter list) => (return value list) {function body}

For example:

// 版权 @2023 凹语言 作者。保留所有权利。

func swap(i, j: i32) => (i32, i32) {
    return j, i
}

func main {
    a, b := swap(4, 2)
    println("a:", a, ", b:", b)
    println(add(a, b))
}

func add(i, j: i32) => i32 {
    return i + j
}

The output of this program is as follows:

a: 2 , b: 4
6

For functions without return values, the => (return value list) part can be omitted, and for functions without input parameters, the (parameter list) part can be omitted, such as in the above example: func main {... }, which is the abbreviation of: func main() => () {...}.

Like global variables, functions can be declared in any source file within a package and do not require "declare before use".

See Chapter 4 for more information about functions.