In this chapter, we're going to learn about function return
values, and optional function decorators.
To return a value from a function, the declaration looks like this:
nameToAge: HashMap[String[64], uint256]
@external
def getAge(name: String[64]) -> uint256:
return self.nameToAge[name]
The above getAge
function returns a uint256
which is represented by the symbol ->
followed by the return type uint256
.
In a previous chapter, we explored 2 of the function decorators: @internal
and @external
.
There are a few more optional function decorators:
Decorator | Description |
---|---|
@pure |
Function does not read contract state or environment variables |
@view |
Function does not alter contract state, but may read contract state |
@payable |
Function is able to receive Ether |
@nonreentrant(<unique_key>) |
Function cannot be called back into during an external call |
Following the above example, we can add @view
function decorator on the getAge
function as it reads the contract state (storage variable) but does not alter it.
nameToAge: HashMap[String[64], uint256]
@view
@external
def getAge(name: String[64]) -> uint256:
return self.nameToAge[name]
We want to create an internal
function which does not read or alter the contract state or environment variables (meaning a pure
function) which returns a uint256
.
- Create an function named
_generateRandomDNA
with internal and pure function decorators. - The function should accept an input parameter:
_name
of typeString[32]
. - The function returns a
uint256
type. - Use the
pass
keyword to define an empty function body.