8

How is this :

function myFunc() returns(uint a){}

different from this:

function myFunc() returns(uint){}
9

It's purpose is two-fold, firstly to serve as a visual aid to deciphering the meaning of a return value by simply looking at the function signature.

Secondly, it does not require one to initialize a given variable in the code OR explicitly return the value itself, for example:

function calculateSum(uint a, uint b) pure returns (uint sum) {
    sum = a + b;
}

is equivalent to:

function calculateSum(uint a, uint b) pure returns (uint sum) {
    uint sum = a + b;
    return sum;
}
|improve this answer|||||
3

It does not change the result of your program.

One of its purposes is to help remember what a function returns. For example:

function getTimeSinceAdded() return (uint seconds){...}

Now you only need to look at one line of code to know that the return value is in seconds, you don't need to read the function.

Another example is if you have multiple return values:

function myFunc() returns(uint, uint, uint){}

function myFunc() returns(uint timestamp, uint ethers, uint userID){}

Giving the return values names again makes it much easier to read what is returned and in which order.

Edit:

Like @hcaw said, the other purpose is so you don't have to use the return statement. Instead, you can assign each return value separately:

function myFunc() returns(uint timestamp, uint ethers, uint userID)
{
    timestamp = 4;
    // some code...
    ethers = 1 ether;
    // some more code...
    userID = 4;
}
|improve this answer|||||
  • Side note: seconds is a reserved keyword, so your first example would not even compile. – goodvibration Feb 27 '19 at 12:49

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.