41099 Chap.7 Functions, control flow and non-blocking timing
Functions, control flow and non-blocking timing
What this chapter covers
- 01
Calling a function, and the parameters that make a helper reusable
- 02
Naming conventions, and why case sensitivity is not a cosmetic issue
- 03
Replacing a run of separate declarations with one array
- 04
Choosing between conditional chains, selection, and the two loop forms
- 05
Elapsed time scheduling, with one timestamp per task
- 06
Naming states, listing their actions and writing down every transition
Migrate a two rate blinking pattern off blocking waits
- 3Give each task its own stored timestamp and its own interval constant.
- 3Read the clock once per pass so both tests compare against one instant.
- 3Test elapsed time rather than an arrival deadline, and fold the button check into the same pass.
Key terms
- Parameter
- A value passed into a function when it is called.
- Return value
- The result a function hands back to whatever called it.
- Array
- A single named collection holding several values of one type, reached by index.
- Fall through
- Continued execution into the following case when a selection branch is not terminated.
- Blocking wait
- An instruction that stops the whole program for a fixed duration.
- State machine
- A program organised as named states, each with actions and conditions that advance it.
- Transition condition
- The test that moves a state machine from one state to the next.
- Timestamp
- A stored clock reading used to measure how long something has been true.
Functions, control flow and non-blocking timing FAQ
Why should a helper take parameters instead of reading the program's own variables?
A helper that reaches outside itself can only ever do one job, and moving it into another project drags those variables along. One that is given what it needs and hands back a result can be tested on its own, reused for a second sensor, and described in a sentence. Deciding what to pass in rather than what to reach for is the engineering content of writing helpers.
What makes a selection structure run several branches at once?
A missing terminating keyword at the end of a branch. Execution enters at the matching label and continues into every following branch until told to stop, so the symptom is that choosing one option performs that option and everything below it. A conditional chain cannot do this, which is why the fault appears only after a rewrite.
Is a state that only waits the same thing as a blocking wait?
No, and the difference is what makes the pattern useful. A waiting state records the moment it was entered and on every later pass asks whether enough time has elapsed to move on, while the rest of the program continues to run. A blocking wait hands the whole processor to one task and nothing else happens at all.
Why write the states down before writing any code?
Because a drawing of named states with conditions on the arrows makes the transitions you forgot immediately obvious, which reading code does not. It is also the artefact to show when a tutor asks how the device decides what to do next, and the course materials recommend planning the interaction and the timing before writing anything.
What is gained by replacing several separate declarations with one array?
Not brevity, but the ability to loop. Once seven pin numbers live in one collection, a loop can walk them, so setting a pattern becomes a few lines instead of a few dozen and adding another digit does not multiply the code. The course materials introduce arrays at exactly the point where the segment display would otherwise become unmanageable.
What should the default branch of a selection structure do?
Report. A default that silently does nothing wastes the most useful thing the structure offers. Printing the unexpected value to the serial monitor converts a silent failure during a demonstration into a fact you can talk about, which is a far stronger position than a device that simply stops.
Assessment move
Plan before typing: a system diagram for how the parts interact and a timeline for when things happen. Replace every blocking wait with a timestamp and an elapsed test as a matter of habit, because almost every later requirement assumes it. Keep a commented legend naming each state number.
Working through Functions, control flow and non-blocking timing in 41099? Sia is AskSia’s AI Engineering tutor — ask any 41099 Functions, control flow and non-blocking timing question and get a clear, step-by-step explanation grounded in how 41099 is taught and assessed. Read this chapter free, then take your hardest questions to Sia.