IDEA9103 Chap.10 Functions, Returns and Reusable Libraries
Functions, Returns and Reusable Libraries
Functions package repeatable work behind named contracts. Parameters describe expected inputs, calls supply arguments and return values carry results back to the caller. This chapter traces nested calls through a call tree, uses returned nodes to remove global canvas dependencies, and separates reusable utility definitions from the project specific sequence that composes one artwork.
What this chapter covers
- 01
Function declarations
- 02
Parameters and arguments
- 03
Return values
- 04
Nested function calls
- 05
Call tree tracing
- 06
Reusable utility files
Worked example · free
Trace two nested helpers
- 1offset receives 14.
- 1double receives 14 and returns 28.
- 1offset adds five to the returned value.
- 1offset returns 33.
Key terms
- Function declaration
- Code that defines a named block of behaviour without executing it immediately.
- Parameter
- A placeholder name for an input in a function definition.
- Argument
- A concrete value supplied to a parameter when a function is called.
- Return statement
- An instruction that ends a function path and sends a value back to its caller.
- Call tree
- A branching representation of which functions invoke other functions during execution.
Functions, Returns and Reusable Libraries FAQ
What is the difference between declaring and calling a function?
A declaration defines the reusable machine, including its parameters and body. A call executes that definition with arguments and may receive the value produced by a return statement.
Why return a generated shape instead of appending it inside the function?
Returning the node removes a dependency on one global canvas. The caller can inspect it, choose any compatible parent and control when it is appended, which also controls paint order.
What belongs in a utility file?
Keep definitions that can move unchanged between projects, such as bounded random integers or colour formatting. Project specific calls, composition order and references to a particular story or canvas belong in the project script.
How do I write a useful function contract?
Name the accepted inputs, the value returned and every external state change. For a shape creator, state which geometry and colour arguments it expects, whether it returns an SVG node and whether it appends anything itself. The contract should let a caller predict the result without reading the body.
When a function relies on a global canvas or an undocumented fallback, expose that dependency as an argument or describe it explicitly before calling the helper reusable.
What is the clearest way to trace nested calls?
Write the outer call, descend into the next called function and continue until reaching a function that can return without another unresolved call. Record that returned value, substitute it into the waiting expression and resume the caller. This depth-first method makes control flow and data flow visible together.
Label arguments on the downward path and returned values on the upward path so the first incorrect transformation can be located without guessing from the final output alone.
Why should a node-creation helper return before appending?
Returning the configured node separates construction from placement. The caller can inspect attributes, choose a compatible SVG parent and decide the append order that controls layering. The helper no longer depends on one global stage, so it can move to another sketch unchanged. This design also supports focused testing: verify the returned element first, then test the composition step separately.
A direct append may be appropriate for a deliberately narrow function, but that side effect belongs in its contract.
How can I check whether code is genuinely reusable?
Imagine moving the function into a new project without copying any of the old project's global variables, element identifiers or story-specific names. If its parameters provide everything it needs and its return value or documented side effects are sufficient, the boundary is reusable. Test it with at least two inputs and a different SVG parent.
Project choreography—the particular calls and their order—should remain in the project script even when the utility definitions live in a shared file.
How should I test a helper before using it in a larger composition?
Choose a small input whose result can be calculated by hand, call the helper in isolation and compare the return with that prediction. Repeat with an edge case such as zero, a missing optional argument or a boundary coordinate that the contract permits. For a node-returning helper, inspect the element type and attributes before appending it.
Only after these checks should the project script combine the helper with random generation or other calls. This order localises errors and preserves a trustworthy function boundary as the composition grows.
Assessment move
For every function, write a one sentence contract naming its inputs, output and side effects. Trace nested calls depth first and record each returned value. Refactor a direct DOM mutation into a returning helper, then test the returned node before appending it. Review script order so utility definitions load before the project code that calls them. Finish by narrating one complete output through the call tree.
Working through Functions, Returns and Reusable Libraries in IDEA9103? Sia is AskSia’s AI Computer Science tutor — ask any IDEA9103 Functions, Returns and Reusable Libraries question and get a clear, step-by-step explanation grounded in how IDEA9103 is taught and assessed. Read this chapter free, then take your hardest questions to Sia.