Vyper theorytheory 0/50 · 0%
Functions · medium

27. Multiple return values

Returning tuples from a function.

A function can return several values as a tuple by declaring multiple return types, and the caller destructures them positionally.

@external
@view
def stats() -> (uint256, uint256):
    return self.total_supply, self.total_staked

@external
def use_stats():
    supply: uint256 = 0
    staked: uint256 = 0
    supply, staked = self.stats()

This avoids allocating a throwaway struct just to bundle a couple of related values for a single call site.

Check your understanding

  1. 1. How does a function declare it returns two values?

  2. 2. How does a caller receive a tuple return?

  3. 3. When is a tuple return preferable to a struct?