Function Basics

https://editor.p5js.org/rootallroot/sketches/cZLs0rKzu

This week I learned that functions are really useful for organizing code, so I don’t have to put everything inside draw(). For example, I can create a drawFish() function and reuse it to draw many fish just by changing the x, y, and size. A function like bounce() can also reuse the same bouncing rule for different objects. I also learned that console.log() only shows me a value, while return gives the value back so I can use it later. Functions basically feel like small reusable tools that make my code cleaner and easier to work with.

Two reasons of function

bcaa552da1c6073183455b77aaef5855.jpg

c3897ec96b31c36227227642667e40c6.jpg

ed9a12d1e8d4e9723c795a859ad5856c.jpg

Recursive function

就是递归函数:一个 function 在自己里面再次调用自己。

Infinite Circles

https://editor.p5js.org/des8963/sketches/pX3sEbnuZ

Fractal Tree

https://editor.p5js.org/des8963/sketches/DJqJgfYf8