Week 3 ยท Functions, Outputs & Buttons

Session 11: Functions

Teach & build120 minLesson 08: Functions: Naming & Reusing Tasks

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.

  1. Lesson 08: Why functions matter
  2. Lesson 08: Parts of a function
  3. Lesson 08: Returning a value
  4. Theory Reference: Functions

Session plan

  1. 0:0010 min
    Recap

    Quiz: loops and arrays

  2. 0:1025 min
    Demo

    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.

  3. 0:3530 min
    Build

    Worked sketch plus one new function

    Students upload the worked sketch, then write flashAll(byte times) and call it from loop().

  4. 1:0545 min
  5. 1:5010 min
    Wrap-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.