Journey through Cairo V — Implicit Arguments

Darlington Nnam
Coinmonks
5 min readSep 9, 2022

--

Welcome once again, to the fifth article, in our series, “Journey through Cairo”. In our last article which can be found here, we explained Storage in Cairo, and took a good look at how mapping can be implemented using Cairo’s storage.

Today, we’d be taking a walk through implicit arguments, and how they function generally in Cairo. As always, if you are just joining mid-flight, endeavour to checkout the articles before this to better understand and flow with this series.

P.S: All code snippets used in this tutorial, makes use of the old Cairo syntaxes from Cairo-lang v0.9.0, since its what was used in our Starklings exercises as at the time of this writing.

Implicit Arguments

Cairo functions takes two types of arguments: the explicit arguments, provided between `(` and `)`, and the implicit arguments, provided between `{` and `}`.

The major difference between the both is that, while explicit arguments are used within the function’s body, and can’t be accessed outside it, implicit arguments are inherited by other functions calls that require them.

Let’s take a look at this function from our previous article on Storage:

@externalfunc update_id{      syscall_ptr : felt*,      pedersen_ptr : HashBuiltin*,      range_check_ptr    }(_number: felt):    id.write(_number)end

As we can see here, this function takes in three implicit arguments:

  1. syscall_ptr — this argument enables Cairo functions to make system enabled calls.
  2. pedersen_ptr — this argument is used for computing pedersen hashes.
  3. range_check_ptr — this argument is used for integer comparisons.

You might be wondering, why we needed to pass all these arguments, when we are neither computing any hash function or even doing any integer comparison? This is because the storage variables which we were trying to write to, requires these implicit arguments in order to compute the actual memory address of this variable. This may not be needed in simple variables such as the id storage variable we looked at from earlier, but with storage mappings such as with balance, computing the Pedersen hash is part of what read() and write() do, and that’s why the parent functions which calls these storage variables must have these implicit arguments clearly stated.

To further understand this, let’s dive into some Starklings exercises!

implicit_arguments01.cairo

This exercise requires we fix the implicit_sum function to make the test pass.

As we can see from the test, the test_sum function calls our implicit_sum function, passing unto it, a and b arguments, but rather passing it implicitly.

To use these arguments, we’d have to specify them as implicit arguments in our implicit_sum function too, so Cairo is informed that these arguments can be or was passed between function calls.

func implicit_sum{a: felt, b: felt}() -> (result: felt):   return (a + b)end

Having done this in line 10, let’s check if our test passes.

Yes! it does

implicit_arguments02.cairo

In this exercise, we are requires to fix child_function_1 and child_function_2, to make the test pass, but without altering the function body.

As we can see from the exercise, the parent function has two implicit arguments a and b

child_function_1 uses the implicit argument a, and child_function_2 uses the implicit argument b. To make this exercise pass, we’d need to specify the implicit argument used by each function in their function signatures.

for child_function_1:

func child_function_1{a: felt}() -> (result : felt):  return (2 * a)end

and for child_function_2:

func child_function_2{b: felt}() -> (result : felt):  return (b + 3)end

Now let’s check if the test passes.

Voila! it does

implicit_arguments03.cairo

According to Starklings, what is really neat with implicit arguments is that they are returned implicitly by any function using them, this is a very powerful feature of the language since it helps with readability, letting the developer omit implicit arguments in the subsequent function calls.

In this exercise, we are expected to fix the black_box function to make the test pass.

This is very easy to do as we just need to assign the argument, secret to match the words ‘very secret!’ and Cairo will automatically return the argument, without the need to explicitly return it.

Now let’s check if we passed the test.

Yesss! We did.

Conclusion

We have come half way through this journey, and by now i’m sure you are now getting familiar with how Cairo works in the broader light.

We just went through Implicit arguments in Cairo, and how helpful they can be to you whilst writing your contracts.

As always, If you got value from this article, do well to share with others.

You could also connect with me on the following socials, especially on Twitter, where i share my little findings on Cairo!

Twitter: https://twitter.com/0xdarlington

LinkedIn: https://www.linkedin.com/in/nnamdarlington

Github: https://github.com/Darlington02

New to trading? Try crypto trading bots or copy trading

--

--