Visibility in Solidity

Functions and state variables have to declare whether they are accessible by other contracts or externally.

Functions can be declared as

VisibilityDescription
publicAny contract and account can call
privateAccessible only inside the contract that defines the function
internalAccessible only inside contract that inherits an internal function
externalAccessible only other contracts and accounts can call

State variables can be declared as public, private, or internal but not external.



// SPDX-License-Identifier: MIT
pragma solidity ^0.8.15;

contract ViewAndPure {
    uint public gamePoints = 0;


    // No modification or reading from the state
    function checkRewards(uint multiplier, uint points) public pure returns (uint) {
        return multiplier * points;
    }

    // No modification but can read from the state
    function addGamePoints(uint newScore) public view returns (uint) {
        return gamePoints + newScore;
    }

}

If you call view or pure functions externally, you do not pay a gas fee.

Read more about storage and memory here