Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve documentation for recursion example #991

Merged
merged 1 commit into from
Apr 10, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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