10. Function I
Click here to load reader
-
Upload
joseph-murphy -
Category
Education
-
view
42 -
download
2
Transcript of 10. Function I
![Page 1: 10. Function I](https://reader038.fdocuments.in/reader038/viewer/2022100603/5596338f1a28abca2e8b4840/html5/thumbnails/1.jpg)
From last time…
• Global vs. local variable scope!
• Making static assets with saveFrame()
![Page 2: 10. Function I](https://reader038.fdocuments.in/reader038/viewer/2022100603/5596338f1a28abca2e8b4840/html5/thumbnails/2.jpg)
Functions I
CAP 3032
![Page 3: 10. Function I](https://reader038.fdocuments.in/reader038/viewer/2022100603/5596338f1a28abca2e8b4840/html5/thumbnails/3.jpg)
Functions Reviewvoid setup() {} void draw() {}
• We can do two things with functions:!
! 1. call a function:!
! ! size(500,500);
2. define a function:!
! ! void size(int width, int height) {}
![Page 4: 10. Function I](https://reader038.fdocuments.in/reader038/viewer/2022100603/5596338f1a28abca2e8b4840/html5/thumbnails/4.jpg)
Defining a Function
returnType functionName( argumentList ) { // some code goes here }
the arguments we can pass in
the type of value we expect to be ‘returned’
![Page 5: 10. Function I](https://reader038.fdocuments.in/reader038/viewer/2022100603/5596338f1a28abca2e8b4840/html5/thumbnails/5.jpg)
the arguments we will pass inthe variable type we expect to be ‘returned’
1. Function Name: a name!
2. Input: the variable types of arguments we will pass in!
3. Output: the variable type that will be returned ( void signifies that we do not expect returned value )
returnType functionName( argumentList ) { // some code goes here }
![Page 6: 10. Function I](https://reader038.fdocuments.in/reader038/viewer/2022100603/5596338f1a28abca2e8b4840/html5/thumbnails/6.jpg)
Demo!square() function
![Page 7: 10. Function I](https://reader038.fdocuments.in/reader038/viewer/2022100603/5596338f1a28abca2e8b4840/html5/thumbnails/7.jpg)
Why functions?• Functions organize code in a way that humans
can understand!
• Modularity: smaller, more readable, more manageable bits of code; it’s debuggable!!
• Reusability: you can reuse code within a sketch (two pong paddles) or between sketches
![Page 8: 10. Function I](https://reader038.fdocuments.in/reader038/viewer/2022100603/5596338f1a28abca2e8b4840/html5/thumbnails/8.jpg)
ExampleOrganized into functions vs. pure chaos
![Page 9: 10. Function I](https://reader038.fdocuments.in/reader038/viewer/2022100603/5596338f1a28abca2e8b4840/html5/thumbnails/9.jpg)
Time Permitting!
Demo!Make Pong Functions
![Page 10: 10. Function I](https://reader038.fdocuments.in/reader038/viewer/2022100603/5596338f1a28abca2e8b4840/html5/thumbnails/10.jpg)
For next time…• Read Shiffman, p. 107–119 (Functions II)!
• Continue (or start) working on Iteration 1
Quiz 2!Variables, Conditionals, & Loops