Skip to content
Tutorials Center
My learning

Topics

Lesson 12 of 47 ยท Language Basics

Functions and Visibility

Learn Solidity functions: public, external, internal and private visibility, view and pure, multiple and named returns, and function overloading.

Functions are the doors into a contract. Some are open to the whole world, some only to other functions inside the same contract, and some promise not to change anything. Solidity makes you state these properties explicitly on every function, so a reader (and the compiler) can see who may call it and what it may touch. Getting this right is one of the cheapest ways to avoid security bugs.

A function header has a fixed order: name and parameters, then visibility, then state mutability, then optional modifiers, then returns. For example function total() external view returns (uint256).

Four visibilities

public Anyone, and this contract too.
external Only from outside, as a call or transaction.
internal This contract and its children.
private This contract only.

Private hides access from other code, not the data from readers.

1 / 2

Visibility

Every function needs one of four visibility keywords:

  • public: callable from outside and from inside the contract.
  • external: callable only from outside (another contract or a wallet). Calling it from inside needs this.f(), which is a real external call and costs more.
  • internal: callable from inside the contract and from contracts that inherit from it. This is the default style for helper functions, and a leading underscore is the usual naming convention.
  • private: callable only from the contract that defines it, not even from children.
SOLIDITY
contract Counter {
    uint256 private count;

    function increment() external {
        _bump(1);
    }

    function _bump(uint256 by) internal {
        count += by;
    }
}

A word of caution about private: it restricts which code may call a function or read a variable. It does not hide anything. All contract storage is public on the blockchain, and anyone can read a private variable by reading the storage slot. Never store a secret in one.

A public state variable automatically gets an external-style getter function with the variable's name, which is why a separate getCount() is usually unnecessary.

State mutability: view, pure and payable

Without a mutability keyword, a function may read and write state and does not accept ether. Three keywords change that:

  • view promises the function does not modify state. It can read storage, msg.sender, block.timestamp and so on. Modifying state, emitting events or sending ether are not allowed.
  • pure promises it neither reads nor writes state. It may only work with its arguments and constants, such as a math helper.
  • payable lets the function receive ether along with the call. Without it, a call that sends ether reverts. See Sending and Receiving Ether.
SOLIDITY
function add(uint256 a, uint256 b) public pure returns (uint256) {
    return a + b;
}

function currentCount() external view returns (uint256) {
    return count;
}

The compiler checks these promises and fails to compile if you break them, which is a good reason to add them whenever they apply. A subtle point about cost: calling a view function from outside, for example from a front end or a script, is free because the node just reads and answers. But when a transaction or another contract calls it, it runs like any other code and uses gas.

Returning values

A function declares what it returns after returns. You can return one value, several values, or none:

SOLIDITY
function stats() external view returns (uint256 min, uint256 max, uint256 total) {
    min = _min;
    max = _max;
    total = _total;
}

Here the return values are named. They behave like local variables that start at their default value (zero), and when the function ends, whatever they hold is returned. You may also use an explicit return (a, b, c); and skip the assignments. Named returns make the ABI self-documenting for tools, and they help with long return lists, but an explicit return is clearer for short functions. Pick one style per function and do not mix them in confusing ways.

The caller can unpack several values with a destructuring assignment and can skip the ones it does not need:

SOLIDITY
(uint256 lo, , uint256 sum) = stats();   // ignores the middle value

Reference types in return lists need a data location, normally memory: returns (string memory).

Function overloading

Two functions can share a name if their parameter types differ. The compiler picks one by matching the argument types at the call site:

SOLIDITY
function describe(uint256 value) public pure returns (string memory) { /* ... */ }
function describe(address account) public pure returns (string memory) { /* ... */ }

There are two limits. The return types do not take part in resolution, so you cannot overload on return type alone. And overloading can be ambiguous with implicit conversions: calling f(50) when both f(uint8) and f(uint256) exist does not compile, because the literal 50 fits both. In practice, many teams avoid overloading in contracts that tools will call, because front-end libraries need extra care to choose the right version (the selector, covered in ABI Encoding and Low-Level Calls, differs per signature).

Choosing visibility in practice

A good habit is to start with the narrowest option and widen only when needed: private or internal for helpers, external for functions that only outsiders call, and public only when both inside and outside callers need it. external is often a little cheaper than public when the arguments are large arrays, because an external function can read them straight from calldata without copying. Marking every function public by habit is a common source of accidentally exposed internals, so make the choice deliberately every time.

Stand-ins you can run

Solidity does not run here, so these TryIts are JavaScript stand-ins. The first enforces visibility rules by checking who is calling.

JS A contract that enforces visibility

Press Run (or Ctrl + Enter) to see the console output here.

No output.

The next stand-in models mutability: a view function gets a read-only copy of state, a pure function gets no state at all, and a normal function may write. It also shows named returns and destructuring.

JS view, pure and named returns

Press Run (or Ctrl + Enter) to see the console output here.

No output.

What is real: the four visibility levels, the meaning of view, pure and payable, and that the compiler enforces them. What is the stand-in: in Solidity the checks happen at compile time (or in the EVM for external calls), while the JavaScript versions check at run time, and the stand-in has no ABI, no inheritance machinery beyond a single parent link, and no gas.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. Which visibility lets a contract's own functions and its child contracts call a function, but not outside callers?

    internal is limited to the contract and contracts that inherit from it. private excludes children too, and external is only for outside callers.
  2. 2. A function reads a state variable but never changes anything. Which mutability fits?

    view allows reading state. pure would not compile because it may not read storage, and payable is about receiving ether.
  3. 3. Does marking a variable private keep its value secret?

    All contract storage is public on the blockchain. private only controls which contracts and functions can access the variable by name.
  4. 4. What are named return values?

    Naming a return makes it a local variable with a default value; whatever it holds at the end is returned. An explicit return statement works too.

Summary

  • Visibility is one of public, external, internal and private; narrow it as much as the design allows.
  • private limits code access only. Never put secrets in contract storage.
  • view reads state, pure touches no state, payable accepts ether; the compiler enforces the promises.
  • Functions can return several values, named or not, and callers can destructure them.
  • Overloading is allowed by parameter types but can be ambiguous and awkward for tooling.

Next: Control Flow, require, revert and Custom Errors.