7AAVDH10 Chap.4 Loops and User-Defined Functions
Loops and User-Defined Functions
Trace repeated state
A for loop applies one body to successive items. A user-defined function packages a named transformation with explicit inputs and a returned result.
This chapter records accumulator state after every iteration, distinguishes printing from returning, and places return after a complete traversal rather than inside the loop.
Make reuse accountable
The final project criteria name both a loop and a user-defined function.
The chapter treats them as research evidence, not checklist ornaments: variable names, comments, small validation inputs and a clear connection to the research question must demonstrate what the code does and why.
What this chapter covers
- 01
For loops
- 02
Loop variables
- 03
Accumulators
- 04
Function parameters
- 05
Return values
- 06
Validation inputs
Worked example · free
Count long tokens
- 2Initialise the accumulator once. The marks shown are a study allocation, not an official University marking scheme.
- 3Update only for qualifying tokens.
- 3Return after the complete loop.
Key terms
- Accumulator
- An accumulator stores a running result that changes during iteration.
- Parameter
- A parameter names an input accepted by a function.
- Return Value
- A return value is the result sent from a function to its caller.
Loops and User-Defined Functions FAQ
Why does my function stop after one item?
A return statement is probably indented inside the loop. That placement ends the entire function on the first pass, so keep updates inside the loop and return the completed result afterwards.
Is printing the same as returning?
Printing displays a value for the reader, while returning provides a value to the calling code. A reusable function should return the result that later cells need, even if temporary prints support debugging.
How do I validate a loop?
Use a tiny input whose state changes can be written by hand, then test empty, no-match and all-match cases. Compare the observed accumulator after each item with the invariant stated for the method.
What makes a function reusable in research code?
A reusable function has a focused job, clear parameters, a documented output and behaviour that remains stable across contrasting inputs. Test it with a known result, an empty case and a boundary value. Avoid hidden mutation unless changing the input is part of the stated contract.
The function name, comments and validation should let another reader explain how its transformation supports the research question without reverse-engineering every implementation detail.
Exam move
Start every loop exercise with an invariant: after each iteration, the accumulator equals the number or total for qualifying items already seen. Choose a three-item input and write the current item, condition result and updated state in separate columns. Compare the trace with the invariant after every row.
Practise initialisation mistakes by placing the accumulator before and inside the loop, then explain why one version preserves prior work and the other erases it. Move return into the loop and predict the partial result before executing; move it back outside and confirm that all items are visited. Separate printing from returning by calling the function inside another expression.
A printed value may look correct while leaving the caller with None, so inspect the actual returned type. Test empty, no-match, one-match and all-match inputs. Add one threshold item that distinguishes greater than from greater than or equal. For mutation awareness, pass a list into a function, change an item and inspect the caller's list afterwards.
Decide whether the side effect belongs in the documented contract or whether the function should return a new value. Use meaningful parameters and an action name that accurately predicts the body. Read the function without its name and infer its purpose, then read the name without its body and compare. Maintain an error ledger for initialisation, update, indentation, return and side effect.
In exam revision, trace state changes and identify the first line that makes the output diverge. In project preparation, connect the repeated unit and condition to a methodological choice, validate with a known result and explain how the returned value supports the research question. Finish by running the same function on two distinct inputs to demonstrate genuine reuse instead of one successful demonstration.
Add a weekly refactoring task that begins with three copied code blocks. Identify the part that varies, move that value into a parameter and place the stable transformation inside one function. Predict all returned outputs before the first call. Then add a deliberately empty input and confirm that the contract still produces an intelligible result.
Compare a function that changes its input list with one that returns a new list, and state which behaviour the caller can safely rely on. End by writing a short markdown explanation that names the repeated unit, accumulator invariant, exit point and research meaning of the returned value.