How is this :
function myFunc() returns(uint a){}
different from this:
function myFunc() returns(uint){}
How is this :
function myFunc() returns(uint a){}
different from this:
function myFunc() returns(uint){}
Its 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) {
uint sum = a + b;
return sum;
}
returns (uint sum) while it should really just be returns (uint)
– Andre Miras
Dec 23 '20 at 10:11
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;
}
seconds is a reserved keyword, so your first example would not even compile.
– goodvibration
Feb 27 '19 at 12:49
i benefited from this feature whenever i called functions from an external client such as web3 or ethers, the returned object has named properties, instead of iterating over the object properties to get their values, you just destructure the object with the names
FYI, the syntax was deprecated in 0.5.0: https://docs.soliditylang.org/en/latest/050-breaking-changes.html#syntax ("Function types with named return values are now disallowed.")