Solidity: Functions and Visibility

x1xx1x
4 min read

Functions

  • Functions can be defined inside and outside of contracts.

Functions outside of a contract, also called “free functions”, always have implicit internal visibility. Their code is included in all contracts that call them, similar to internal library functions.

Functions in Solidity have the following form :

function function_name(<param_type> <param_name>) <visibility> <state mutability> [returns(<return_type>)]{ ... }

With the second approach, you can omit the names of the return variables and specify only their types.

function arithmetic(uint _a, uint _b) public pure
        returns (uint o_sum, uint o_product)
    {
        return (_a + _b, _a * _b);
    }

2. Provide return values directly with the return statement:

function arithmetic(uint _a, uint _b) public pure
        returns (uint o_sum, uint o_product)
    {
        o_sum = _a + _b;
        o_product = _a * _b;
    }

1. Using names of the return variables:

A function can return an arbitrary number of values as output. There are two ways to return variables from functions:

Visibility

There are four types of visibility for functions:

  • Private: The most restrictive one, the function can only be called from within the smart contract where it’s defined.Its not accessible even by child contracts that inherit it.

Use Case:

  • Internal helper functions that must never be overridden or accessed from outside logic.

  • Example: Data cleaning, logging, or complex validation steps.

Code:

solidity
Copy code
function _cleanData() private pure returns (bool) {
    return true;
}
  • Internal: Can be called only from this contract and any child contract that inherits from it.
function internalDoor() internal view returns (string memory) {
return "Welcome to the lab!";
}
  • Like a family-only laboratory — you can let your sidekicks or apprentices use this space, but strangers? Nope.

-Use it for tools, helpers, or logic that should only run inside your smart contract family.

  • External: Can only be called from outside the smart contract. (Must use this if you want to call it from within the smart contract.)

  • You can’t call it from inside — unless you use this, which is gas-expensive

solidity
Copy code
function externalCall() external view returns (string memory) {
    return "You called from afar!";
}
  • Picture a hotline to your superhero base.

Only outsiders can call it — you can’t pick up your own phone unless you waste resources pretending to call yourself.

  • Use this when exposing a clean API to other contracts or users — especially for gas optimization (calldata is cheaper than memory).

  • Public: Can be called from anywhere. (The most permissive one)

function publicGate() public view returns (string memory) {
return "Front door wide open!";
}
  • Imagine a grand hall — anyone can walk in. You can walk in, your sidekick can walk in, even your enemy’s cat can walk in if they know the spell (i.e., function name).

  • Use this when you're offering services to others (like getBalance(), checkWinner(), etc.)

VisibilityCalled inside this contractCalled from child contractsCalled from external accounts or contracts
public
private
internal
external❌ (directly)

When to use What?

Use caseBest Visibility
Secret logicprivate
Inheritance logicinternal
Public APIs (UI, dApps)public
Efficient APIs (gas-focused)external

State mutability

  • state mutability determines whether a function can change the state of a contract or not.The state of a contract refers to its variables, which can be read or modified by its functions.

  • there are four different types of state mutability, which are as follows:

  1. view: Functions declared with view can only read the state, but do not modify it.
pragma solidity ^0.8.0;

contract MyContract {
    uint256 public myNumber = 10;

    function getMyNumber() public view returns (uint256) {
        return myNumber;
    }
}

b. pure: A pure function is a function that does not read or modify the state of a contract. These functions are typically used for mathematical or string operations and are executed locally. In Solidity, pure functions are denoted by the keyword “pure”.

pragma solidity ^0.8.0;

contract Calculator {
    function add(uint256 x, uint256 y) public pure returns (uint256) {
        return x + y;
    }
}

c. payable: A payable function is a function that can receive Ether in a contract. These functions are typically used for financial transactions. In Solidity, payable functions are denoted by the keyword “payable”.

pragma solidity ^0.8.0;

contract MyContract {
    function receivePayment() public payable {
        // Do something with the received Ether
    }
}

d. non-payable functions : a non payable function is a function that cannot receive ether in a contract. They are typically used to modify the state of a contract. they are denoted by the absecnce of the word “payable”.

0
Subscribe to my newsletter

Read articles from x1x directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

x1x
x1x

Everyday learner