Bounce Coding - Step 3 - Functions

If this is your first time reading about Bounce Coding, I recommend you start from the beginning.

Functions

A function is a chunk of code that we can pass arguments to. It encapsulates a behavior of our choosing. In fact, we've already been using one: ellipse. ellipse is a function that draws an ellipse to the screen given a center location and a width and height. Since we're drawing circles, let's write a circle function. We can define a function that accepts three arguments (centerx, centery and radius) and draws a circle with that information using the ellipse function.  

Things to note

  • Notice how we can now run the circle function multiple times to draw multiple circles. Functions allow us to get away with writing less code. If we needed to make changes to the circle function, we'd only need to make that change in one place rather than needing to change every chunk of code had we just copy and pasted our circle code multiple times. Circle is so simple that this may seem trivial, but in more complex programs, this is a life saver.
  • For more information on functions, check out Khan Academy's tutorial.