Functions

Functions can return multiple values as a tuple.

14.1 Syntax

Functions can return multiple values as a tuple. Tuples are not first-class values: you destructure them immediately (let (x, y) = swap(1, 2);).

fun add(a: u64, b: u64): u64 { a + b }                     // private
public fun swap(a: u64, b: u64): (u64, u64) { (b, a) }    // returns a tuple
fun no_return() { }                                          // returns ()

14.2 Visibility table

Same module only Modules in same package public(friend) / friend fun Declared friend modules

Any module; on Sui also directly from PTBs Yes (not directly from transactions unless entry) Directly from transactions public entry Both from modules and transactions

Allowed, but plain public is usually preferred on Sui

14.3 Entry functions

Aptos: only entry functions (or scripts) can be the target of a transaction. Parameters are restricted to &signer, primitives, String, vectors of these, Object<T>, and Option. Entry functions cannot return values.

Sui: any public function can be called from a PTB, and its return values can be fed to later commands. entry (non-public) functions can be called from transactions but not from other modules, which is useful for functions that must not be composed (e.g., randomness consumers). Since 2024, entry functions may return values only if they have drop.

// Aptos
public entry fun transfer_apt(from: &signer, to: address, amount: u64) {
    aptos_framework::aptos_account::transfer(from, to, amount);
}

// Sui
entry fun roll_dice(r: &sui::random::Random, ctx: &mut TxContext) { /* ... */ }

14.4 View functions (Aptos)

View functions are callable off-chain through the REST API/SDK without a transaction. On Sui, read state by fetching objects or with a dry-run/devInspect call.

#[view]
public fun balance_of(owner: address): u64 { /* read-only */ 0 }

14.5 Inline functions

Sui Move uses macro functions (macro fun) for the same purpose (Chapter 22). Aptos Move 2.2+ also supports first-class function values (closures stored in variables and, with abilities, in structs).

// Aptos Move: inline functions are expanded at the call site and can take lambdas
inline fun apply_twice(x: u64, f: |u64| u64): u64 { f(f(x)) }

public fun demo(): u64 { apply_twice(3, |v| v * 2) }   // 12

14.6 The acquires annotation (Aptos)

A function that accesses global storage of a resource type declared in its own module (via borrow_global, borrow_global_mut, move_from) historically had to declare acquires T. Callers that transitively access it also needed the annotation. Recent Aptos Move 2 compilers infer acquires, but you will see it in most existing code:

public fun get(addr: address): u64 acquires Counter {
    borrow_global<Counter>(addr).value
}

14.7 Native functions

Framework modules declare native fun functions implemented in the VM (hashing, signature verification, BCS serialization). Application code cannot declare natives.