home/ atoms/ processing-recursion

Recursive functions in Processing call themselves to generate self-similar, branching forms

A recursive function calls itself as part of its own execution. Each call must move toward a base case — a condition that stops the recursion — otherwise the program enters an infinite loop and crashes. In visual programming, recursion generates self-similar (fractal-like) structures: a drawBranch() function calls drawBranch() with a smaller radius, creating nested circles or tree branches. The depth of recursion controls how many levels of detail are generated. Processing’s stack is finite, so deep recursion (>few hundred levels) will cause a StackOverflowError.

Examples

void drawCircle(float x, float y, int radius, int num) { ellipse(x, y, radius2, radius2); if (num > 1) { drawCircle(x+20, y, radius/2, num-1); } }

Assessment

Trace the call stack for drawCircle(50,50,40,3); draw the three circles that would appear; identify what would happen if the base case (num > 1) were removed.

“if (num > 1) { num = num - 1; int branches = int(random(2, 6));”
corpus · processing-handbook-no-login-mirror-pdf-reas-and-fry · chunk 47