This week we had to work on creating discrete code blocks called functions. The idea behind using these functions is that they can be used repeatedly instead of having to write lots of individual pieces of code to achieve the same result. I did a few exercises to prepare myself as my pace of learning this is not that quick.
I found a cool resource here https://happycoding.io/tutorials/p5js/creating-functions to learn how to create functions. I had been watching the coding train videos which are great but found myself constantly rewatching the videos and pausing all the time. This on the other hand was not video based and was really great for me to grasp the concept.
The first thing I did was create an individual flower and then called the draw function for the flower. I typed out the original code in the tutorial to do this.
Once I did this I was able to create several more instances of the flower in different sizes and refresh the sketch to produce more flowers of differing sizes.
I then tried to use a function to create bees on the sketch but could not get this to work! I will work on this element to improve. I decided to try creating a new sketch using my own functions and made a house.
The next part of this challenge was to create multiple houses which I also need more time to figure out. The roof is separate from the main part of the house as a function, I would like to figure out how to change that and also give the individual parts color. That is what I have to get to grips with next.
I did the other exercise of replicating a sketch assigned to us by using functions.
Here are the links to the sketches I worked on this week.
I am getting to grips with functions but still need more practice to be fully aware of how a function functions!
One thought on “Creative Coding 1.3 Functioning Functions!”
I’m so glad the Happy Coding tutorial was useful for practicing functions this week. Thank you for documenting your process through each of the sketches.
If you keep working on the House sketch to draw multiple units, you might investigate drawing the door, windows, and roof in relation to the rect that frames the house. This is similar to drawing the flowers: each petal is drawn in relation to the central circle.
Here’s another example. In this sketch of trees, each tree consists of a trunk and an ellipse for the leaves. The tree trunks are drawn using the same variables for the leaves but with modifiers: https://editor.p5js.org/enickles/sketches/_5_ClvdxO
Keep me posted on how it goes!