Functions

an informal syntax of function declaration can defined as:

function <identifier> ( <ty> <identifier> ) <modifiers> return <return decl> { ... }
 
<modifiers>  := <visibility> <mutability?> <inheritance modifier?> <custom modifier*>
<visibility> := public | external | internal | private
<mutability> := view | pure | payable
<inheritance modifier> := virtual | override
<custom modifier> := <identifier>

where:

  • <x*> indicates zero or more
  • <x?> indicates optional
  • <x+> indicates one ore more

Visibility (aka. Function types)

Function types define the visibility of a function

  • public - anyone can access
  • external - cannot be accessed internally, only externally
  • internal - only this contract and the contract that inherit this contract
  • private - can be accessed only from this contract

Note

The visibility flag is optional at and before solidity 0.7 and default to public if not explicitly stated.

Mutability

  • pure function declares only pure computation is involved, no state variable will be read or written.
  • view function is readonly over state variable
  • non-payable is default mutability (can’t be explicitly stated), can read and modify the state variable
  • payable function means it can receive ether, beside the state variable, it can also access the global variables (e.g. msg).