# Recursive Functions Using Blocks

All the posts on recursion I’ve read is for triggers, but never for blocks somehow. In Python, C, Java, and many other languages, recursion is calling a function within itself, creating a sometimes, more efficient, loop. Recursion is one of two types of functions, the other being iterative functions, where instead of calling itself, it repeats a certain part of the code multiple times.

For example, a recursive function for calculating fibonacci in Python, would look like this:

while an iterative function would look like this:

In Gimkit, we can make recursive functions using blocks, by utilizing the “Broadcast message on channel” block.

First, we need to make a counter variable so the function doesn’t run indefinitely. In order to do this, we can make a property named “Counter”, and set it to 10 (or however number of times you want it to run).

Next, we need to make a lifecycle, that will run on channel “Start Game”, when the game starts.

In this example, we will be using a text as the means to access the block and the channel will be “Start Game”.
In order to make it stop after a certain amount of loops, we can make it so that if the counter > 0 it will run, if not, it will stop.

Now in that if statement, we can decrement the counter by one, and broadcast message on “Start Game” calling itself again.

Now directly before it decrements the counter, you can run whatever action you need to loop.

You are now finished, and can continue to explore this new idea of recursive functions with gimkit blocks

12 Likes

Wow! You are one of the few users who first post, is a guide!
Welcome btw

5 Likes

Welcome to the Forums! Please read FAQ and TOS, and the guidelines.
Alright this is a nice guide, and a bit complicated. (very good for a new user, I can see someone suspecting an alt)
By the way there are recursion guides on blocks I think.

Nice job! You are officially in the top 1% of new users who actually post a guide as their first post, instead of a code or a “please help me idk what to do wahhhh” type thing.
And on top of that, nice showcase of your amazing knowledge of Gimkit’s coding aspects.
Make sure to do the Discobot tutorial too, by the way.

3 Likes

oh ok, didn’t see any so thats why I made this one

1 Like

i have the same exact problem

1 Like

I’ve been doing gimkit for about a year, but programming for about 4 years tho. I just never had any major ideas for posting

4 Likes

(post deleted by author)

thats true, thx for the advice

OOOHHHH waitt…
Ok nevermind about that, sorry. I just realized this is a for loop. (kinda) Nvm!

So you need the counter for it to work. But then it’s not really recursion.
Also, this has been made before:

And also you could do this with a counter and a checker, so the block code would only be needed to do advanced stuff.

2 Likes

Ok, thanks for letting me know

1 Like

im almost certain this is bear.

bear??? who is bear…

1 Like

Don’t mind that, random coder on the Discord and used to be on Forums but… ahh… quit. [1]

1. who is possibly you ↩︎

1 Like

Really liked this! I do agree that this can be a fairly effective way to make loops. Sometimes if you’re setting a property in the function, you can just stop the loop once the length of the property is a certain number.

first off, new users don’t come making guides like this, they act like bear, bear was talking about cs earlier today and this user knew bear was a person (who) and not a what

Look at Rithekd and Clic, and a bunch of other people. Sure, it’s a possibility that it’s Bear in disguise, but it’s not exactly the most probable thing.

Hmm that does point to the Bear Alt Theory

That’s probably because of the context and the way you said:

This is bear, not this is a bear.

Most signs point to that it’s not bear.

1 Like

The one drawback to gimkit recursion is that there really aren’t functions, and so any “arguments” aren’t saved and we cant make programs like we can in any programming language.

1 Like

Why would raeb make an entire alt to post this

1 Like
2 Likes