- Write functions with parameters and single, multiple or named return values.
- Choose the correct visibility: public, external, internal or private.
- Choose the correct state mutability: view, pure, payable or the default.
- Explain function overloading and internal vs external calls.
What are Functions?
Functions are blocks of code that perform a specific task for given inputs, so the same lines do not need to be repeated. For example, an add function takes two numbers and returns their sum wherever it is needed.
function <name>(<parameters>) <visibility> [<mutability>] [<modifiers>] [returns (<return types>)] {
// statements
}
function add(uint a, uint b) public pure returns (uint) {
return a + b;
}
Solidity Function Visibility Specifiers
Visibility decides who can call a function. Every function must state its visibility.
| Specifier | Callable from | Typical use |
|---|---|---|
public | Everywhere: inside the contract, derived contracts, other contracts and users. | General API functions |
external | Only from outside (users and other contracts). Inside the contract it can be called only as this.f(), which is an external call. | Entry points; parameters can use cheap calldata |
internal | The current contract and contracts derived from it. | Shared helper logic |
private | Only the current contract (not derived contracts). | Implementation details |
Solidity Function State Mutability
State mutability tells the compiler (and users) how a function interacts with blockchain data:
| Keyword | Reads state? | Changes state? | Notes |
|---|---|---|---|
view | Yes | No | Free when called off-chain |
pure | No | No | Works only with its inputs and local variables |
payable | Yes | Yes | Can receive Ether (msg.value) |
| (default, non-payable) | Yes | Yes | Rejects any Ether sent to it |
The compiler checks these rules: a pure function that reads a state variable, or a view function that writes one, will not compile.
Return Values and Overloading
- A function can return several values:
returns (uint, bool)andreturn (x, true);. The caller can unpack them:(uint a, bool ok) = f(); - Named return variables (
returns (uint sum)) are local variables that are returned automatically at the end. - Overloading: several functions can share a name if their parameter types differ, e.g.
area(uint side)andarea(uint l, uint w). - A
publicstate variable automatically creates a getter function.
Example : Functions in Practice
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.20;
contract FunctionsDemo {
uint public total; // getter total() is generated
function add(uint a, uint b) public pure returns (uint) { // pure
return a + b;
}
function addToTotal(uint x) external { // changes state
total = _double(x) + total;
}
function getTotal() public view returns (uint) { // view
return total;
}
function divide(uint a, uint b) public pure returns (uint quotient, uint remainder) {
quotient = a / b; // named returns
remainder = a % b;
}
function area(uint side) public pure returns (uint) { // overloading
return side * side;
}
function area(uint length, uint width) public pure returns (uint) {
return length * width;
}
function donate() external payable {} // accepts Ether
function _double(uint x) internal pure returns (uint) { // internal helper
return x * 2;
}
function useDivide() external pure returns (uint) {
(uint q, ) = divide(17, 5); // unpack, ignore second value
return q; // 3
}
}
Key Takeaways
- Visibility: public (everyone), external (outside only), internal (contract + children), private (contract only).
- Mutability: view reads, pure neither reads nor writes, payable accepts Ether.
- Functions can return multiple or named values and can be overloaded by parameter types.
The PDF E-Notes are the earlier printable version of this chapter. The notes on this page are the latest, revised version.
Video : Functions in Solidity
Resource Files
Question / Answer (15 questions)
function name(parameters) visibility [mutability] [modifiers] [returns (types)] { ... }, e.g. function add(uint a, uint b) public pure returns (uint) { return a + b; }.function divide(uint a, uint b) public pure returns (uint q, uint r) { q = a / b; r = a % b; } returns both the quotient and remainder.(uint q, ) = divide(17, 5);.MCQ Test : Check Your Understanding (5 questions)
Select an option and press Check Answer, or answer all questions and press Submit Test to see your score. Each answer comes with an explanation.
Bibliography/References
The notes, questions and MCQs on this page are original explanations written for this course. The works listed below were consulted and are cited for attribution and further reading. Product names, logos and trademarks belong to their respective owners. Short quotations, where used, are marked and attributed.
- Solidity Team. (n.d.). Contracts (creating contracts, visibility and getters, function modifiers, constant and immutable, functions, events, custom errors, inheritance, abstract contracts, interfaces, libraries). Solidity Documentation. https://docs.soliditylang.org/en/latest/contracts.html
- Solidity Team. (n.d.). Expressions and Control Structures (if/else, loops, checked arithmetic, error handling, try/catch). Solidity Documentation. https://docs.soliditylang.org/en/latest/control-structures.html
- Solidity Team. (n.d.). Style Guide. Solidity Documentation. https://docs.soliditylang.org/en/latest/style-guide.html
- codedamn. (n.d.). Solidity functions. https://codedamn.com/news/solidity/solidity-functions
- Modi, R. (2018). Solidity Programming Essentials: A beginner’s guide to build smart contracts for Ethereum and blockchain. Packt Publishing. ISBN 978-1-78883-138-3.