ScratchData LogoScratchData
Back to johnm's profile

Tinkering with recursion

JOjohnm•Created February 8, 2013
Tinkering with recursion
2
2
306 views
View on Scratch

Instructions

Before you run this, take a look at the scripts. What do you think the "spin" procedure will do? A procedure that calls itself, like "spin", is called "recursive". In this case, "spin" makes the fish turn a little, then calls "spin" which makes the fish turn a little, then calls spin... As you can see, this is an endless process, like a "forever" loop. Actually, it's not quite as efficient as a forever loop. Each time spin is called, it uses up a tiny amount of computer memory to remember the place from which it was called so it can return there later. Thus, if you ran this program for a long time, it would eventually use up all the available memory and you'd get an error. However, since it makes only one recursive call on each display frame and Scratch runs at 30 frames per second, this program can run for many hours without running out of memory. (But in a long-running animation or game, it's better to use forever loops for your endless loops to avoid consuming memory unnecessarily.) Note: If you combine an endless recursion with the "run without screen refresh" option, you can run out of memory in just a few minutes. If you try that, as I did, be prepared to restart your browser... But as long as you avoid the "run without screen refresh" option, you can experiment with recursive procedures, including "infinite recursion", without worrying about running out of memory.

Project Details

Project ID10040430
CreatedFebruary 8, 2013
Last ModifiedFebruary 8, 2013
SharedFebruary 8, 2013
Visibilityvisible
CommentsAllowed