NEWTON

NEWTON


Popular tags

    How to write a function in Cairo Lang (StarkNet)?

    Asked

    4 months ago

    238

    views


    3

    Hello! This is Day 2 of the 17 days of the Cairo challenge. And I have no idea how to solve the playground exercise “Functions”. Perhaps you can help me?

    // The following code is missing the function `add1`. // 1. Click on "Run" to see the error. // 2. Write the function `add1`, based on `add1_square`, so // that the program terminates successfully. // // Note that `add1` doesn't have to call other functions. // // You can learn more about functions [here](https://www.cairo-lang.org/docs/how_cairo_works/functions.html). // Write your code here. func add1_square(x: felt) -> (x: felt) { // Call `add1` and unpack the result into `z`. let (z) = add1(y=x); return (x=z * z); } func main() { // Call `add1_square` and save the result into `res`. let (res) = add1_square(x=12); // Verify the computation. assert res = (12 + 1) * (12 + 1); return (); }

    Answers to this question are a part of the ✨ 17 days of Cairo Lang with Playground & Newton. ✨ 

    Vote for your favorite answer - the best answer will win a $10 award. A new day – a new reward! During the next 17 days, our goal is to attract more developers to the Cairo language and to systematize the knowledge of Cairo lang. Read rules

      #17daysOfCairocairo-beginnerscairoplayground

    Newton

    asked

    4 months ago


    13 answers

    4

    Accepted answer

    Problem

    // The following code is missing the function `add1`.
    // 1. Click on "Run" to see the error.
    // 2. Write the function `add1`, based on `add1_square`, so
    //    that the program terminates successfully.
    func add1_square(x: felt) -> (x: felt) {
        // Call `add1` and unpack the result into `z`.
        let (z) = add1(y=x);
        return (x=z * z);
    }
    
    func main() {
        // Call `add1_square` and save the result into `res`.
        let (res) = add1_square(x=12);
    
        // Verify the computation.
        assert res = (12 + 1) * (12 + 1);
        return ();
    }
    

    Solution

    func add1_square(x: felt) -> (x: felt) {
        // Call `add1` and unpack the result into `z`.
        let (z) = add1(y=x);
        return (x=z * z);
    }
    
    func main() {
        // Call `add1_square` and save the result into `res`.
        let (res) = add1_square(x=12);
    
        // Verify the computation.
        assert res = (12 + 1) * (12 + 1);
        return ();
    }
    func add1(y:felt) -> (z:felt) {
        return (z=y + 1);
    }
    
    

    Output

    Number of steps: 10
    Program hash: 0x03856cfae0d569a83c0e2c9b9524c1e8a42730ccd24ca449ed415923a4c76fb0
    

    Explanation

    Let’s look at the flow of how this program works.

    In main function

    The main function calls the add1_square function with one parameter named x which holds value 12. The controls go to the function add1_square.

    let (res) = add1_square(x=12);
    

    In add1_square function

    The add1_square accepts one argument x of value 12 and returns one argument.It calls add1 function and control goes to the add1.

    func add1_square(x: felt) -> (x: felt){ //x=12
    let (z) = add1(y=x); // y=12
    return (x=z * z);}
    

    In add1 function

    add1 function accepts one argument y which is 12(see above) and returns one argument z. The z returns the value y+1 which is 13, from where it is called which is add1_square function.

    In add1_square function

    x value(z * z) returned to where it is called that is the main function. res holds the square of z which is 13*13.

        let (z) = add1(y=x); //z=13
        return (x=z * z);//x=13*13
    

    At the end computation is verified which turns out to be true as res holds the value x which is 13*13.

    assert res = (12 + 1) * (12 + 1); //13*13=13*13 (checking if two variables are same)
    

    Cairo's Assert statements can be used for 2 different use cases:

    1. Check if two variables are the same
    2. Set a variable's value if it's currently not set

    Here, it's checking if two variables are the same as res is already defined.

    You can also use serialize_word to print the res.

    With serialize_word

    // Use the output builtin.
    %builtins output
    
    // Import the serialize_word() function.
    from starkware.cairo.common.serialize import serialize_word
    
    func add1_square(x: felt) -> (x: felt) {
        // Call `add1` and unpack the result into `z`.
        let (z) = add1(y=x);
        return (x=z * z);
    }
    
    func main{output_ptr: felt*}() {
        // Call `add1_square` and save the result into `res`.
        let (res) = add1_square(x=12);
        serialize_word(res);
        // Verify the computation.
        assert res = (12 + 1) * (12 + 1);
        return ();
    }
    func add1(y:felt) -> (z:felt) {
        return (z=y + 1);
    }
    
    

    Output

    Program output:
      169
    
    Number of steps: 16
    Program hash: 0x076e558dcac2872eb0c875a77c1f7be6401f606d62abbe327c93a36652228ab9
    

    Ishita Rastogi

    answered

    4 months ago

    0

    You learn to code by copy pasting

    You can pretty much solve this challenge by copying the syntax of the previous.

    Copy this function:

    func add1_square(x: felt) -> (x: felt) {
        // Call `add1` and unpack the result into `z`.
        let (z) = add1(y=x);
        return (x=z * z);
    }
    

    On thing I think that could be confusing is that you need to declare a variable name for function parameters and return values. But basically it can be the same! (here x & x).

    Good developers copy, great developers steal.

    So here is our theft:

    // Write your code here.
    func add1(y: felt) -> (y: felt) {
        return (y=y+1);
    }
    

    Note that you need to call the variable y because higher up in the program it's called as such. (It's when add1(y=x) is called).

    1

    It is required to add add1 function before add1_square:

    func add1(y: felt) -> (y: felt) {
        return (y=y + 1);
    }
    

    By looking at the usage of add1 in add1_square, we see that the required argument should be called y and should be of type felt. Also, since the add1 is simple, the increment is done within the return statement without sacrificing readability.

    Output

    Number of steps: 10
    Program hash: 0x00866f10be8849f5d7e293e35c35ae1b178820a025edf7da20a8b64e3e7601a8
    

    0

    We need to add this function:

    func add1(y: felt) -> (z: felt) {
        return (z = y + 1);
    }
    

    0xac3d...5d2902

    answered

    4 months ago

    0

    // The following code is missing the function add1. // 1. Click on "Run" to see the error. // 2. Write the function add1, based on add1_square, so // that the program terminates successfully. // // Note that add1 doesn't have to call other functions. // // You can learn more about functions here.

    // Write your code here.

    func add1_square(x: felt) -> (x: felt) { // Call add1 and unpack the result into z. let (z) = add1(y=x); return (x=z * z); }

    func main() { // Call add1_square and save the result into res. let (res) = add1_square(x=12);

    // Verify the computation.
    assert res = (12 + 1) * (12 + 1);
    return ();
    

    }

    func add1(y : felt) -> (z:felt){ return (z = y + 1); }

    0xf2c1...A516b0

    answered

    4 months ago

    1

    The goal is to implement :

    f : x -> (x+1) * (x+1)
    

    The square function is already implemented, so we just add to implement the add1 function which is :

    f : x -> x+1
    

    Here is the implementation :

    func add1(y : felt) -> (z : felt):
        return (z=y + 1)
    end
    

    As asked, we unpacked the result into z.

    0xd0Ff...340534

    answered

    4 months ago

    1

    Easy way to implement the function add1 is to look at the current function signature in the exercice:

    add1(y=x)
    

    The function you need to implement will have the name add1 and take a named input argument called y. From the rest of the exercice, you can deduce that the function will need to add one to the input parameter and return it (you can see this from assert res = (12 + 1) * (12 + 1);).

    From this we can build the following function:

    func add1(y : felt) -> (res : felt){
        return(res=y+1);
    }
    

    which simply adds one to the input parameter and returns it.

    answered

    4 months ago

    1

    function add1_square receives x as input and returns
    (x+1)^2, we need function add1 increase input by one before perform squaring.

    func add1(y:felt) -> (output: felt){
        return(output=y+1);
    }
    

    //

    answered

    4 months ago

    Your answer

    NEWTON

    NEWTON