DEV Community

Cover image for Quick Guide to Different Types of Function in Solidity
Rohit
Rohit

Posted on

Quick Guide to Different Types of Function in Solidity

Constructor function: A constructor function is either public or Internal. This is the first function that is automatically called only when the smart contract is deployed on the network. It cannot be called afterwards.

View function: A view function can only perform reading from the state or from another view function.

Pure function: A pure function does not perform any reading or viewing on the state of the blockchain.

Function Visibility: Who can call the function defines its visibility. Depending on who do you want your function to call, the visibility fo the function changes. There are four types of function visibility:

Public Visibility (can be called externally and internally),

Private Visibility (can be called only internally, only for the contract),

External Visibility (can be called from other contracts and can be called externally)

Internal Visibility (can be called from the contract itself or from the derived contracts, they cant be invoked by transactions)

Fallback function: A fallback function is always external. It is called when a transaction without a function-call is sent to the smart contract OR when the function-call in the smart contract isn't found.

UPDATE Solidity 0.6: There are two different types of fallback function now

The “fallback” function is called when no other function matches and no money is sent along.

The “receive” function matches when no other function matches and money is sent along.

Example:

pragma solidity ^0.6.0;
contract FunctionsExample {

 mapping(address => uint) public balanceReceived;
 address payable owner;

//Constructor function: A constructor function is either public or Internal. This is the first function that is automatically called only when the smart contract is deployed on the network. It cannot be called afterwards.
 constructor() public {
 owner = msg.sender;
 }

//Public Visibility (can be called externally and internally)
 function destroySmartContract() public {
 require(msg.sender == owner, "You are not the owner");
 selfdestruct(owner);
 }

 function receiveMoney() public payable {
 assert(balanceReceived[msg.sender] + msg.value >= balanceReceived[msg.sender]);
 balanceReceived[msg.sender] += msg.value;
 }

 function withdrawMoney(address payable _to, uint _amount) public {
 require(_amount <= balanceReceived[msg.sender], "not enough funds.");
 assert(balanceReceived[msg.sender] >= balanceReceived[msg.sender] - _amount);
 balanceReceived[msg.sender] -= _amount;
 _to.transfer(_amount);
 }

//The receive function matches when no other function matches and money is sent along
 receive () external payable {
 receiveMoney();
 }

//View function: A view function can only perform reading from the state or from another view function 
 function getOwner() public view returns(address) {
 return owner;
 }

//Pure function: A pure function does not perform any reading or viewing on the state of the blockchain
 function convertWeiToEth(uint _amountInWei) public pure returns(uint) {
 return _amountInWei / 1 ether;
 }

}

Top comments (0)