How is this :
function myFunc() returns(uint a){}
different from this:
function myFunc() returns(uint){}
Ethereum Stack Exchange is a question and answer site for users of Ethereum, the decentralized application platform and smart contract enabled blockchain. It only takes a minute to sign up.
Sign up to join this communityHow is this :
function myFunc() returns(uint a){}
different from this:
function myFunc() returns(uint){}
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;
}
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