This intro is intended for those who are already familiar with the previous cairo syntax and vm as we will compare aspects of old cairo to new ones.
To begin we will take a look at variables which which we can assign to a felt, or other number data types.
let x = 1;
Variables are by default read only, so if we try to reassign the variable like
let x = 1;
x = 2;
we recieve
error: Cannot assign to an immutable variable.`
However we can mark the variable as mutable and re-assign without issue
let mut x = 1;
x = 2;
The variables can be scoped with brackets
// To run these functions, rename them to main, or call them from main
func foo() -> felt {
let x = 1;
{
let x = 2;
}
x // returns 1
}
func bar() -> felt {
let x = 1;
{
let x = 2;
return x; // returns 2
}
}
To test the boundary conditions of a felt we will need a pow operation. This is currently not implemented in cairo natively but we can make a pow function quite easily.
func pow(base : felt, exp : felt,) -> felt{
let res = pow_inner(base, exp - 1, base);
res
}
func pow_inner(base : felt, exp : felt, total : felt) -> felt {
match exp {
0 => total,
_ => {
pow_inner(base, exp - 1, total * base)
},
}
}
The felt max is the size of the field - 1. Adding 1 will have our variable wrap around to 0.
func main(){
let felt_max = pow(2, 251) + 17 * pow(2, 192);
felt_max + 1 == 0; // evaluates to true
}