ScratchData LogoScratchData
Back to kriblo_test's profile

Example: Recursion using an inner custom block

KRkriblo_test•Created June 29, 2021
Example: Recursion using an inner custom block
29
13
135 views
View on Scratch

Instructions

Recursive algorithms can be very powerful. But one of the key shortcomings of Scratch is that you can't use local variables in custom blocks. Having only "sprite only" variables makes certain recursive algorithms difficult to pull off, but not impossible. There are several ways to get around this issue (see examples below), but one way is to use an inner custom block. In the "Branch" custom block in this example we want to call the custom block recursively, to draw a branching tree. The next branch should start where the parent branch ends. If you use a sprite only variable, or even the built in x & y positions, they will have changed for successive calls on the same recursive level. The inputs of an inner procedure are truly local however, and solves this problem. See code inside. You might want to try it at TW: https://turbowarp.org/549260734 All code by me (@kriblo). Example: Simple recursive custom block: https://scratch.mit.edu/projects/539966894/ Example: Koch Curve using a stack in a recursive procedure: https://scratch.mit.edu/projects/528437799/ Example: Branching Recursion using Clones https://scratch.mit.edu/projects/540891095/

Project Details

Project ID549260734
CreatedJune 29, 2021
Last ModifiedMarch 22, 2022
SharedJune 29, 2021
Visibilityvisible
CommentsAllowed