Understanding return
Returning data is an integral part of a Solidity function. Solidity provides two different syntaxes for returning data from a function. In the following code sample, two functions – getBlockNumber
and getBlockNumber1
– are defined. The getBlockNumber
function returns uint
without naming the return
variable. In such cases, developers can resort to using the return
keyword explicitly to return from the function.
The getBlockNumber1
function returns uint
and also provides a name for the variable. In such cases, developers can directly use and return this variable from a function without using the return
keyword, as shown in the following code listing:
// SPDX-License-Identifier: MIT pragma solidity >=0.7.0 <0.9.0; contract ReturnValues { uint counter; function setNumber() public { counter = block.number; } ...