Functions & Visibility
Functions are the entry points of a contract. Their declaration says who may call them (visibility), whether they touch state (state mutability, next lesson), what they take and what they return.
function name(uint256 a, address b) external view returns (uint256 result) { … }
// name parameters visibility mutability return valuesParameters and multiple return values
A function can return several values as a tuple. Callers unpack them with a destructuring assignment, and can skip values they don't need with an empty slot:
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.24;
import "hardhat/console.sol";
contract Main {
function divide(uint256 a, uint256 b) public pure returns (uint256 quotient, uint256 remainder) {
quotient = a / b;
remainder = a % b;
}
function stats(uint256 x, uint256 y, uint256 z) public pure returns (uint256, uint256, uint256) {
uint256 lo = x < y ? (x < z ? x : z) : (y < z ? y : z);
uint256 hi = x > y ? (x > z ? x : z) : (y > z ? y : z);
return (lo, hi, (x + y + z) / 3);
}
function main() external pure {
(uint256 q, uint256 r) = divide(47, 6);
console.log("47 / 6 =", q, "remainder", r);
(, uint256 highest, ) = stats(4, 19, 7); // keep only the middle value
console.log("highest:", highest);
}
}47 / 6 = 7 remainder 5
highest: 19
Named arguments
For functions with many parameters, call them with {name: value} syntax, which protects you from mixing up the order:
transfer({to: recipient, amount: 100});Visibility
| Visibility | Callable from outside (transactions, other contracts) | Callable inside the contract | Callable from derived contracts |
|---|---|---|---|
external |
yes | only as this.f() (an external call) |
only as this.f() |
public |
yes | yes | yes |
internal |
no | yes | yes |
private |
no | yes | no |
Functions must declare their visibility explicitly. State variables can be public, internal (the default) or private, but not external.
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.24;
import "hardhat/console.sol";
contract Shop {
uint256 private sold;
function buy(uint256 qty) external {
_record(qty); // internal helper
}
function price(uint256 qty) public pure returns (uint256) {
return qty * 250 - _discount(qty);
}
function _record(uint256 qty) internal {
sold += qty;
}
function _discount(uint256 qty) private pure returns (uint256) {
return qty >= 10 ? qty * 25 : 0;
}
function totalSold() external view returns (uint256) {
return sold;
}
}
contract Main {
function main() external {
Shop shop = new Shop();
shop.buy(3);
shop.buy(12);
console.log("sold :", shop.totalSold());
console.log("price(4) :", shop.price(4));
console.log("price(12):", shop.price(12));
// shop._record(1); // compile error: internal functions are not visible from Main
}
}sold : 15
price(4) : 1000
price(12): 2700
Try removing the // in front of shop._record(1); and run again: the compiler refuses, because _record is internal.
Warning:
privateandinternalrestrict code access, not reading. All storage is public on a blockchain, so never store secrets (passwords, private keys) in a contract.
external vs public
Both can be called from outside. external functions can't be called internally by name, but they can read array and string arguments directly from the call data (calldata), which is slightly cheaper. A common rule: use external for functions only meant to be called from outside, public when you also call them internally.
Internal calls vs external calls
Calling price(4) inside Shop is an internal call: a simple jump inside the same code. Calling shop.price(4) from Main is an external call (a message call to another address), which costs more gas and can fail independently. this.price(4) inside Shop is also an external call to itself.
Function overloading
Several functions may share a name if their parameter types differ:
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.24;
import "hardhat/console.sol";
contract Main {
function area(uint256 side) internal pure returns (uint256) {
return side * side;
}
function area(uint256 width, uint256 height) internal pure returns (uint256) {
return width * height;
}
function main() external pure {
console.log("square 7 :", area(7));
console.log("rect 3x8 :", area(3, 8));
}
}square 7 : 49
rect 3x8 : 24
That is how console.log itself works: the library defines hundreds of overloads for different argument combinations.
Test yourself
Which visibility lets a function be called from derived contracts but NOT from outside?
externalpublicinternalprivate
Given function f() public returns (uint, uint, uint), how do you keep only the last value?
uint c = f()[2];(, , uint c) = f();uint c = f().last;(uint c) = f();
Does marking a state variable private hide its value from the public?
- Yes, it is encrypted
- No, anyone can read contract storage from the chain
- Yes, only the owner can see it
- Only for
stringvariables