Week 3 ยท Functions, Outputs & Buttons
Session 11: Functions
Learning outcomes
- Identify the return type, name, parameters and body of a function
- Write void helper functions that act on outputs
- Write a function that returns a value and use the result in an expression
- Refactor repeated code into a named function
Kit and components
- Four-LED circuit from S9, Uno and lab PC
Pre-reading about 20 min
Students read these on their phones before class. They are listed on the student Before class page.
Session plan
- 0:0010 minRecap
Quiz: loops and arrays
- 0:1025 minDemo
Name the task
Refactor the S9 all-on and all-off loops into setAll(bool state), then add chase(bool forward, int stepMs). Show loop() now reads like a plan.
- 0:3530 minBuild
Worked sketch plus one new function
Students upload the worked sketch, then write flashAll(byte times) and call it from loop().
- 1:0545 min
- 1:5010 minWrap-up
void or int?
Checkpoint: ready to move on?
Students can write and call a function with a parameter and a return value, and explain the difference between void and int return types.
Extension for fast finishers
Catch-up path
Label the four parts of each function in the worked sketch before writing a new one.
Homework and practice
Code Along 5.3 and 5.4.
Facilitator notes
- Write a function from scratch bridges electronics and code - ask students to check its answers with Ohm's law.