Can someone provide a simple loop example which calls another function?

I am trying to call a function for testing using protostar, and up until now, I tried it with recursion, but it always limits to around 80 calls.

Can someone share an example of a loop implementation?

I tried this link, but it is not working (still in the learning phase, so not sure what to do).

My current implementation is somewhat like the below:

@view
func test_loop{bitwise_ptr: BitwiseBuiltin*, range_check_ptr}() {
    tempvar n = 5;
    loop_start:
    test_function_to_call();
    tempvar n = n - 1;
    jmp loop_start if n != 0;
    return ();
}

I need to call test_function_to_call() at least n times.

18 Likes

Hi ser,

Have a look here

Hope it helps!
G.

14 Likes

Hey @gaetbout

Thanks for the link. I quickly looked, but I think this might not solve my problem.

  1. The foreach and other function still uses recursion. In my case, as n can be a big number, it is hitting the limit I think.
  2. The functions require an array, whereas in my case, it just requires a call to a function without any parameter n times.

Appreciate the help and will be saving the link for future use cases.

13 Likes

Here’s a simple loop example in Cairo calling another function:

@view
func test_loop{bitwise_ptr: BitwiseBuiltin*, range_check_ptr}() {
    let mut n = 5;
    loop {
        test_function_to_call();
        n = n - 1;
        if n == 0 {
            break;
        }
    }
    return ();
}

Make sure test_function_to_call is defined and callable. Recursion limits occur due to call stack size, so loops avoid that. Also, confirm Protostar config supports your syntax and try using let mut for mutable variables instead of tempvar in loops.