Functions, Visibility and State Mutability
A function's declaration decides who can call it, what it may do and whether it can receive ether. This lesson explains visibility, state mutability, return values and the automatic getters of public variables.
Learning outcomes
After completing this lesson, you will be able to:
- Distinguish public, external, internal and private visibility.
- Distinguish view, pure, payable and non-payable functions.
- Explain why private data is still publicly readable.
- Describe the getters generated for public state variables.
Function syntax
A function declaration combines a name, parameters, visibility, state mutability, modifiers and return types: function transfer(address to, uint256 amount) external returns (bool). Functions can return several values, returns (uint256 total, bool ok), and callers can destructure them: (uint256 t, ) = f();.
Visibility
The following table summarises the four visibility levels.
| Visibility | Callable from outside | Callable inside the contract | Callable by derived contracts | Allowed for state variables |
|---|---|---|---|---|
| public | Yes | Yes | Yes | Yes (creates a getter) |
| external | Yes | Only as this.f() | Only as this.f() | No |
| internal | No | Yes | Yes | Yes (the default) |
| private | No | Yes | No | Yes |
Visibility controls which code can call a function or access a variable through Solidity. It does not hide data: anyone can read a private variable's slot directly from the chain with eth_getStorageAt. Secrets such as passwords must never be stored in a contract, even as private.
A vault with visibility mistakes
State mutability
State mutability declares what a function does to state, and the compiler enforces it. The following terms describe the options.
- view
- Reads state but does not modify it: no writes, no events, no ether sent, no state-changing calls.
- pure
- Neither reads nor modifies state; it works only on its inputs, such as a mathematical helper.
- payable
- May receive ether;
msg.valueholds the amount. - (none)
- May modify state but rejects ether sent with the call.
Calling a view or pure function from outside, with eth_call, costs nothing. Called inside a transaction, the same function's gas is part of the transaction's cost.
pure, view or neither?
Public getters
Declaring a state variable public makes the compiler generate an external view getter with the same name. For uint256 public totalSupply; the getter is totalSupply(); for mapping(address => uint256) public balances; it is balances(address); for a public array it takes an index and returns one element, not the whole array.
Generated getters
Choosing visibility
A contract has a helper _calculateFee(uint256) used only by its own functions and those of a contract that inherits from it, and a function deposit() that users call with ether. Which declarations fit?
function _calculateFee(uint256 amount) internal pure returns (uint256): internal because derived contracts need it but outsiders must not call it, and pure if it only computes from its input. function deposit() external payable: external because only users call it, and payable because it receives ether; without payable, every deposit would revert.
A "hidden" answer
A quiz contract stores the answer in bytes32 private answer; and pays a prize to whoever submits it. Within minutes of deployment, someone claims the prize. How?
private stops other contracts from reading the variable through Solidity, but the value is in the contract's storage, which is public. The attacker read the slot with eth_getStorageAt (or from the deployment transaction's data) and submitted it. A secure design stores only a hash of the answer and uses a commit-reveal scheme so that submissions cannot be copied from the mempool.
Which visibility lets derived contracts call a function but stops external callers?
Show answer
internal. private would also stop external callers but would hide the function from derived contracts too.
Exam Tips
Remember for the exam
- public and external functions are callable from outside; internal and private are not.
- private does not make data secret; all storage is publicly readable.
- view reads state, pure reads nothing, payable accepts ether.
- Public state variables get automatic getter functions.