Pure Functions
Part of the Fundamentals section of Coddy's Solidity journey. Lesson 23 of 53.
A pure function computes from its parameters and local values without reading or writing contract state. This makes a calculation easy to reason about. Marking a function pure is a promise checked by the compiler, not a request to erase data.
Define this helper inside the contract:
function perimeter(uint256 w, uint256 h) internal pure returns (uint256) {
return 2 * (w + h);
}Use it inside the provided main function:
console.log(perimeter(8, 3));The helper needs only width and height. It returns twice their sum, so it can be pure. The main wrapper stays view because it uses the debugging console; the helper itself contains no logging or state access.
A pure function does not read or write contract state.
Challenge
EasyCreate a pure internal helper named triple that returns three times its uint256 input. Print triple(value) from main.
The tests pass arguments to main in this order: uint256 value. Each test starts with fresh contract state.
Use console.log for the requested output. Print only the requested values, one per line, with no extra labels unless the task specifies them.
Try it yourself
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.26;
import "forge-std/console.sol";
contract Main {
function main(uint256 value) external view {
// Write your solution here.
}
}This lesson includes a short quiz. Start the lesson to answer it and track your progress.
All lessons in Fundamentals
Practice on your own: Solidity playground