NEWTON

NEWTON


Popular tags

    Cairo: let vs tempvar what is the difference?

    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?

      cairocairo-lang

    Hypobrai

    asked

    4 months ago


    2 answers

    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 lets, 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 lets to tempvars 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

    Your answer

    NEWTON

    NEWTON