CSc 101: Section 10 – Functions

Now that we know a bit about functions, let’s write some! If you need it, pull up the function slides from lecture for reference.

Turn in at least 4 of the problems to the D2L dropbox.

Problem 1

Create a processing program called "tree" that draws a tree similar to the one shown above on a 200x200 canvas. We'll use this dree-drawing code throughout the next few problems.


Problem 2

Next, modify the code you wrote in the first problem so that the tree is not drawn within the draw() function. Instead, write a new function named drawTree(). This function should take two parameters - the X and Y coordinate to draw the tree at. Once you've written this function, test it out by calling it a few times.


Problem 3

Now, create a new processing program called "bush" (but keep the tree one around too - we'll need it later on). In this new program, write a function that draws a buch called drawBush(). This function should have three parameters: The X coordinate, the Y coordinate, and single number that will be used to determine the brightness of the bush. You can use this parameter when you set the fill color of the shapes used to draw it.


Problem 4

Create yet another new program called "forest". Copy the drawTree and drawBush functions from your other program into this one. Then, call each of these functions 3-4 times to draw forest, like the one in the picture to the left.


Problem 5

Create one more new processing program called "bird". In this one, you will write a function to draw a very simple "bird" in the sky, like you can see in the picture. This function should be called drawBird, and will take three parameters: The first and second will be the X and Y coordinates of the bird. The third parameter will be a boolean variable. If the value is true, the wings of the bird should be drawn upward. Otherwise, draw the wings downward. Use an if-statement in your function to determine which type of bird to draw. The picture to the left shows three birds being draw (3 calls to the function). Two of them have their wings up, the other has wings down.


Problem 6

Now, copy the drawBird function and add it into your "forest" program. Once you do so, call it a few times to draw some birds in the sky.


Download Solutions