From 23ee392ea061a380e094134421373d2345c4edef Mon Sep 17 00:00:00 2001 From: Swapnil Sengupta Date: Sat, 3 Apr 2021 00:51:18 +0530 Subject: [PATCH] Improve documentation for recursion example --- .../examples/en/00_Structure/08_Recursion.js | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) diff --git a/src/data/examples/en/00_Structure/08_Recursion.js b/src/data/examples/en/00_Structure/08_Recursion.js index 2a7a486036..a16e46b054 100644 --- a/src/data/examples/en/00_Structure/08_Recursion.js +++ b/src/data/examples/en/00_Structure/08_Recursion.js @@ -1,8 +1,10 @@ /* *@name Recursion *@description A demonstration of recursion, which means functions call themselves. - * Notice how the drawCircle() function calls itself at the end of its block. - * It continues to do this until the variable "level" is equal to 1. + * A recursive function must have a terminating condition, without which it will + * go into an infinite loop. Notice how the drawCircle() function calls itself + * at the end of its block. It continues to do this until the variable "level" is + * equal to 1. */ function setup() { @@ -16,11 +18,17 @@ function draw() { } function drawCircle(x, radius, level) { + // 'level' is the variable that terminates the recursion once it reaches + // a certain value (here, 1). If a terminating condition is not + // specified, a recursive function keeps calling itself again and again + // until it runs out of stack space - not a favourable outcome! const tt = (126 * level) / 4.0; fill(tt); ellipse(x, height / 2, radius * 2, radius * 2); - if (level > 1) { - level = level - 1; + if (level > 1) { + // 'level' decreases by 1 at every step and thus makes the terminating condition + // attainable + level = level - 1; drawCircle(x - radius / 2, radius / 2, level); drawCircle(x + radius / 2, radius / 2, level); }