Aptos Move by Example
  • 🚀Getting Started
  • Set-Up
  • Why is Move Secure
    • Move prover
  • Move vs Solidity
    • Resources
    • Parallel Processing
    • Reentrancy attacks
    • Memory management
    • Smart contract verification
    • Compiled language
  • Basic Concepts
    • Move.toml
    • Primary data-types
    • Strings
    • Comments
    • Functions
    • Function Visibilities
    • Control flow and expressions
    • Loops
    • Error
    • Struct and its Abilities
    • Scripts
    • Operations
  • Intermediate Concepts
    • Local variables
    • Constants
    • Signer
    • Vector
    • Address
    • Uses and Aliases
    • Maps
    • Hash functions
    • References
    • Unit test
    • Generics
    • Type Arguments
    • Type Inference
  • Advanced Concepts
    • Global Storage Structure
    • Global Storage Operations
    • Phantom Type Parameters
    • Timestamps
    • Ownership
    • Move coding conventions
    • View functions
    • Aptos account
    • Aptos Coin
    • Aptos Token(Nft)
    • Object
    • Token V2
  • Applications
    • First App
    • ToDoList
    • Voting System
    • Basic Tokens
    • Storage using Generics
    • Company
    • Collection
    • Football Card
    • Staking Module
    • MultiSender Wallet
    • English Auction
    • Dutch Auction
    • Attendance Sheet
    • Polling Contract
    • Lottery Contract
  • Decentralized Finance
    • Simple Swap Protocol Contract
    • Code of Swapping Protocol
  • Hacks
    • Coming soon
  • Hands on tutorials
    • Indexer tutorials
Powered by GitBook
On this page
Edit on GitHub
  1. Basic Concepts

Function Visibilities

Functions in move can be declared as:

private - By default the functions in move are private(that means they can only be called within the same module and cannot be access outside the module by other modules and scripts.

public - A public function can be called by any function in any module or script.

public(friend) - A public(friend) function can be called by any function in the same module and by the function of the module which are explicitly defined in the friend list.

entry - Entry function are the function where move program starts its execution. They can be combined with public and public(friend) modifier.

address my_addrx{
module A{
    fun A_foo():u8{    //private function
        return 123
    }
}

module B{
    use std::debug::print;
    fun B_foo():u8{
        return my_addrx::A::A_foo()  //ERROR - as A_foo() is a private function
    }

    #[test]
    fun testing_B()
    {
        let number=B_foo();
        print(&number);
    }
}
}
address my_addrx{
module A{
    public fun A_foo():u8{    //public function
        return 123
    }
}

module B{
    use std::debug::print;
    fun B_foo():u8{
        return my_addrx::A::A_foo()  //Module B can call A_foo() as it is a public function
    }

    #[test]
    fun testing_B()
    {
        let number=B_foo();
        print(&number);
    }
}
}
address my_addrx{
module X{
    friend my_addrx::Y; //declaring friends of module Y

    public(friend) fun X_foo():u8{
        return 123
    }   
}

module Y{
    fun Y_foo():u8{
        return my_addrx::X::X_foo() //Module Y can call X_foo() as Y is in friend list of X
    }
}

module Z{
    fun Z_foo():u8{
        return my_addrx::X::X_foo() //ERROR - as module Z is not in friend list of X
    }   
}
}
module my_addrx::E{
    use std::debug::print;
    use std::string::utf8;
    public entry fun foo(){   //entry function can be private,public or public(friend)
        print(&utf8(b"This is an entry function"));
    }
    #[test]
    fun testing()
    {
        foo();
    }
}
PreviousFunctionsNextControl flow and expressions

Last updated 2 years ago