DEV Community

Cover image for Defining and Calling Functions in Yul
Shlok Kumar
Shlok Kumar

Posted on

Defining and Calling Functions in Yul

Defining and Calling Functions in Yul allow for code reuse and make complex operations easier to carry out

Yul is an intermediate-level programming language used for low-level EVM (Ethereum Virtual Machine) operations. It is used for writing smart contracts in Ethereum, and it is compatible with the Solidity programming language. In this blog, we will explore how to define and call functions in Yul.

Defining Functions in Yul

Defining a new function in Yul requires two things: a function definition and a jump instruction to call the function. Here is an example of how to define a function in Yul:

Copy Codefunction greet() -> s:
    s := "Hello World!"
    mstore(0x0, 0x20)
    mstore(0x20, 0x0a)
    return(0x0, 0x2f)
Enter fullscreen mode Exit fullscreen mode

This declares a function called greet that takes no arguments and returns a string. The code creates a string literal "Hello World!", then stores its location, length, and zero-terminator in memory using the mstore function. Finally, it returns the start and end positions of the string using the return keyword.

Calling Functions in Yul

Next, you can call the greet function in another function, like so:

Copy Codefunction printGreeting() -> s:
    calldatacopy(0x0, 0x0, calldatasize())
    let sLen := calldataload(0x04)
    let result := call(gas(), 0x05, 0x00, 0x00, sLen)
    return(0x00, returndatasize())
Enter fullscreen mode Exit fullscreen mode

Here, we have created a function called printGreeting that calls the greet function. We first copy the input data (calldata) to memory using calldatacopy. We then extract the length of the input data using calldataload. Next, we use the call keyword to call the greet function, passing in the appropriate parameters. Finally, we return any output data from the greet function using returndatasize.

Example

Here is an example of a function that adds two numbers:

function add(a, b):
  let c := add(a, b)
  return(c)
Enter fullscreen mode Exit fullscreen mode

In this example, the function is named add, and it takes two parameters, a and b. The code for the function adds a and b together and stores the result in c. The return statement then returns the value of c. To call a function in Yul, you use the name of the function followed by the arguments in parentheses. Here is an example of calling the add function:

let result := add(2, 3)
Enter fullscreen mode Exit fullscreen mode

In this example, the add function is called with the arguments 2 and 3. The result of the function is then stored in the variable result.Functions can also be used to return multiple values. Here is an example of a function that returns the sum and difference of two numbers:

function sumAndDiff(a, b):
  let sum := add(a, b)
  let diff := sub(a, b)
  return(sum, diff)
Enter fullscreen mode Exit fullscreen mode

In this example, the function is named sumAndDiff, and it takes two parameters, a and b. The code for the function calculates the sum and difference of a and b and stores the results in sum and diff. The return statement then returns both values. To call a function that returns multiple values, you can use the let keyword to assign each value to a separate variable. Here is an example of calling the sumAndDiff function:

let (sum, diff) := sumAndDiff(5, 3)
Enter fullscreen mode Exit fullscreen mode

In this example, the sumAndDiff function is called with arguments 5 and 3. The let keyword is used to assign the values returned by the function to the variables sum and diff.

Conclusion

In conclusion, defining and calling functions in Yul is a straightforward process. Functions can be defined using the function keyword followed by a function name and its return type. To call a function, use the call keyword along with the function's name and any necessary parameters. By understanding how to define and call functions in Yul, you'll be able to create more complex smart contracts and execute low-level EVM operations.

Functions are an essential part of Yul that allow for code reuse and make complex operations easier to carry out. By defining and calling functions in your Yul code, you can make your code more readable and maintainable.

For more content, follow me at - https://linktr.ee/shlokkumar2303

Top comments (0)