Solidity functions are declared with the function keyword, and their visibility (public, external, internal or private) decides who can call them: anyone, only other contracts and transactions, only this contract and its children, or only this contract. Together with a state mutability keyword (view, pure or payable), visibility defines a function's public interface and its gas cost. Choosing the narrowest visibility that works is the first rule of Solidity functions visibility.
This guide uses Solidity 0.8.x, the version line you should use for new contracts. You already know how to write a contract with state variables, so here you will add behaviour to it.
A function declaration has a name, parameters, a visibility, an optional mutability, optional modifiers and an optional returns list. Visibility is required for every function in Solidity 0.5 and later, so the compiler rejects a function that omits it.
increment changes state, so a call to it needs a transaction and costs gas. current only reads state, so a node can run it locally for free when you call it from outside the chain. Parameters and return values are typed, and a function can return several values by listing them in returns (uint256 a, bool b).
Visibility applies to functions and to state variables, with one difference: state variables cannot be external.
| Visibility | Called from inside the contract | Called from derived contracts | Called from other contracts or transactions |
|---|---|---|---|
public | Yes | Yes | Yes |
external | Only as this.f() | Only as this.f() | Yes |
internal | Yes | Yes | No |
private | Yes | No | No |
public and external functions form the contract's ABI, which is what wallets, front ends and other contracts see. internal and private functions are compiled as plain jumps inside the contract and never appear in the ABI.
An external function can only be reached through a message call. Calling it from inside the same contract needs the clumsy this.f() form, which makes a real external call. A public function can be called both ways.
For arguments of reference types such as arrays, external functions can read directly from calldata, which avoids copying the data into memory. Since Solidity 0.6.9, public functions may also use calldata parameters, so the old gas advantage of external is much smaller than it used to be. Use external when the function is meant only for outside callers, because it documents that intent.
internal is the default choice for helper functions that a child contract may reuse. private hides the function from child contracts too. Neither hides anything from the blockchain: the bytecode and the state are public, so visibility is about the code interface, not about secrecy.
This example shows which calls compile.
Child can call pub() and inn() directly. It can reach ext() only through this.ext(), which costs more gas because it is a real call. It cannot see priv() at all.
Mutability tells the compiler and the caller what a function may do.
view: reads state but does not change it.pure: neither reads nor changes state; it only works with its arguments and constants.payable: may receive Ether along with the call. Functions without payable revert if the caller sends value.The compiler enforces view and pure, so writing to a variable inside a view function is a compile error. Note that a view function called inside a transaction still costs gas, because the execution happens on-chain; only direct off-chain calls are free.
A public state variable gets a generated getter function with the same name, so mapping(address => uint256) public tips; above lets anyone call tips(addr). You can also define special functions without the function keyword:
constructor: runs once at deployment.receive() external payable: runs when the contract gets plain Ether with empty calldata.fallback() external: runs when no function matches the call data.public. A function like withdraw or setOwner with no access check is callable by anyone. Visibility is not access control; add a check such as require(msg.sender == owner) or a modifier.private hides data. Anyone can read private storage slots with a node call.this.f() for internal calls. It works for public functions but wastes gas and changes msg.sender to the contract itself.view when it should be pure or the reverse. The compiler warns about unneeded state access, so read the warnings.There is none. Since Solidity 0.5 you must state the visibility explicitly. For state variables the default is internal.
Use external for functions that only outside callers use, and public when the contract also calls the function itself. With calldata parameters allowed on both, the choice is mostly about intent.
No. internal and private functions are not in the ABI, so no transaction or other contract can call them directly.
Not when called directly from a wallet or script through a node. They cost gas when another transaction calls them.
Every Solidity function needs a visibility: public for everyone, external for outside callers only, internal for the contract and its children, and private for the contract alone. Add view, pure or payable to describe what the function does with state and Ether. Visibility shapes the ABI but never replaces access control, so protect sensitive functions with explicit checks.