Constants
Part of the Fundamentals section of Coddy's Solidity journey. Lesson 29 of 53.
Use a contract-level constant for a value fixed at compile time. Supply its value in the declaration. A constant cannot be reassigned. It is useful for a fixed conversion factor or a stable rule shared by several functions.
Declare this at contract scope:
uint256 constant HOURS_PER_DAY = 24;Use it inside the provided main function:
uint256 daysCount = 3;
console.log(daysCount * HOURS_PER_DAY);HOURS_PER_DAY is fixed at twenty-four, so three days contain seventy-two hours. Unlike ordinary state initialization, a constant is compiled into its uses. Uppercase names are a common convention for constants, not a compiler requirement.
A constant is initialized in its declaration and cannot be reassigned.
Challenge
EasyDeclare a uint256 constant MINUTES_PER_HOUR equal to 60. Print hoursCount times that constant.
The tests pass arguments to main in this order: uint256 hoursCount. 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 hoursCount) 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