函式 (Functions)
函式是 Move 程式的建構區塊。它們可由使用者交易及其他函式呼叫,並將可執行程式碼組織為可重複使用的單位。函式可接受引數並回傳值。它們會在模組層級以 fun 關鍵字宣告。如同其他所有模組成員,預設為私有,只能從模組內存取;若要讓其他模組可見,將在本章稍後的可見性修飾詞章節中說明。
module book::math;
#[test_only]
use std::unit_test::assert_eq;
/// Function takes two arguments of type `u64` and returns their sum.
/// The `public` visibility modifier makes the function accessible from
/// outside the module.
public fun add(a: u64, b: u64): u64 {
a + b
}
#[test]
fun test_add() {
let sum = add(1, 2);
assert_eq!(sum, 3);
}
在此範例中,我們定義了一個 add 函式,它接受兩個 u64 型別的引數並回傳其總和。位於同一模組中的 test_add 函式是會呼叫 add 的測試函式。測試使用 assert_eq! 巨集比較 add 的結果與預期值。若兩個值不同,執行會自動中止。
函式宣告 (Function Declaration)
在 Move 中,函式通常使用 snake_case 命名慣例。這表示函式名稱應全部使用小寫字母,並以底線分隔單字。範例包括 do_something、add、get_balance、is_authorized 等。
函式會以 fun 關鍵字宣告,後接函式名稱(有效的 Move 識別字)、括號中的引數清單,以及回傳型別。函式主體是一個區塊,且如同任何區塊,沒有分號的最後一個運算式即為函式的回傳值。return 關鍵字可讓函式提早回傳;其餘控制流程運算式章節會涵蓋此內容。
fun return_nothing() {
// empty expression, function returns `()`
}
存取函式 (Accessing Functions)
如同其他模組成員,函式可透過路徑匯入及存取。該路徑由模組路徑與函式名稱組成,並以 :: 分隔。例如,若 book 套件中 math 模組內有名為 add 的函式,其完整路徑會是 book::math::add。若模組已匯入——匯入方式會在匯入模組章節中說明——你可直接以 math::add 存取它,如下列範例所示:
module book::use_math;
use book::math;
fun call_add() {
// function is called via the path
let sum = math::add(1, 2);
}
多個回傳值 (Multiple Return Values)
Move 函式可回傳多個值,當你需要從函式回傳多筆資料時尤其實用。回傳型別會指定為型別元組,回傳值則會提供為運算式元組:
fun get_name_and_age(): (vector<u8>, u8) {
("John", 25)
}
具有元組回傳值的函式呼叫結果,必須透過 let (tuple) 語法解構至變數中:
// Tuple must be destructured to access its elements.
// Name and age are declared as immutable variables.
let (name, age) = get_name_and_age();
assert_eq!(name, "John");
assert_eq!(age, 25);
若任何已宣告的值需要宣告為可變動,請將 mut 關鍵字放在變數名稱之前:
// declare name as mutable, age as immutable
let (mut name, age) = get_name_and_age();
若部分回傳值不需要,可使用 _ 符號忽略:
// ignore the name, only use the age
let (_, age) = get_name_and_age();
延伸閱讀 (Further Reading)
- Move 參考文件中的函式。