Function syntax in Move is shared between module functions and script functions. Functions inside of modules are reusable, whereas script functions are only used once to invoke a transaction.
Functions are declared with the
fun keyword followed by the function name, type parameters, parameters, a return type, acquires annotations, and finally the function body.
Module functions, by default, can only be called within the same module. These internal (sometimes called private) functions cannot be called from other modules or from scripts.
To allow access from other modules or from scripts, the function must be declared
Function names can start with letters
z or letters
Z. After the first character, function names can contain underscores
Z, or digits
After the name, functions can have type parameters
For more details, see Move generics.
Functions parameters are declared with a local variable name followed by a type annotation
We read this as
x has type
A function does not have to have any parameters at all.
This is very common for functions that create new or empty data structures
When a function accesses a resource using
borrow_global_mut, the function must indicate that it
acquires that resource. This is then used by Move's type system to ensure the references into global storage are safe, specifically that there are no dangling references into global storage.
acquires annotations must also be added for transitive calls within the module. Calls to these functions from another module do not need to annotated with these acquires because one module cannot access resources declared in another module--so the annotation is not needed to ensure reference safety.
A function can
acquire as many resources as it needs to
After the parameters, a function specifies its return type.
: u64 indicates that the function's return type is
Using tuples, a function can return multiple values
If no return type is specified, the function has an implicit return type of unit
(). These functions are equivalent
script functions must have a return type of unit
As mentioned in the tuples section, these tuple "values" are virtual and do not exist at runtime. So for a function that returns unit
(), it will not be returning any value at all during execution.
A function's body is an expression block. The return value of the function is the last value in the sequence
For more information on expression blocks, see Move variables.
Some functions do not have a body specified, and instead have the body provided by the VM. These functions are marked
Without modifying the VM source code, a programmer cannot add new native functions. Furthermore, it is the intent that
native functions are used for either standard library code or for functionality needed for the given Move environment.
native functions you will likely see are in standard library code such as
When calling a function, the name can be specified either through an alias or fully qualified
When calling a function, an argument must be given for every parameter.
Type arguments can be either specified or inferred. Both calls are equivalent.
For more details, see Move generics.
The result of a function, it's "return value", is the final value of it's function body. For example
The return value here is
double_x + double_y
A function implicitly returns the value that its body evaluates to. However, functions can also use the explicit
These two functions are equivalent. In this slightly more involved example, the function subtracts two
u64 values, but returns early with
0 if the second value is too large:
Note that the body of this function could also have been written as
if (y > x) 0 else x - y.
return really shines is in exiting deep within other control flow constructs. In this example, the function iterates through a vector to find the index of a given value:
return without an argument is shorthand for
return (). That is, the following two functions are equivalent: