COMP90059 Chap.5 Functions, Return Values and Scope
Functions, Return Values and Scope
A function packages one coherent operation behind a name and an input-output contract. Parameters are placeholders in the definition, while arguments are the values supplied by a call. A returned value makes the call an expression that can be assigned, combined or passed to another function; printing is a separate side effect.
Each call creates a local namespace, so the same parameter name can hold different values in different calls without overwriting another frame. Decomposition is most useful when helpers own distinct rules, such as calculation, classification and coordination. Mutable arguments require a precise contract because rebinding a local parameter differs from mutating the shared object it references.
Direct tests should cover ordinary, boundary and invalid inputs.
What this chapter covers
- 01
Def creates a function object without running its body
- 02
Parameters receive argument values for one call
- 03
Return ends a call and hands a value to the caller
- 04
Print displays output but normally returns None
- 05
Each call owns a separate local namespace
- 06
A helper should own one coherent rule
- 07
Default arguments express a genuine standard behaviour
- 08
Tests should call calculation helpers directly
- 09
Function contracts distinguish returned results from documented side effects
- 10
Argument expressions are evaluated before parameter binding occurs
- 11
Pure helpers are easier to combine and test than interactive procedures
- 12
Early returns can make invalid and boundary cases explicit
Worked example · free
Compose a mean and classification report
- 1Validate that count is not zero before calling the calculation helper.
- 1The mean helper returns 34 divided by 8, which is 4.25.
- 1Pass 4.25 to the band helper rather than recomputing the mean.
- 1The comparison 4.25 greater than 4 returns the label above target.
- 1Return the pair rather than printing inside either helper.
Key terms
- Parameter
- A placeholder name in a function definition that receives an argument value during a call.
- Argument
- An expression supplied at a call site and evaluated before being bound to a parameter.
- Return value
- The value sent from a function call back to the expression that requested it.
- Local scope
- The private namespace created for one function call and removed when that call finishes.
- Name shadowing
- A local binding that temporarily hides an outer binding with the same name inside the call.
- Default parameter
- A parameter value used when the caller omits the corresponding optional argument.
- Function contract
- A concise agreement describing accepted inputs, returned output, possible errors and any mutation or other side effect visible to the caller.
- Pure function
- A function whose result depends only on its arguments and which does not modify external state, making repeated tests predictable.
- Call frame
- The temporary execution context for one call, containing its parameter bindings, local names and the point to which control will return.
Functions, Return Values and Scope FAQ
What is the difference between print and return?
Print sends a representation to output. Return sends a value to the caller so the surrounding program can store, test or combine it in later expressions.
Why can two calls use the same parameter name?
Each call receives a separate local namespace. The parameter belongs to that frame and may hold a different value in another call without overwriting the earlier frame's binding.
When should code become a helper function?
Extract code when it represents one reusable rule with clear inputs and a result or documented side effect, not merely because several lines look long or happen to be adjacent.
How should I test a function?
Call it directly with an ordinary input, a boundary and an invalid or empty case. Compare returned values and inspect any promised mutation separately from printed or displayed output.
Why separate input and printing from calculation?
A calculation helper that accepts values and returns a result can be tested without simulated keyboard input or captured output. A thin coordinator can then own interaction and formatting, while the reusable rule remains deterministic and easier to diagnose.
What belongs in a function contract?
Record the accepted input types and meaningful constraints, the returned value, expected exceptional cases and every externally visible side effect. Then test the function at each contract boundary. This makes a caller's responsibilities explicit and prevents accidental reliance on local names, printed output or undocumented mutation.
Exam move
For every function, write a one-line contract naming inputs, return value and side effects. Trace calls by drawing a separate namespace box, binding arguments to parameters and moving only the return value back to the caller. Build a three-helper program with validation, calculation and classification separated. Test each helper alone before the coordinator.
Add a default argument and explain why the default is genuinely standard. Finally, pass a list into two functions: one that returns a new list and one that mutates in place. Keep a second alias to observe the difference. This sequence covers call evaluation, local scope, decomposition, defaults and mutation contracts.
Working through Functions, Return Values and Scope in COMP90059? Sia is AskSia’s AI Computer Science tutor — ask any COMP90059 Functions, Return Values and Scope question and get a clear, step-by-step explanation grounded in how COMP90059 is taught and assessed. Read this chapter free, then take your hardest questions to Sia.