Sui Move language - Functions
Functions and different types of function, visibility etc.

Search for a command to run...
Functions and different types of function, visibility etc.

No comments yet. Be the first to comment.
In this series, I will cover the move language and smart contract development on the Sui blockchain network.
Everything we need to know about structs
All about perpetual contracts

A Technical Guide for Beginners to Ethereum's Proof of Stake

Transaction and type of Transactions in the Ethereum blockchain

Structure of an Ethereum Node

Update to ERC1155

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.
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
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
}
}
Move language has different kinds of function. They are
Regular functions
Entry functions
Friend functions - Deprecated
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 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() }
}
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
}
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.
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.