0

Solidity: Functions.

Functions are the most important part of smart contracts as this is the place where the logic of the smart contract takes place, in other words, whatever the smart contract is supposed to do, this will most likely happen within the logic defined in the functions of said contract.

Functions can receive parameters that are used within their implementation for calculations and assignments for instance. Functions can also return values to the caller and they can return any number of values (not only single values). All this details are specified by the signature of the function, the signature is simple the function definition containing the name, parameters, modifiers and return type.



Functions

Let’s consider the following example, here, the function fooFunction receives an unsigned integer (uint256) as parameter, runs some logic based on that parameter (namely, compares it to 7) and finally it returns a boolean .

 contract fooContract {
    function fooFunction(uint256 _val) public pure returns (bool) {
        if(_val == 7) {
            return true;
        }
        return false;
    }
}


Returning Multiple Values

Functions can have multiple return values, meaning that a function can return a collection of values of different types in the form of return(v1, v2, …, vn). Let’s consider the following example, here the fooFunction returns a tuple of unint256 and bool:

contract fooContract {
    function fooFunction(uint256 _val) public pure returns (uint256, bool) {
        if(_val == 7) {
            return (1, true);
        }
        return (0, false);
    }
}


Overloading

Overloading in contract means having functions with the exact same name, however, you cannot have two functions with the same name and same number and types of parameters. In other words, the below is allowed in Solidity because the second parameter is different (even though the name of the function is exactly the same, fooFunction).

contract fooContract {
    function fooFunction(uint256 _val, bool _val2) public pure returns (bool) {
        if(_val == 7 && _val2) {
            return true;
        }
        return false;
    }
    
    function fooFunction(uint256 _val, uint256 _val1) public pure returns (bool) {
        if(_val == 7 && _val1 == 7) {
            return true;
        }
        return false;
    }
}


View Functions

When a function is marked as view, it means that the logic of the function will not alter in any way the state of the contract, but they can read from it (hence the view keyword). In the below example, the function fooFunction reads the balance mapping and returns the value assigned to the sender.

contract fooContract {
    mapping (address => uint256) balance;
    
    function fooFunction() public view returns (uint256) {
        return balance[msg.sender];
    }
}


Pure Functions

A pure function will not read nor write from/to the state of a smart contract. They are used when the function is only doing processing and there is no intent to read/write from/to the state. In the below example, the logic of the function simply does some calculation and returns a value without touching the state of the contract.

contract fooContract {
    function fooFunction(uint256 _val) public pure returns (bool) {
        if(_val == 7) {
            return true;
        }
        return false;
    }
}


Fallback Functions

A fallback function is the function called when no other function matches the signature. In other words, if you make a call to a contract with a function identifier that does not exists in the contract (or if no data is provided at all) then the fallback function is invoked.

Previously the fallback was declare as below:

contract fooContract {
    function () external payable {
        
    }
}

However, in recent changes this function was split in two: receive and fallback . The receive function is invoked whenever the calldata is empty (whether or not ether is sent), the fallback function is invoked when the function identifier does not match any function in the contract or when calldata is empty and receive is not defined.

contract fooContract {
    function fooFunction(uint256 _val) public {
        // Logic
    }
    
    fallback() external {
        // Fallback function
    }
    
    receive() external payable {
        // To fund the smart contract
    }
}


<< Prev