Functions
Functions are a fundamental part of any programming language, including Stylus, enabling you to encapsulate logic into reusable components.
This guide covers the syntax and usage of functions, including internal and external functions, and how to return multiple values.
Learn More
Overview
A function in Stylus consists of a name, a set of parameters, an optional return type, and a body.
Just as with storage, Stylus methods are Solidity ABI equivalent. This means that contracts written in different programming languages are fully interoperable.
Functions are declared with the fn
keyword. Parameters allow the function to accept inputs, and the return type specifies the output of the function. If no return type is specified, the function returns void
.
Following is an example of a function add
that takes two uint256
values and returns their sum.
This code has yet to be audited. Please use at your own risk.
fn add(a: uint256, b: uint256) -> uint256 {
return a + b;
}