Solidity functions are the building blocks of smart contracts, allowing you to define the behavior and logic of your decentralized applications. They can be called internally or externally, and their visibility and state permissions can be controlled through modifiers. Understanding how to properly implement functions is crucial for creating efficient and secure smart contracts.

Here are some basic function examples:

Addition โž•

function add(uint a, uint b) public pure returns (uint){
  return a + b;
}

Message ๐Ÿ“

function add(uint a, uint b) public pure returns (uint){
  return a + b;
}

Solidity Visibility Modifiers ๐Ÿ‘๏ธ

  • private ๐Ÿ”’ : Only accessible within the current contract
  • internal ๐Ÿ” : Accessible within the current contract and inherited contracts
  • external ๐ŸŒ : Only callable from outside the contract (via transactions or other contracts)
  • public ๐Ÿ”“ : Accessible from anywhere (no restrictions)

State Permission Modifiers โšก

If you don’t specify pure/view, the function will consume gas. Keep this in mind!

  • pure ๐Ÿงฎ : Only references variables. No gas cost
  • view ๐Ÿ“Š : Performs calculations and returns results. No blockchain state changes, no gas cost