Recitation 7: Functions and Arrays – Zhao Yang

Step 1:

For step 1, I created a function based on what we learned in class, which can draw a crying face when executing it. This function requires 5 parameters, which are x coordinate, y coordinate, the size of the face, the color of the face, and the color of the tears. 

Step 2:

Step 3:

Step 4:

Additional step:

For the additional step, I created a keyPressed function. If you press the space on the keyboard, it will reassign color to each face and its tears. 

And here is my code for all the steps above. 

https://github.com/JooooosephY/Recitation-7/blob/master/recitation7.pde

Questions:

  1. The function of the for loop in step 2 is to display 100 instances of my graphic. But where the for loop is really influences the output. If I write the for loop in the setup() function, the for loop will be executed only once. Thus, the output will be that there will display 100 graphics with a fixed position. However, if I write the for loop in the draw() function, it will become a nested loop. Since the draw() function itself is a loop function, if we still add a for loop in the draw() function, every time when the draw() function is executed the for loop will be executed once. Hence, the output is that each graphic will be assigned a different position every time. As we can see in the video of step 2, the image keeps changing because of the nested loop. 
  2. By using the array, we can easily assign different parameters to various similar objects at one time instead of creating many variables and assigning different parameters one by one. Furthermore, using an array can make the code more readable without any unnecessary and tedious variables. And arrays can hold any type of data and each element can be individually assigned and read. If I’ll use the array in my project, I would probably use it to create a game. For example, I would generate a certain amount of enemies with same outlook. And they have the same way to attack the character that we can control. And the array can be used to store the parameters such as their heights. Since they have similar outlooks but they are different from each other, using the array is a good way to assign them with different parameters. 

Leave a Reply