# Structure of a Contract

## Structure of a Contract

Contracts in Solidity are similar to classes in object-oriented languages. Each contract can contain declarations of [State Variables](https://pliangroup.gitbook.io/plian/master/for-developers/solidity/solidity-in-depth/broken-reference), [Functions](https://pliangroup.gitbook.io/plian/master/for-developers/solidity/solidity-in-depth/broken-reference), [Function Modifiers](https://pliangroup.gitbook.io/plian/master/for-developers/solidity/solidity-in-depth/broken-reference), [Events](https://pliangroup.gitbook.io/plian/master/for-developers/solidity/solidity-in-depth/broken-reference), [Struct Types](https://pliangroup.gitbook.io/plian/master/for-developers/solidity/solidity-in-depth/broken-reference) and [Enum Types](https://pliangroup.gitbook.io/plian/master/for-developers/solidity/solidity-in-depth/broken-reference). Furthermore, contracts can inherit from other contracts.

There are also special kinds of contracts called [libraries](https://psolidity.readthedocs.io/en/latest/contracts.html#libraries) and [interfaces](https://psolidity.readthedocs.io/en/latest/contracts.html#interfaces).

The section about [contracts](https://psolidity.readthedocs.io/en/latest/contracts.html#contracts) 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.

```
pragma solidity >=0.4.0 <0.6.0;

contract SimpleStorage {
    uint storedData; // State variable
    // ...
}
```

See the [Types](https://psolidity.readthedocs.io/en/latest/types.html#types) section for valid state variable types and [Visibility and Getters](https://psolidity.readthedocs.io/en/latest/contracts.html#visibility-and-getters) for possible choices for visibility.

### Functions

Functions are the executable units of code within a contract.

```
pragma solidity >=0.4.0 <0.6.0;

contract SimpleAuction {
    function bid() public payable { // Function
        // ...
    }
}
```

[Function Calls](https://psolidity.readthedocs.io/en/latest/control-structures.html#function-calls) can happen internally or externally and have different levels of [visibility](https://psolidity.readthedocs.io/en/latest/contracts.html#visibility-and-getters) towards other contracts. [Functions](https://psolidity.readthedocs.io/en/latest/contracts.html#functions) accept [parameters and return variables](https://psolidity.readthedocs.io/en/latest/contracts.html#function-parameters-return-variables) 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 Modifiers](https://psolidity.readthedocs.io/en/latest/contracts.html#modifiers) in the contracts section).

```
pragma solidity >=0.4.22 <0.6.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
        // ...
    }
}
```

### Events

Events are convenience interfaces with the EVM logging facilities.

```
pragma solidity >=0.4.21 <0.6.0;

contract SimpleAuction {
    event HighestBidIncreased(address bidder, uint amount); // Event

    function bid() public payable {
        // ...
        emit HighestBidIncreased(msg.sender, msg.value); // Triggering event
    }
}
```

See [Events](https://psolidity.readthedocs.io/en/latest/contracts.html#events) in contracts section for information on how events are declared and can be used from within a dapp.

### Struct Types

Structs are custom defined types that can group several variables (see [Structs](https://psolidity.readthedocs.io/en/latest/types.html#structs) in types section).

```
pragma solidity >=0.4.0 <0.6.0;

contract Ballot {
    struct Voter { // Struct
        uint weight;
        bool voted;
        address delegate;
        uint vote;
    }
}
```

### Enum Types

Enums can be used to create custom types with a finite set of ‘constant values’ (see [Enums](https://psolidity.readthedocs.io/en/latest/types.html#enums) in types section).

```
pragma solidity >=0.4.0 <0.6.0;

contract Purchase {
    enum State { Created, Locked, Inactive } // Enum
}
```
