Definition
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;
@external
def my_function():
...
- 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.
@external
def my_function(x: uint256, b: uint256):
pass
A function with default argument:
@external
def my_function(x: uint256, b: uint256 = 1):
pass
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
andb
. Let's take an example where we call both arguments;
- This allows the function to be called with both
# x is set to 10
# b is explicitly set to 5
my_function(10, 5)
- Selector 2 for my_function with only x provided:
- This allows the function to be called with just
x
, using the default value forb
. And when we call only the required argument;
- This allows the function to be called with just
# x is set to 10
# b defaults to 1 (since no value for b is provided)
my_function(10)
- 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.
Example:
# an internal function that doubles a number
@internal
def _double(value: uint256) -> uint256:
return value * 2
# an external function that users can call, which uses _double internally.
@external
def get_double(value: uint256) -> uint256:
return self._double(value)
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)