Structure of a Contract
Contracts in Solidity are similar to classes in object-oriented languages. Each contract can contain declarations of State Variables , Functions , Function Modifiers , Events , Struct Types and Enum Types . Furthermore, contracts can inherit from other contracts.
There are also special kinds of contracts called librariesarrow-up-right and interfacesarrow-up-right .
The section about contractsarrow-up-right contains more details than this section, which serves to provide a quick overview.
State Variables
State variables are variables whose values are permanently stored in contract storage.
Copy pragma solidity >=0.4.0 <0.6.0;
contract SimpleStorage {
uint storedData; // State variable
// ...
} See the Typesarrow-up-right section for valid state variable types and Visibility and Gettersarrow-up-right for possible choices for visibility.
Functions are the executable units of code within a contract.
Copy pragma solidity >=0.4.0 <0.6.0;
contract SimpleAuction {
function bid() public payable { // Function
// ...
}
} Function Callsarrow-up-right can happen internally or externally and have different levels of visibilityarrow-up-right towards other contracts. Functionsarrow-up-right accept parameters and return variablesarrow-up-right to pass parameters and values between them.
Function Modifiers
Function modifiers can be used to amend the semantics of functions in a declarative way (see Function Modifiersarrow-up-right in the contracts section).
Events are convenience interfaces with the EVM logging facilities.
See Eventsarrow-up-right in contracts section for information on how events are declared and can be used from within a dapp.
Structs are custom defined types that can group several variables (see Structsarrow-up-right in types section).
Enums can be used to create custom types with a finite set of ‘constant values’ (see Enumsarrow-up-right in types section).