0

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?

1

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.

  • So am I correct that a function with returns (uint) in its declaration implicitly produces a return variable with some hidden name and returns it in all cases I forget to write return? It just seems kinda strange that the following two situations are handled differently: 1) function with returns (uint) returns true - type error, does not compile; 2) described above - compiles. Aren't both cases examples of functions breaking the predefined "calling contract"? – Sergei Tikhomirov Nov 16 '16 at 16:12

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.