Discuss Scratch

starmega43
Scratcher
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?
SpyCoderX
Scratcher
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:
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
Scratcher
1000+ posts

Recursive functions

50_scratch_tabs
Scratcher
1000+ posts

Recursive functions

colinmacc wrote:

(#3)
This discussion may help - https://scratch.mit.edu/discuss/post/8251364/
Um…that's this post.
Oh, I see what you're doing!

Last edited by 50_scratch_tabs (Nov. 26, 2024 00:50:33)

colinmacc
Scratcher
1000+ posts

Recursive functions

50_scratch_tabs wrote:

colinmacc wrote:

(#3)
This discussion may help - https://scratch.mit.edu/discuss/post/8251364/
Um…that's this post.
Oh, I see what you're doing!

Sorry couldn’t help myself

Last edited by colinmacc (Nov. 26, 2024 00:58:02)

ItBeJC
Scratcher
1000+ posts

Recursive functions

colinmacc wrote:

50_scratch_tabs wrote:

colinmacc wrote:

(#3)
This discussion may help - https://scratch.mit.edu/discuss/post/8251364/
Um…that's this post.
Oh, I see what you're doing!

Sorry couldn’t help myself
That was magnificent.
bsteichman
Scratcher
500+ posts

Recursive functions

SpyCoderX wrote:

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.
I don't think run without screen refresh needs to be checked


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
Scratcher
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.
kriblo
Scratcher
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.
colinmacc
Scratcher
1000+ posts

Recursive functions

I mean ultimately… I was right. This thread IS helpful!
GoofyCoder12314
Scratcher
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
Scratcher
100+ posts

Recursive functions

GoofyCoder12314 wrote:

Is recursion banned on scratch?
No, it's not banned.

Powered by DjangoBB