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.

import Nat "mo:base/Nat";

actor {
let _numbers1 : [Nat] = [1, 2, 3, 4, 5, 6, 7]; // Immutable array
let _numbers2 : [var Nat] = [var 1, 2, 3, 4, 5, 6, 7] ; // Mutable array

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

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.