University of Sydney · FACULTY OF COMPUTER SCIENCE

IDEA9103 Chap.10 Functions, Returns and Reusable Libraries

- one subject, every graph, every model, every mark
6 Chapters3-page Bible
Our own words - no uploaded lecturer files
Updated for this semester
Chapter 10 of 10 · IDEA9103

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.

In this chapter

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

Q [4 marks]. The practice mark allocation used here is not part of the University's published assessment scheme. double(n) returns n times two. offset(n) returns double(n) plus five. Evaluate offset(14).
The call graph descends from the main sketch into rectangle creation, helper functions and the final append operation.The call graph descends from the main sketch into rectangle creation, helper functions and the final append operation.main sketchcreateRect()appendChild()randomInt()makeRGB()
The call graph descends from the main sketch into rectangle creation, helper functions and the final append operation.
  • 1offset receives 14.
  • 1double receives 14 and returns 28.
  • 1offset adds five to the returned value.
  • 1offset returns 33.
The final result is 33 because the inner return becomes an input to the unfinished outer expression.
Sia tip — Trace into the deepest active call and write its return value before resuming the caller.
Glossary

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.
FAQ

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.

Study strategy

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.

A+Everything unlocked
Unlocks this Bible + all 202 of your University of Sydney subjects - and 1,000+ Bibles across every Australian university.
Sia - your IDEA9103 tutor, unlimited, worked the way the exam marks it
The full 3-page Bible + practice bank with worked solutions
Chrome extension - sync your LMS so Sia knows your deadlines
Bilingual EN / Chinese on every Bible and every Sia answer
$0.99 Trial
30-day money-back · cancel in one tap · how it works