11. functions ii

18
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

Upload: joseph-murphy

Post on 24-Dec-2014

44 views

Category:

Education


5 download

DESCRIPTION

 

TRANSCRIPT

Page 1: 11. Functions II

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

Iteration 1

CAP 3032

Page 3: 11. Functions II

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

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

The Iterative Design Process

Empathize

Define

Ideate

Prototype

Test

Page 6: 11. Functions II

Functions II

CAP 3032

Page 7: 11. Functions II

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

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

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

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

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

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

Demo!Square & Circle

Page 14: 11. Functions II

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

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

Demo!Passing An Argument

Page 18: 11. Functions II

For next time…

• Read Shiffman, p. 121–130 (Objects I)!

• Two weeks until Iteration 1 is due