Sui Move language - Functions
Hello everyone..
Welcome to another day in in exploring Web3 Engineering.
Since we are exploring the Move language for Sui Smart contract development. Today, let us look into the different types of functions provided by the language, function declarations, calling, visibility etc., So, without any further ado, let's get started.
Function Declaration
The move functions are declared using the fun
keyword. And the syntax is as follows:
<visibility>? fun <identifier>(arg: arg_type, ...): <return_type> {}
And example function will look like this
public fun sum(a: u128, b: u128): u128 {
a + b // returns `a+b`
}
Since move is inspired from the Rust, it also follows the same pattern for returning values. The last statement with no semicolon is implicitly considered as return statement. The language also have a return
keyword, if we want to explicitly mention it.The move functions can return
A single value
A tuple of values or multiple values using a tuple
A complex object
Function calling
When calling a function, the name can be specified either through an alias or fully qualified.
module a::example {
public fun zero(): u64 { 0 }
}
module b::other {
use a::example::{Self, zero};
fun call_zero() {
// With the `use` above all of these calls are equivalent
a::example::zero(); // package::module::function
example::zero(); // module::function - since module is imported
zero(); // function - since function is imported
}
}
Types of function
Move language has different kinds of function. They are
Regular functions
Entry functions
Friend functions - Deprecated
Regular Functions
The regular functions are the simplest functions that have any number of arguments and returns any number of arguments. They are defined as shown above.
Entry Functions
Entry functions are considered as the entry point of the execution. These are usually used to define a function that contains the core logic, and can only be called the functions in the same module. Entry functions are defined entry
and the syntax is as follows. The entry functions are by default not exposed to another contracts.
module a::m {
entry fun foo(): u64 { 0 }
fun calls_foo(): u64 { foo() }
}
Friend Functions - Deprecated
Functions in other modules can be declared as a friend
as a trusted source. The syntax is as follows. A friend is declared using the friend keyword followed by the location of the function.
module 0x42::a {
use module_b;
use module_c::function_3;
friend 0x43::function_1; // using address
friend module_b::function_2; // using alias module name
friend function_3; // using imported function name
}
Visibility
The move language supports 3 kinds of visibility specifiers. They are
Public - Any function declared as public can be called by any source. Declared using the keyword
public
.Private - The move functions by default are private. They are only accessible by the functions withing the module.
Public(Package) - The public package is a more restricted form of public visibility. It allows a function to be accessed publicly just within that package but not to outside sources. It is declared by using the keyword
public(package)
keyword.- Before the addition of public(package), public(friend) was used to allow limited public access to functions in the same package, but where the list of allowed modules had to be explicitly enumerated by the callee's module.
With that we are done with the functions on the move smart contracts. Let us look into the Struct data structure provided by the move lang in the next one.
Subscribe to my newsletter
Read articles from Jay Nalam directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by
Jay Nalam
Jay Nalam
Hi, I'm Jay Nalam, a seasoned Web3 Engineer committed to advancing decentralized technologies. Specializing in EVM-based blockchains, smart contracts, and web3 protocols, I've developed NFTs, DeFi protocols, and more, pushing boundaries in the crypto realm.