NEWTON
Asked
4 months ago
40
views
1
I'm new to Cairo and I want to know what is the difference when I write let x = 1;
and tempvar x = 1;
. Is there any difference in Cairo Lang between let and tempvar?
Hypobrai
asked
4 months ago
0
Accepted answer
Imagine you have the following code:
tempvar x = 2;
let y = x * x * x;
let z = y * y * y;
serialize_word(z);
Because of the let
s, the serialize_word(z)
is equivalent to
serialize_word(x * x * x * x * x * x * x * x * x)
.
"tempvar" stands for "temporary variable", which means that
a variable will be allocated for the intermediate values
(x * x * x
and y * y * y
). If you change the two let
s to tempvar
s and run the program this reduces the same computation to use only 4 steps and 2 new variables.
answered
4 months ago
0
let
is used to declare a reference. let
keyword doesn't create an instruction, it simply replace reference by value. Example, everywhere I use x, compiler will replace it by 10.
let x = 10;
serialize_word(x);
equivalent to
serialize_word(10);
tempvar
is used to declare a variable. It means we will take memory to store my variable. And tempvar
keyword will create an instruction.
answered
4 months ago
Cairo: What is the difference between let and tempvar?
What is the difference between tempvar/let in Cairo Lang? How to use allow_locals and local?
Cairo: Difference between amount and gas_fee
Cairo Lang / StarkNet: What are Revoked references? What is alloc_locals?
How to write a function in Cairo Lang (StarkNet)?
What does ret do in Cairo?
What are Event Indexers in Cairo lang? What is a StarkNet indexer?
Fixed Point pow operation error
Does starknet have name service?
Cairo Error: Getting Error while setting value of item in array
How to create Structs in Cairo Lang for StarkNet?
Cairo: let vs tempvar what is the difference?
Cairo error for Unexpected '.' after ... which is future
What hashing functions are available in Cairo? How are they secure?