[Ethereum] Function with returns compiles without return statement

remixsolidity

I was surprised to find out that the returns clause in function declaration seems not to be enforced. That is, the following code compiles in browser-solidity (foo returns 0 when invoked):

pragma solidity ^0.4.4;

contract TestContract {
    function foo() returns (uint) {
        uint b = 3+5;
        //return b;
    }
}

Isn't it supposed not to type-check if the function that is declared to return something in fact does not? What is the rationale behind this design decision?

Best Answer

The statement "return x" is equivalent to "r = x; return;" where r is the return variable. As all variables are automatically assigned a "zero value" (including the return variables), not using "return" is not unsafe but it might indicate a programming error.

Especially if you name the return variables, code that does not use an explicit "return" is sometimes easier to read and check.

Related Topic