NEWTON
Asked
6 months ago
47
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
6 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
6 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
6 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?
Will Kakarot be an L3 on top of the Starknet validity rollup?
Is Kakarot a Starkware project?
StarkNet Faucet: How can I get a Goerli-2 eth faucet tokens (StarkNet)?
What the etherscan equivalent for starknet is ?
Cairo Error calling function: Expected 'function_name' to be a struct. Found: 'function'.
Does anyone have an example of a positional request to `starknet_addInvokeTransaction` on devnet that is working?
What are gas fees in StarkNet?