Functions, Parameters & Locals
Functions are where all WebAssembly code lives. Each function has a type (its parameters and results), a list of extra locals, and a body of instructions.
(func $name (param $p1 i32) (param $p2 f64) (result i32)
(local $tmp i32)
...body...)Parameters
Parameters are read with local.get, using their name or their index (parameters are numbered first, from 0).
(module
(import "console" "log" (func $log (param i32)))
;; area of a rectangle
(func $area (param $width i32) (param $height i32) (result i32)
(i32.mul (local.get $width) (local.get $height)))
(func (export "main")
(call $log (call $area (i32.const 3) (i32.const 4)))
(call $log (call $area (i32.const 10) (i32.const 25)))))12
250
Several unnamed parameters can be declared in one group: (param i32 i32 f64). You then refer to them as
local.get 0, local.get 1 and local.get 2.
Locals
Extra variables are declared with (local …) right after the type. Locals:
- are typed and exist only for one call of the function,
- are numbered after the parameters,
- start as zero (
0or0.0): there is no uninitialised local in Wasm.
(module
(import "console" "log" (func $log (param i32)))
;; average of three numbers, using a local for the sum
(func $average (param $a i32) (param $b i32) (param $c i32) (result i32)
(local $sum i32)
(local.set $sum (i32.add (local.get $a) (local.get $b)))
(local.set $sum (i32.add (local.get $sum) (local.get $c)))
(i32.div_s (local.get $sum) (i32.const 3)))
(func $zero_local (result i32)
(local $never_set i32)
(local.get $never_set))
(func (export "main")
(call $log (call $average (i32.const 10) (i32.const 20) (i32.const 45)))
(call $log (call $zero_local))))25
0
local.set, local.get and local.tee
| Instruction | Stack effect | Meaning |
|---|---|---|
local.get $x |
[] → [t] |
push the local's value |
local.set $x |
[t] → [] |
pop a value into the local |
local.tee $x |
[t] → [t] |
store into the local and keep the value on the stack |
local.tee ("tee" like a T-shaped pipe junction) saves a local.set + local.get pair:
(module
(import "console" "log" (func $log2 (param i32 i32)))
(func (export "main")
(local $n i32)
;; n = 6 * 7, and also use the value directly
(call $log2
(local.tee $n (i32.mul (i32.const 6) (i32.const 7)))
(i32.add (local.get $n) (i32.const 1)))))42 43
Function types and type definitions
Every function signature is stored once in the module's type section. You can name a signature and reuse it,
which becomes important for imports and call_indirect:
(module
(import "console" "log" (func $log (param i32)))
(type $binop (func (param i32 i32) (result i32)))
(func $add (type $binop) (i32.add (local.get 0) (local.get 1)))
(func $sub (type $binop) (i32.sub (local.get 0) (local.get 1)))
(func (export "main")
(call $log (call $add (i32.const 8) (i32.const 5)))
(call $log (call $sub (i32.const 8) (i32.const 5)))))13
3
When you write (param …) (result …) inline, wat2wasm adds a matching type entry for you. Identical signatures share
one entry.
Returning early
The value left on the stack at the end is the result, but return can leave a function sooner. Instructions after
an unconditional return are unreachable and are still validated, but never run.
(module
(import "console" "log" (func $log (param i32)))
(func $abs (param $x i32) (result i32)
(if (i32.lt_s (local.get $x) (i32.const 0))
(then (return (i32.sub (i32.const 0) (local.get $x)))))
(local.get $x))
(func (export "main")
(call $log (call $abs (i32.const -15)))
(call $log (call $abs (i32.const 9)))))15
9
Mistakes to avoid
- Assigning to a parameter is allowed (
local.set $xworks on parameters too), but the caller never sees the change: arguments are passed by value. - Locals must be declared before any instruction.
(local …)in the middle of the body is a syntax error. - The stack must match the result: leaving two values when the function says
(result i32)fails validation.
Test yourself
In (func (param i32 i32) (local f64) …), what is the index of the f64 local?
- 0
- 1
- 2
- It has no index
What is the value of a local that was declared but never set?
- Undefined: reading it traps
- Random garbage
- Zero
- The last value used by any function
How does local.tee differ from local.set?
- It works on globals
- It stores the value and also leaves it on the stack
- It copies a local to another local
- It only works with i64