Functions
Functions in Solidity
Functions define the logic and behavior of a smart contract. In Solidity, functions can be classified by their behavior (pure, view, payable) and visibility (public, private, internal, external). Understanding these helps developers write secure and efficient smart contracts.
1. Pure Functions
A pure function neither reads nor modifies the contract’s state. It only works with local variables and function parameters. Pure functions are useful for performing calculations, comparisons, or utility tasks without depending on blockchain storage.
Example (Utility Math Function):
pragma solidity ^0.8.0;
contract PureExample {
function calculateSquare(uint256 x) public pure returns (uint256) {
return x * x;
}
}
In this example, calculateSquare(5) always returns 25 without interacting with the blockchain. It’s gas-efficient since it doesn’t touch state variables.
2. View Functions
A view function allows us to read state variables but prevents us from modifying them. They are often used for retrieving stored values.
Example (Reading Contract Data):
pragma solidity ^0.8.0;
contract ViewExample {
uint256 public storedNumber = 42;
function getNumber() public view returns (uint256) {
return storedNumber;
}
}
Here, getNumber() retrieves the value 42 stored on the blockchain but cannot change it.
3. Payable Functions
A payable function allows the contract to receive Ether. Without the payable modifier, any attempt to send Ether would fail. These functions are widely used in crowdfunding contracts, token purchases, or payment systems.
Example (Accepting Donations):
pragma solidity ^0.8.0;
contract DonationBox {
address public owner;
constructor() {
owner = msg.sender;
}
// Accept Ether donations
function donate() public payable {}
// Retrieve total balance in the contract
function getBalance() public view returns (uint256) {
return address(this).balance;
}
}
Anyone can call donate() and send Ether. The contract’s balance increases, and getBalance() shows the total donations collected.
4. Public Functions
A public function can be accessed both inside and outside the contract. By default, functions are public unless explicitly marked otherwise.
Example (Counter):
pragma solidity ^0.8.0;
contract PublicExample {
uint256 public counter;
function increment() public {
counter += 1;
}
}
Both the contract itself and external users can call increment() to increase the counter value.
5. Private Functions
A private function can only be called inside the same contract. They are useful for helper functions or sensitive logic that should not be accessible externally.
Example (Hidden Logic):
pragma solidity ^0.8.0;
contract PrivateExample {
uint256 private secretCode = 12345;
function getSecretCode() public view returns (uint256) {
return revealSecret();
}
function revealSecret() private view returns (uint256) {
return secretCode;
}
}
External users cannot directly call revealSecret(). They can only access the result through getSecretCode().
6. Internal Functions
An internal function can be used inside the contract and by derived (child) contracts. It’s like private, but with inheritance support.
Example (Inheritance):
pragma solidity ^0.8.0;
contract Parent {
function internalHelper() internal pure returns (string memory) {
return "Internal function called!";
}
}
contract Child is Parent {
function useInternal() public pure returns (string memory) {
return internalHelper();
}
}
The Child contract can call internalHelper() because it inherits from Parent.
7. External Functions
An external function can be called only from outside the contract. They are more gas-efficient than public functions for external calls. Inside the contract, they can only be accessed using this.functionName().
Example (External Calls):
pragma solidity ^0.8.0;
contract ExternalExample {
function externalOnly() external pure returns (string memory) {
return "I am external!";
}
function callExternal() public view returns (string memory) {
return this.externalOnly();
}
}
Calling externalOnly() directly from within the contract will fail, but using this.externalOnly() works.
Summary
| Function Type | Access / Behavior | Practical Use Case |
|---|---|---|
| pure | No state read/write | Math utilities (e.g., add, square) |
| view | Read-only state | Checking balances, stored data |
| payable | Accepts Ether | Crowdfunding, payments |
| public | Accessible anywhere | General functions |
| private | Only inside contract | Helper methods, secret logic |
| internal | Inside + child contracts | Shared logic in inheritance |
| external | Only outside calls | Optimized APIs, external triggers |
Continue Learning
Explore more topics in Solidity or browse other tutorials.