Skip to content

Commit

Permalink
Improve documentation for recursion example
Browse files Browse the repository at this point in the history
  • Loading branch information
Swapnil-2001 committed Apr 2, 2021
1 parent fd318c1 commit 23ee392
Showing 1 changed file with 12 additions and 4 deletions.
16 changes: 12 additions & 4 deletions src/data/examples/en/00_Structure/08_Recursion.js
Original file line number Diff line number Diff line change
@@ -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() {
Expand All @@ -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);
}
Expand Down

0 comments on commit 23ee392

Please sign in to comment.