DEV Community

Muwawu Moses
Muwawu Moses

Posted on • Updated on

Mastering Vyper Functions(part1 )


As defined in the vyperlang official documentation, functions are executable units of code within a contract. Functions may be called internally or externally depending on their visibility and, they may accept input arguments and return variables in order to pass values between them. Let's have a quick function structure example;

def my_function():
Enter fullscreen mode Exit fullscreen mode
  1. Function visibility

It's a must that all functions must include exactly one visibility decorator i.e @internal or @external.

  • External functions:

These are marked with the @external decorator and are part of the contract interface and may only be called via transactions or from other contracts.

An external function may either have default or no default arguments. First,i will provide a function with no default argument.

def my_function(x: uint256, b: uint256):

Enter fullscreen mode Exit fullscreen mode

A function with default argument:

def my_function(x: uint256, b: uint256 = 1):

Enter fullscreen mode Exit fullscreen mode

For functions with default arguments, the Vyper compiler will generate N+1 overloaded function selectors based on N default arguments.

A function selector is how a vyper contract knows which function you are trying to call in the transaction.

In this example, my_function has one required argument x and one default argument b, which defaults to 1 if not provided.
Function Selectors

Because there is one default argument (b), Vyper will generate 1 + 1 = 2 function selectors:

Selector 1: For my_function(uint256 x, uint256 b)
Selector 2: For my_function(uint256 x)

  • Selector 1 for my_function with both arguments provided:
    • This allows the function to be called with both x and b. Let's take an example where we call both arguments;
# x is set to 10
# b is explicitly set to 5
my_function(10, 5) 

Enter fullscreen mode Exit fullscreen mode
  • Selector 2 for my_function with only x provided:
    • This allows the function to be called with just x, using the default value for b. And when we call only the required argument;
# x is set to 10
# b defaults to 1 (since no value for b is provided)

Enter fullscreen mode Exit fullscreen mode
  • Internal functions:

These functions are marked with the @internal decorator. They can only be accessed by other functions within the same contract and are called using the self object.

Imagine you have a contract that needs to do some repetitive calculations. Instead of writing the same code multiple times, you create an internal function to handle this. Other functions in your contract can call this internal helper whenever needed.


# an internal function that doubles a number
def _double(value: uint256) -> uint256:
    return value * 2

# an external function that users can call, which uses _double internally.
def get_double(value: uint256) -> uint256:
    return self._double(value)

Enter fullscreen mode Exit fullscreen mode

Well, congratulations for coming this far. I would recommend you to visit my next tutorial for more information about functions.
If you found this article helpful, please give me a heart, a follow and welcome any kind of interaction in the comment section.

Thank you!

Top comments (0)