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

**URL:** <https://community.starknet.io/t/can-someone-provide-a-simple-loop-example-which-calls-another-function/2122>\
**Category:** Starknet Technical Development\
**Tags:** cairo\
**Created:** [October 9, 2022, 12:22pm UTC](https://community.starknet.io/t/can-someone-provide-a-simple-loop-example-which-calls-another-function/2122 "2022-10-09T12:22:00Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![remedcu](https://dub1.discourse-cdn.com/flex005/user_avatar/community.starknet.io/remedcu/32/962_2.png) [@remedcu](https://community.starknet.io/u/remedcu)\
**Post date:** [October 9, 2022, 12:22pm UTC](https://community.starknet.io/t/can-someone-provide-a-simple-loop-example-which-calls-another-function/2122/1 "2022-10-09T12:22:00Z")

</div>

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](https://gist.github.com/agolajko/77faf455f94fd674f1fb50c2b196b66d), but it is not working (still in the learning phase, so not sure what to do).

My current implementation is somewhat like the below:

```auto
@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.

---

<div class="post-metadata">

**Author:** ![gaetbout](https://dub1.discourse-cdn.com/flex005/user_avatar/community.starknet.io/gaetbout/32/712_2.png) [@gaetbout](https://community.starknet.io/u/gaetbout)\
**Post date:** [October 9, 2022, 8:47pm UTC](https://community.starknet.io/t/can-someone-provide-a-simple-loop-example-which-calls-another-function/2122/2 "2022-10-09T20:47:21Z")

</div>

Hi ser,

Have a look here

> **[cairo-streams/src/onlydust/stream/internal at main · onlydustxyz/cairo-streams](https://github.com/onlydustxyz/cairo-streams/tree/main/src/onlydust/stream/internal)**
>
> main/src/onlydust/stream/internal

Hope it helps!  
G.

---

<div class="post-metadata">

**Author:** ![remedcu](https://dub1.discourse-cdn.com/flex005/user_avatar/community.starknet.io/remedcu/32/962_2.png) [@remedcu](https://community.starknet.io/u/remedcu)\
**Post date:** [October 9, 2022, 9:10pm UTC](https://community.starknet.io/t/can-someone-provide-a-simple-loop-example-which-calls-another-function/2122/3 "2022-10-09T21:10:41Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![hacxx](https://avatars.discourse-cdn.com/v4/letter/h/a88e4f/32.png) [@hacxx](https://community.starknet.io/u/hacxx)\
**Post date:** [May 20, 2025, 2:12pm UTC](https://community.starknet.io/t/can-someone-provide-a-simple-loop-example-which-calls-another-function/2122/5 "2025-05-20T14:12:56Z")

</div>

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

```auto
@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.
