Functions in Solidity: A Complete Guide

Functions are a key element of any Solidity contract.

Posted by Anthony Hume
on February 27, 2025


They define the logic of the contract, manage data, and interact with the blockchain. In this article, we will explore different types of functions, their features, and best practices for using them.

1. Types of Functions in Solidity
Functions in Solidity can be classified based on several criteria:

1.1. By Visibility
Visibility determines which other contracts or users can call the function. Solidity provides four visibility modifiers:

public – can be called both from inside and outside the contract.
external – can only be called from outside the contract.
internal – can be accessed only within the current contract and its child contracts.
private – can be accessed only within the same contract.

pragma solidity ^0.8.0;

contract VisibilityExample {
uint private count = 0;

function publicFunction() public view returns (uint) {
return count; // Can be called internally and externally
}

function externalFunction() external pure returns (string memory) {
return "Only external calls!";
}

function internalFunction() internal pure returns (string memory) {
return "Only for this contract and children!";
}

function privateFunction() private pure returns (string memory) {
return "Only for this contract!";
}
}

1.2. By State Changes
Depending on how a function interacts with the blockchain, it can be:

view – does not modify the blockchain state, only reads data.
pure – neither modifies the blockchain state nor reads data.
payable – allows the function to receive Ether.

contract StateFunctions {
uint private value = 100;

function getValue() public view returns (uint) {
return value; // Reads data but does not modify it
}

function addNumbers(uint a, uint b) public pure returns (uint) {
return a + b; // Does not interact with contract storage
}

function deposit() public payable {
// This function can receive Ether
}
}

1.3. By Access Level
Functions can be called by different participants in the network:

Open functions – available to any user.
Restricted functions – can be called only by specific addresses (e.g., the contract owner).

contract AccessControl {
address private owner;

constructor() {
owner = msg.sender;
}

modifier onlyOwner() {
require(msg.sender == owner, "Not the owner!");
_;
}

function sensitiveFunction() public onlyOwner {
// Code that only the owner can execute
}
}


2. Special Functions
Solidity also provides several special functions:

2.1. Constructor (constructor)
Called when the contract is deployed. Used to set initial values.

contract MyContract {
uint public value;

constructor(uint _value) {
value = _value; // Initializing a variable at deployment
}
}

2.2. Fallback Function (fallback())
Called when the contract receives Ether without a specified function or when a non-existent function is called.

contract FallbackExample {
event Received(address sender, uint amount);

fallback() external payable {
emit Received(msg.sender, msg.value);
}
}

2.3. Receive Function (receive())
Used exclusively for receiving Ether without data.

contract ReceiveExample {
event FundsReceived(address sender, uint amount);

receive() external payable {
emit FundsReceived(msg.sender, msg.value);
}
}

3. Best Practices for Working with Functions
Minimize the use of public functions – they increase the risk of attacks.
Use access modifiers – such as onlyOwner for critical functions.
Optimize gas usage – avoid unnecessary calculations in functions.
Validate input data – use require() to check conditions.

Conclusion

Functions in Solidity are the primary mechanism for controlling smart contracts. They can be public or private, modify the blockchain state, or simply perform calculations. Understanding how functions work will help you build secure and efficient smart contracts.