Programming technique · B2.3.4
Methods and modularisation
Functions and modularisation
Methods divide a program into reusable blocks with clear responsibilities. They can receive data through parameters and return a result to the caller.
Functions divide a program into reusable blocks with clear responsibilities. They can receive data through parameters and return a result to the caller.
Why modularise?
Reuse
Write a calculation or validation rule once, then call it from every place that needs the same behaviour.
Maintainability
One clear responsibility is easier to locate and change than duplicated logic spread through the program.
Testing
A calculation with explicit inputs and a returned result can be tested independently with normal and boundary values.
Readable structure
Names such as calculateFare or isValidChoice reveal the program's decomposition.
Returning a result
The declared return type must match the value returned. A method that returns double must provide a decimal-compatible result on every possible path.
Python does not declare a return type in this example, but the caller still depends on the returned value having the expected meaning and usable type. Every relevant path should return a suitable result.
The parameter belongs to the called block while it runs. The returned value is then stored by the caller.
Parameters
Parameters are local variables that receive values from the method call.
Parameters are local names that receive values from the function call.
Void methods
A void method performs an action but does not return a value.
Functions used for actions
A function without an explicit return can perform an action such as displaying a menu. It returns None automatically.
Scope: local versus wider state
A local variable exists only inside the method or block where it is declared. A class-level field has wider availability. Pass required values into a method and return results whenever practical rather than turning temporary state into class-wide state.
A local variable normally exists only inside the function where it is assigned. A module-level name has wider availability. Pass required values into a function and return results whenever practical rather than turning temporary state into module-wide state.
Check your understanding
Answer each question before opening the model answer.
-
What is the difference between a parameter and a return value?
Reveal model answer
A parameter carries data into the called block. A return value carries one result back to the caller.
-
Why is a repeated fare calculation a good candidate for a function or method?
Reveal model answer
The same responsibility can be written once, tested independently and reused without duplicating the calculation.
-
Where is the local variable inches available in the scope example?
Reveal model answer
Only inside centimetresToInches / centimetres_to_inches while that call is executing.
-
Why can unnecessary global or class-wide state reduce maintainability?
Reveal model answer
More parts of the program can depend on or change the same value, making responsibilities and state changes harder to trace.
One clear responsibility
A name should describe one job, such as calculate_fare or is_valid_choice.
Call the method
Defining a method does not execute it. The program must call it from another method.
Call the function
Defining a function does not execute it. The program must call it from another part of the program.
Do not duplicate logic
Repeated calculations or validation rules are strong candidates for a reusable block.
Challenges Choose one
Choose a challenge that feels appropriate for you. Code heat is only a rough estimate, not a fixed level.
VAT Method
SelectedAsk the user for the price of an item. Write a method that receives the price and returns the VAT amount. Call the method from main and display the original price, VAT and final price.
Conversion Utility
SelectedCreate a menu-driven metric-to-imperial conversion utility. Each conversion must be implemented as a separate method with a clear parameter and return value. Include at least four conversions and keep input/output responsibilities separate from calculations.
Darts
SelectedCreate a darts game starting at 501. A player enters the total from each three-dart throw. A throw is valid when it reaches exactly zero or leaves a score of 2 or more. A throw that leaves 1 or takes the score below zero is a bust and does not change the score. Use methods for at least input validation, applying a throw and displaying the score. Test an ordinary throw, an exact checkout, a throw that leaves 1 and a throw below zero.
Pass the Pigs
SelectedCreate a simplified Pass the Pigs game. Each throw produces one named outcome and score: sider = 1, trotter = 5, double trotter = 20, and pig out = reset the current running score to 0. After a scoring throw, the player may throw again or bank. The goal is to reach 100 banked points. Use separate methods for generating an outcome, calculating its score and running a turn.
Optional extension: add more named outcomes and adjust their probability ranges so the game remains balanced. A two-player version is not required.
Selected challenge
This choice is shared with the portfolio setup page.
Plan your solution in handwritten pseudocode
Before opening your IDE or writing any program code, handwrite pseudocode for this challenge on paper.
Not marked complete. If you submit now, the GitHub README will record “No”.
Create your challenge folder
Run this command after planning. It creates the correct empty folder inside your portfolio.
Complete the challenge
Use your handwritten pseudocode as the starting plan, then write and test your solution in the folder created above.
Optional two-level scaffold
Try from your handwritten pseudocode first. Scaffold gives some structure; Scaffold + comments gives stronger guidance. Use only the level you need, and update your pseudocode first if the support changes your plan.
Submit for review
Run this when your program is complete. It creates the README, commits the folder and pushes it. The README records whether you marked the handwritten pseudocode as complete; the paper itself is handed to your teacher separately.