State variables are variables whose values are permanently stored in contract storage.
pragma solidity >=0.4.0 <0.9.0;
contract SimpleStorage {
uint storedData; // state variable
// ...
}
Functions are the executable units of code. Functions are usually defined inside a contract, but they can also be defined outside of contracts.
pragma solidity >0.7.0 <0.9.0;
contract SimpleAuction {
function bid() public payable {
// ...
}
}
function helper(uint x) pure returns (uint) {
return x * 2;
}
Function modifiers can be used to amend the semantics of functions in a declarative way.
Overloading, that is, having the same modifier name with different parameters, is not possible.
Like functions, modifiers can be overridden.
pragma solidity >=0.4.22 <0.9.0;
contract Purchase {
address public seller;
modifier onlySeller() { // Modifier
require(
msg.sender == seller,
"Only seller can call this."
);
_;
}
function abort() public view onlySeller { // Modifier usage
// ...
}
}