Function Definition

You define functions with the function method with the following signature:

function :function_name, {arg1: :type1, arg2: :type2, ...}, *options, returns: return_type do
  # function body

For example:

function :tokenURI, { id: :uint256 }, :public, :view, :override, returns: :string do
  require(_exists(id: id), 'ERC721Metadata: URI query for nonexistent token')
  # ...


  • :function_name - The name of the function

  • {arg1: :type1, arg2: :type2, ...} - A hash of argument names to types.

  • *options - Zero or more flags that specify additional function characteristics like visibility (:public, :private), state mutability (:view, :pure), etc.

  • returns: return_type - Specifies the return type.

Inside the Function Body

  • s.variableName can be used to read from and write to state variables.

  • If you write an identifier like variableName without the s. prefix, the system will look for it among the function's arguments first, and then among the contract's functions.

Automatically Generated Functions

If you have a public state variable, a getter function is automatically generated. So, there's no need to write a separate getter function for that variable.

Last updated