Variables
Variables in Cairo can be aliased or evaluated. There are 4 different types of variables:
let
: References- Can be revoked
- Can be redefined
- Can be value references (e.g.
let foo = 42
) - Can be expression references (e.g.
let bar = foo
)
tempvar
: Temporary Variables- Can be revoked (because they rely on
ap
) - Can be redefined
- Based on
ap
register - Auto-increment the
ap
register
- Can be revoked (because they rely on
const
: Constants- Cannot be revoked
- Cannot be redefined
- Only integers
- Simple replacement by the Cairo compiler (not translated into a Cairo instruction)
local
: Local Variables- Cannot be revoked
- Can be redefined
- Based on
fp
register - Do not auto-increment the
ap
register
Example
# Constants can be defined at the top level to be accessible by all functions.
const meaning_of_life = 42
func main():
# `alloc_locals` is required to use Local Variables.
# `alloc_locals` is translated to `ap += SIZEOF_LOCALS` behind the scenes.
alloc_locals
# References
let reference = 42
let reference = 43
# Temporary Variables
# The following code is equivalent to:
# ```
# [ap] = 42; ap++
# let tmp = [ap - 1]
# ```
tempvar tmp = 42
tempvar tmp = 43
# Constants
const constant = 42
# `const` variables cannot be redefined.
# const constant = 43
# Local Variables
# The following code is equivalent to:
# ```
# [fp + 0] = 42
# [fp + 1] = 43
# ```
local x = 42
local y = 43
local x = 43
return ()
end