5.1 Public functions
Public functions can be called by both standard and contract principals. At least one public function is required for contracts with interactivity. However, being callable does not necessarily mean that the underlying functionality is public. Developers can include assertions to ensure that only specific contract callers or inputs are valid.
Public functions should return a response value. If the function returns an 'ok' type, the function call is considered valid, and any changes made to the blockchain state will be materialized. This means that state changes, such as updating variables or transferring tokens, will only be committed to the chain if the contract call that triggered these changes returns an 'ok'.
The effects of returning an 'ok' or an 'err' are illustrated in the example below. The function takes an unsigned integer as input and returns 'ok' if it is even or 'err' if it is odd. At the start of the function, the variable 'even-values' is incremented. To determine if the number is even, we calculate the remainder of the division by two and check if it is equal to zero using the 'is-eq' function (n mod 2 should equal 0).
Last updated