Week 3 ยท Functions, Outputs & Buttons
Session 12: Functions that share values
Learning outcomes
- Explain why parameters are copies and local variables are temporary
- Use pass-by-reference when a function must change the caller's variable
- Split a long loop() into named steps without changing behaviour
Kit and components
- Four-LED circuit, Uno and lab PC
Pre-reading about 15 min
Students read these on their phones before class. They are listed on the student Before class page.
Session plan
- 0:0010 minRecap
Quiz: functions
- 0:1020 minDemo
Copies, locals and references
Show a counter that resets because it is local, then a swap that fails with copies and works with references.
- 0:3040 minCode Along
Catch up on earlier Code Along
Finish any core Functions exercises not yet done, then any earlier chapters still open.
- 1:1040 minBuild
Refactor the pattern controller
Students restructure their S10 mini-project into readCommand(), updatePattern() and showPattern(), testing after each step so behaviour never changes.
- 1:5010 minWrap-up
Does loop() read like a plan?
Checkpoint: ready to move on?
Each student's refactored sketch behaves exactly as before, and loop() contains only function calls.
Extension for fast finishers
Write showLevel() from the lesson challenge, or try the Deeper exercises.
- Code Along 5.5: Write your own map()Deeper
- Code Along 5.6: Swap two variablesDeeperLaptop
Catch-up path
Skip the refactor and finish the core Functions exercises.
Homework and practice
Pre-reading for S13.
Facilitator notes
- Flex session: in a shorter course skip it. Write your own map() and Swap two variables are Deeper exercises, so nothing later depends on them.