11. functions ii
DESCRIPTION
TRANSCRIPT
![Page 1: 11. Functions II](https://reader033.vdocuments.us/reader033/viewer/2022050808/549b0d9ab4795910098b4693/html5/thumbnails/1.jpg)
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
![Page 2: 11. Functions II](https://reader033.vdocuments.us/reader033/viewer/2022050808/549b0d9ab4795910098b4693/html5/thumbnails/2.jpg)
Iteration 1
CAP 3032
![Page 3: 11. Functions II](https://reader033.vdocuments.us/reader033/viewer/2022050808/549b0d9ab4795910098b4693/html5/thumbnails/3.jpg)
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…)
![Page 4: 11. Functions II](https://reader033.vdocuments.us/reader033/viewer/2022050808/549b0d9ab4795910098b4693/html5/thumbnails/4.jpg)
Feedback
• What works? What do you like?!
• What doesn’t work? How could it be improved?!
• Any other thoughts or comments?
![Page 5: 11. Functions II](https://reader033.vdocuments.us/reader033/viewer/2022050808/549b0d9ab4795910098b4693/html5/thumbnails/5.jpg)
The Iterative Design Process
Empathize
Define
Ideate
Prototype
Test
![Page 6: 11. Functions II](https://reader033.vdocuments.us/reader033/viewer/2022050808/549b0d9ab4795910098b4693/html5/thumbnails/6.jpg)
Functions II
CAP 3032
![Page 7: 11. Functions II](https://reader033.vdocuments.us/reader033/viewer/2022050808/549b0d9ab4795910098b4693/html5/thumbnails/7.jpg)
Functions
• Functions allow Modularity and Reusability!
• They allow us to have code which follows the principle of DRY: “Don’t Repeat Yourself”
![Page 8: 11. Functions II](https://reader033.vdocuments.us/reader033/viewer/2022050808/549b0d9ab4795910098b4693/html5/thumbnails/8.jpg)
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.”
![Page 9: 11. Functions II](https://reader033.vdocuments.us/reader033/viewer/2022050808/549b0d9ab4795910098b4693/html5/thumbnails/9.jpg)
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…
![Page 10: 11. Functions II](https://reader033.vdocuments.us/reader033/viewer/2022050808/549b0d9ab4795910098b4693/html5/thumbnails/10.jpg)
Functions
• Memorize the structure of a function!!
• You need to be able to “read” functions
returnType functionName( argumentList ) { // the function code is here }
![Page 11: 11. Functions II](https://reader033.vdocuments.us/reader033/viewer/2022050808/549b0d9ab4795910098b4693/html5/thumbnails/11.jpg)
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.”
![Page 12: 11. Functions II](https://reader033.vdocuments.us/reader033/viewer/2022050808/549b0d9ab4795910098b4693/html5/thumbnails/12.jpg)
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.”
![Page 13: 11. Functions II](https://reader033.vdocuments.us/reader033/viewer/2022050808/549b0d9ab4795910098b4693/html5/thumbnails/13.jpg)
Demo!Square & Circle
![Page 14: 11. Functions II](https://reader033.vdocuments.us/reader033/viewer/2022050808/549b0d9ab4795910098b4693/html5/thumbnails/14.jpg)
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
![Page 15: 11. Functions II](https://reader033.vdocuments.us/reader033/viewer/2022050808/549b0d9ab4795910098b4693/html5/thumbnails/15.jpg)
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
![Page 16: 11. Functions II](https://reader033.vdocuments.us/reader033/viewer/2022050808/549b0d9ab4795910098b4693/html5/thumbnails/16.jpg)
Demo!Passing An Argument
![Page 18: 11. Functions II](https://reader033.vdocuments.us/reader033/viewer/2022050808/549b0d9ab4795910098b4693/html5/thumbnails/18.jpg)
For next time…
• Read Shiffman, p. 121–130 (Objects I)!
• Two weeks until Iteration 1 is due