Lesson 3 of 4 Solidity Language Fundamentals

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.

VisibilityCallable from outsideCallable inside the contractCallable by derived contractsAllowed for state variables
publicYesYesYesYes (creates a getter)
externalYesOnly as this.f()Only as this.f()No
internalNoYesYesYes (the default)
privateNoYesNoYes

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.

Spot the bug

A vault with visibility mistakes

Exam skills 2.1.3

Find the lines whose visibility or mutability makes this contract unsafe or broken.

Log in to try this activity Interactive activities are free with an account.

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.value holds 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.

Sort into groups

pure, view or neither?

Exam skills 2.1.3

Choose the strictest state mutability each function can be declared with.

Log in to try this activity Interactive activities are free with an account.

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.

Scenario questions

Generated getters

Exam skills 2.1.3

Predict the getter the compiler generates for public state variables.

Log in to try this activity Interactive activities are free with an account.
Worked example

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.

Worked example

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.

Check yourself

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.