Procedures and Code Reuse
cs 195/107 Notes
A procedure is a named sequence of blocks
.Define it, then call it
once you define a procedure, you can call it from anywhere in your program.Why would I define a procedure?
- Compartmentalize code that you need more than once.
- Copy-Paste leaves multiple copies of the same blocks.
- if something changes, you have to change all copies.
- For large software projects, break down a program hierarchically.
- A procedure is an abstraction. Procedures let you ignore details as you design your program.
DEMO: MakeQuiz app and displaying a list
- Sharing code blocks amongst apps (can't really do this with App Inventor)
Procedure Parameters: the information the procedure needs to do its job.
What parameters might you define for displaying a list?
Sketch the blocks for a procedure Move, which moves a ball a certain amount in the x and y direction. Hint:
you'll call MoveTo within it.
DEMO: Distance between two GPS coordinates