What are Functions?

Functions are code blocks that perform specific tasks for different input parameters without repeating lines of code. For example, a function can add two numbers by taking the input and summating the numbers without repeating the same lines. This approach simplifies the process of performing tasks without repeating lines of code.

Solidity function visibility specifiers

The visibility of the function decides who can access the function, based on this there are four visibility specifiers.

  • private: accessible only inside the current contract.
  • internal: accessible inside the current contract and in child contracts.
  • external: can be accessed only outside the contract.
  • public: these are accessible from everywhere, from inside and outside of a contract, also public functions create getter functions for state variables.
Solidity function state mutability

functions in solidity have certain behavior which can be determined by the state mutability of that function, which tells us how they interact with data stored on the blockchain.

Functions in solidity can be declared as view, pure or payable.

  • View: functions that read the state (data stored on blockchain) but do not modify the state are declared as view.
  • Pure: these are neither read nor modify the state and are declared as pure functions.
  • Payable: functions declared as payable allow the function to send and receive the ether, if a it is not marked as payable it will reject the ether sent to it.
Video : Solidity Functions
Resource File
Question / Answer

internal :Those functions and state variables can only be accessed internally (i.e. from within the current contract or contracts deriving from it), without using this .

private :Private functions and state variables are only visible for the contract they are defined in and not in derived contracts.