Skip to main content

Passing arguments

Beginner
Tutorial

Overview

This document aims to provide examples of passing different argument types to a canister.

Arrays

An array is a collection of items of the same data type.

The following example defines an array of numbers and a function that returns the array.

Motoko differentiates between immutable arrays, which cannot be altered, and mutable arrays, which can be modified.

To declare an immutable array (default):

let a : [Nat] = [0, 1, 2, 3];

To declare a mutable array, the [var _] syntax is used:

let a : [var Nat] = [var 0, 1, 2, 3] ;

To declare a function that returns an array:

public func get_numbers(a: [Nat]) : [Nat] {
return a;
}

The Array Motoko base library provides utility functions on arrays. To learn more about the Array Motoko base library, refer to the Motoko base library reference on Array and the Motoko language quick reference on arrays.