Discuss Scratch
- Discussion Forums
- » Help with Scripts
- » Recursive functions
- starmega43
-
18 posts
Recursive functions
Hello,
I wanted to ask if why is it not possible to make recursive functions in scratch. If it is could someone tell me how to make one?
I wanted to ask if why is it not possible to make recursive functions in scratch. If it is could someone tell me how to make one?
- SpyCoderX
-
1000+ posts
Recursive functions
It is possible to make recursive functions in Scratch! (Be warned that if the recursion lasts too long Scratch will lag.)
To define a recursive function:
To use the function:
Grey blocks are stuff dependent on what you want to do.
To define a recursive function:
define myFunction //Make sure to check "run without screen refresh".
if <should stop?::grey> then
stop [this script v]
end
your code goes here ::grey
myFunction
To use the function:
myFunction ::custom
Grey blocks are stuff dependent on what you want to do.
- colinmacc
-
1000+ posts
Recursive functions
This discussion may help - https://scratch.mit.edu/discuss/post/8251364/
- 50_scratch_tabs
-
1000+ posts
Recursive functions
(#3)Um…that's this post.
This discussion may help - https://scratch.mit.edu/discuss/post/8251364/
Oh, I see what you're doing!
Last edited by 50_scratch_tabs (Nov. 26, 2024 00:50:33)
- colinmacc
-
1000+ posts
Recursive functions
(#3)Um…that's this post.
This discussion may help - https://scratch.mit.edu/discuss/post/8251364/
Oh, I see what you're doing!
Sorry couldn’t help myself

Last edited by colinmacc (Nov. 26, 2024 00:58:02)
- ItBeJC
-
1000+ posts
Recursive functions
That was magnificent.(#3)Um…that's this post.
This discussion may help - https://scratch.mit.edu/discuss/post/8251364/
Oh, I see what you're doing!
Sorry couldn’t help myself
- bsteichman
-
500+ posts
Recursive functions
I don't think run without screen refresh needs to be checked It is possible to make recursive functions in Scratch! (Be warned that if the recursion lasts too long Scratch will lag.)
To define a recursive function:define myFunction //Make sure to check "run without screen refresh".
if <should stop?::grey> then
stop [this script v]
end
your code goes here ::grey
myFunction
To use the function:myFunction ::custom
Grey blocks are stuff dependent on what you want to do.
also…
example of recursive Fibonacci sequence
define fibb (a) (b)
add (a) to [list v]
if <(a) < [100]> then
fibb (b) ((a) + (b))
end
delete all of [list v]
fibb (0) (1)
Last edited by bsteichman (Nov. 26, 2024 03:17:24)
- RokCoder
-
1000+ posts
Recursive functions
As mentioned -
1) @colinmacc's response is off the scale
2) Custom blocks can recurse because their parameters are stored locally to the custom block
Unfortunately, “this sprite only” variables are stored locally to the sprite and not the function so changing the value of a variable at any level of the recursion changes it for all levels of the recursion. You can work around this by pushing and popping function-scoped variables to lists.
1) @colinmacc's response is off the scale

2) Custom blocks can recurse because their parameters are stored locally to the custom block
Unfortunately, “this sprite only” variables are stored locally to the sprite and not the function so changing the value of a variable at any level of the recursion changes it for all levels of the recursion. You can work around this by pushing and popping function-scoped variables to lists.
- kriblo
-
100+ posts
Recursive functions
With regards to the original question… Yes, it's possible, in several ways.
Here's a simple example of recursion, using custom blocks and plenty of comments in the code:
Example: Simple recursive custom block: https://scratch.mit.edu/projects/539966894/
Here's another example using an inner custom block, to make it simpler to use.
Example: Recursion using an inner custom block: https://scratch.mit.edu/projects/549260734/
Here's and example using custom blocks and using lists for “stack”. A stack is used to store values for variables in each instance of the call:
Example: Koch Curve using a stack in a recursive procedure: https://scratch.mit.edu/projects/528437799/
And finally, here's an example of recursion, using clones creating clones:
Example: Branching Recursion using Clones: https://scratch.mit.edu/projects/540891095/
Hope that helps.
Here's a simple example of recursion, using custom blocks and plenty of comments in the code:
Example: Simple recursive custom block: https://scratch.mit.edu/projects/539966894/
Here's another example using an inner custom block, to make it simpler to use.
Example: Recursion using an inner custom block: https://scratch.mit.edu/projects/549260734/
Here's and example using custom blocks and using lists for “stack”. A stack is used to store values for variables in each instance of the call:
Example: Koch Curve using a stack in a recursive procedure: https://scratch.mit.edu/projects/528437799/
And finally, here's an example of recursion, using clones creating clones:
Example: Branching Recursion using Clones: https://scratch.mit.edu/projects/540891095/
Hope that helps.
- colinmacc
-
1000+ posts
Recursive functions
I mean ultimately… I was right. This thread IS helpful!
- GoofyCoder12314
-
2 posts
Recursive functions
Is recursion banned on scratch, im working on a quicksort algorithm on scratch rn and it uses recursion, I've heard its banned tho
- kriblo
-
100+ posts
Recursive functions
No, it's not banned. Is recursion banned on scratch?
- Discussion Forums
- » Help with Scripts
-
» Recursive functions