Python scope, functions and procedures
Distinguish local and global names, use constants, call procedures and functions, and select the specified built-in functions.
Section 10 · Further programming and testing
Mapped to the Cambridge IGCSE Computer Science 0265 syllabus for exams in 2029. These original explanations and self-checks are independent study support, not Cambridge papers, official marks or grades. Practical programming is taught with worked code and trace tasks; an executable Python workspace and timed digital-exam simulation are not yet available.
Before you begin
- Use the 2029 syllabus for this course. Cambridge specifies Python 3.10 or higher for programming and digital exams for both components. The syllabus currently identifies this route for 2029 only; confirm later-year availability with Cambridge and your school.
Work on paper. Try each question before revealing help, and explain why your method works.
Quick readiness check
Before python scope, functions and procedures, which statement is the safest starting point?
What you will learn
- Compare local and global variables and constants
- Identify and use procedures and functions with parameters
- Apply the syllabus functions to a context
Scope limits where a name is used
A local variable or constant is available within its function or procedure. A global variable or constant is available across a wider program scope. Local names reduce unintended interactions, while globals can make shared state easier to access but harder to reason about and test.
Constants should not change during the program. Local or global constants have similar visibility trade-offs to variables, but their fixed purpose makes unintended updates less likely. Prefer a narrow scope unless a value genuinely needs to be shared.
Functions return a value; procedures perform an action
A function can calculate and return a result; a procedure performs a task and may not return a value. Parameters pass data into a subroutine. Calls can include parameters or use defaults/no parameters when defined that way.
The syllabus specifies sum(), .count(), max(), min(), .mean(), round(), random.randrange() and random.randint(). Use the correct input type and know whether an endpoint is included: randint(a,b) includes both endpoints, whereas randrange(start, stop) excludes stop. Some functions such as .mean() depend on the taught library context.
Pause and explain
Which habit best checks your understanding of python scope, functions and procedures?
Worked example
Write a function that receives two numbers and returns the larger.
Show the worked solution
- Define a function with two parameters.
- Compare the values using if/else or max().
- Return the selected value to the caller.
Answer def larger(a, b): return max(a, b)
Try a topic test
Choose Easy, Normal or Difficult. Save your answers and open worked review after finishing.
Test this topic →From guided practice to a new situation
Write your answer and reasoning first. Use a hint only if you are stuck. The model solution is for self-checking; your written work is not automatically marked.
Write a function mean_score(scores) that returns the mean of a non-empty list using sum() and len().
Give me a hint
Add all values, divide by the item count and return the result.
Compare my reasoning
- Define a function with scores as a parameter.
- Calculate sum(scores) / len(scores).
- Return the calculated mean.
def mean_score(scores): return sum(scores) / len(scores)
Look for these in your work
- Parameter used
- Mean formula
- Result returned
A function changes a global score directly, and another function also updates it. Explain one risk and one reason to prefer a local variable with a returned result.
Give me a hint
Think about hidden state and the effect of calling functions in different orders.
Compare my reasoning
- Changing a global from several functions can make the value depend on call order.
- It is harder to test a function independently when it silently changes shared state.
- A local calculation returned explicitly makes data flow clearer and reduces unintended side effects.
Shared global state can create hidden interactions; local values with explicit return are easier to reason about.
Look for these in your work
- Risk described
- Testing impact
- Alternative explained
Generate an integer die roll from 1 to 6 using either random.randrange() or random.randint(). Explain endpoint handling.
Give me a hint
Choose a call whose range includes both 1 and 6.
Compare my reasoning
- random.randint(1, 6) includes both endpoints.
- Alternatively, random.randrange(1, 7) includes 1 through 6 because the stop value is excluded.
- The result represents one of six equally possible integer values under the usual random model.
random.randint(1, 6) or random.randrange(1, 7).
Look for these in your work
- Valid range
- Endpoint rule
- Result interpretation
Common mistakes
- Using a local variable outside its function.
- Confusing a return value with printed output.
- Assuming randint excludes its final endpoint.
What can you explain now?
Which returns a value from a function to its caller: print or return?
Compare with the explanation
return
print displays output; return supplies the result of a function call.
After trying it yourself, choose your next review. This is your self-assessment.
Your review choice appears on Today. Sign in to sync it across devices.
Make it stick
How do scope, parameters and return values help structure a program, and which built-ins fit common tasks?
If you needed the solution, close it and solve the problem again from a blank page. A correct answer is stronger when you can explain the reason for each step.