I've seen many functions using a returns (bool). What does it do? What can it be used for?

function transferFrom(address from, address to, uint256 value) public returns (bool);
  • It's the return value for that specific function. It can be used for control flow or any kind of logic purpose in the caller code. – mirg Mar 13 '18 at 10:19

It returns a boolean value, ie true or false.

Assuming you are reading from a token spec, that transferFrom function most likely should return true if the transfer is made successfully, and false if it fails.

| improve this answer | |
  • How can a transfer fail? – enriquejr99 Mar 13 '18 at 11:31
  • This very much depends on what else is going on in your contract, but one reason which probably applies to your contract would be if the balance of the from account is less than the value that needs to be transferred. So you would have something like require(value <= balance) at the start of the function, where balance is a variable with the from account's balance of this token. – AnAllergyToAnalogy Mar 13 '18 at 12:18

Let me explain with one easy example.

contract A{
    bool public checkvalue;
function transferFrom(uint256 value)
    //.... any logical part here
    //let us consider simple example as below
        if (value == 2) return true;
        else return false;
    function check()public{
        //...your logical part here 
        checkvalue = transferFrom(2);

If you have knowledge on other any programming language like c,java then it is also same boolean in there. Here , check() function calls the transferFrom() function and return the boolean value true or false . Another Practical example is - If you do not need any return value on execution but you have to confirm it works and return as true and if not full fill the logical part on execution then return false. Hope, it might help you.

| improve this answer | |
  • But how can a transfer fail? Aside of out of gas. – enriquejr99 Mar 13 '18 at 11:34
  • 2
    there might be many reasons and depends on what logic you have implemented, let see, if (from_address !=msg.sender) return false. this is also another reasons. – Gopal ojha Mar 13 '18 at 12:05

Not sure that I have the same angle for that question. I asked myself if it's a good practice to return a true value on a function success.

Many see that pattern first in the ERC20 standard, the transfer function for example:

function transfer(address _to, uint256 _value) returns (bool success)

But the ERC20 is pretty old (it was defined in 2015). The security practices nowadays favour require/assert that test a condition and revert the transaction if the condition isn't met. That's the style of coding that newer standards like ERC721 are using. When a transaction is reverted no value is actually returned. Like raising an Exception in most programming languages.

If we look into openzepeplin implementation of ERC20:

function _transfer(address from, address to, uint256 value) internal {
    require(to != address(0), "ERC20: transfer to the zero address");

    _balances[from] = _balances[from].sub(value);
    _balances[to] = _balances[to].add(value);
    emit Transfer(from, to, value);

function transfer(address to, uint256 value) public returns (bool) {
    _transfer(msg.sender, to, value);
    return true;

We see that if the method succeeds true would be returned, otherwise the transaction will be reverted and no value will be returned at all. So it looks like boolean is returned for mere compatibility with the standard.

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.