Functions are reusable code that executes by the compiler.

Solidity functions execute the code In blockchains.

Solidity Functions do the following things

  • update the state of an

What is a Function in Solidity

Each contract in the blockchain has a state or data. Every operation in the blockchain is a transaction. transactions involve calling functions, that execute code to modify the state of a contract.

That means, calling functions creates a transaction in the blockchain.

Functions can read or write the state of a contract.

Functions on-demand code to execute the code in blockchain contracts.

The function involves two parts

  • declare functions
  • calling functions

Generally, functions take parameters from the caller, execute the code, and return the data to the caller.

Here is a function syntax for the declaration

function functionname() <visibility-specifier> <modifier> returns (type) {
return typevalue;
}
  • function is a keyword in solidity
  • function-name: Name of the function and valid identifier in solidity
  • visibility: function visibility how the function is visible to the caller
  • modifier: declare the function to change the behavior of contract data
  • returns: what is the type of data that returns to the caller

Function visibility specifier

Visibility to a function specifies how the function is visible to the caller either internally or external.

The following are valid visibility

  • public: - public functions are visible from internal and external contracts. - Can be called inside a contract or outside contract - Default is public - It is visible to the public, so security concerns.

  • private: - private functions are visible inside the same contract - Can be called inside a contract, Not able to call from an outside contract

  • internal: - Similar to private functions, and visible to contract hierarchy - Can be called inside a contract and Contracts that inherit from it.

  • external:

    • external functions are visible to public functions.
    • Can be called outside contract only.

Function Modifier or qualifiers

Modifiers allows you to define the functions with behavior for changing the state of contract data.

  • constant:

    • Does not change the state of contract functions
    • Read the state global variables
    • Modification of state is not possible
    • used for getter of a state variable calling events or functions that modify the state,
  • view:

    • The view functions display the data.
    • functions do not modify the state variable,
    • Suggest using for reading data operations
  • pure:

    • pure functions do not access data from the blockchain
    • does not allow read or write operations
    • No access to state data or transaction data
    • return type based on the data from a function
  • payable :

    • payable functions allow accepting ether from a caller
    • Function fails if the sender has not provided ether
    • Other functions do not accept ether, only payable functions allow it.

Function return value

Function return values are returned from a function code execution to the caller.

It can be any type returned from the function.