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