11. functions ii
DESCRIPTION
TRANSCRIPT
From last time…• Quiz 2: Variables, Conditionals, & Loops!
• We started to organize our sketches by defining functions and simplifying our draw loop!
• I sent out / posted expectations for Iteration 1!
• I’ve cancelled class next Monday, 10 Feb 2014
Iteration 1
CAP 3032
Expectations• We will have 5 groups:!! 1 GenArt, 1 DataViz, and 3 Game groups!
• Mon, 17 Feb: you will show Iteration 1 to your group and take notes (4–5 minutes per student)!
• Wed, 19 Feb: you will submit your code, the notes you took in class, and an Iteration 2 plan before class (probably via Sakai…)
Feedback
• What works? What do you like?!
• What doesn’t work? How could it be improved?!
• Any other thoughts or comments?
The Iterative Design Process
Empathize
Define
Ideate
Prototype
Test
Functions II
CAP 3032
Functions
• Functions allow Modularity and Reusability!
• They allow us to have code which follows the principle of DRY: “Don’t Repeat Yourself”
The Pragmatic Programmer!Hunt & Thomas on the Principle of Don’t Repeat Yourself (DRY)
“Every piece of knowledge must have a single, unambiguous, authoritative
representation within a system.”
DRY vs. WET• Non-DRY coding is often called WET coding:!
! - Write Everything Twice!
! - We Enjoy Typing!!
• If you are repeating yourself in code, you should automatically start thinking about functions…
Functions
• Memorize the structure of a function!!
• You need to be able to “read” functions
returnType functionName( argumentList ) { // the function code is here }
Random Fill Color
void setRandomRGBFillColor() { fill(random(255),random(255),random(255); }
“This function takes no arguments, sets the fill color to a random RGB color, and returns nothing.”
Random Positionsfloat randomXPosition() { return random(width); } !
float randomYPosition() { return random(height); }
“This function takes no arguments, generates a random value based on width, and returns a float.”
Demo!Square & Circle
Arguments• When need a function to use a value that we give
to it, we give it arguments!
• size(400,300); has two arguments!
• Arguments are said to be “passed” to the function, and must follow some rules:!
! - they must have the correct number of arguments!
! - they must have the correct type of arguments
Passed by Copy• When arguments are passed, they are said to be
“passed by copy”!
• This means that your argument is duplicated (copied) and the copy is handed to the function!
• The function can use/abuse the copy and return a value, without ever affecting the original value passed in
Demo!Passing An Argument
For next time…
• Read Shiffman, p. 121–130 (Objects I)!
• Two weeks until Iteration 1 is due