This topic covers the use of subprograms, including both procedures and functions, to structure code effectively. Students must understand how to implement
Topic Synopsis
This topic covers the use of subprograms, including both procedures and functions, to structure code effectively. Students must understand how to implement these, manage parameters, handle return values, and distinguish between local and global variable scopes.
Key Concepts & Core Principles
- Definition and call: A subprogram is defined using a name, parameters (optional), and a body. It is executed when called by name, possibly with arguments.
- Parameters and arguments: Parameters are variables in the subprogram definition; arguments are the actual values passed when calling. Understand the difference between formal and actual parameters.
- Return values: Functions return a value using the 'return' statement; procedures do not return a value (they perform an action). In Python, a function returns None if no return is specified.
- Local vs. global variables: Variables declared inside a subprogram are local (only accessible within it). Global variables are declared outside and can be accessed anywhere, but modifying them inside a subprogram requires the 'global' keyword (in Python).
- Parameter passing: By default, Python passes parameters by reference for mutable objects (e.g., lists) and by value for immutable objects (e.g., integers). However, the exam focuses on the effect: changes to mutable objects inside a subprogram affect the original.
Exam Tips & Revision Strategies
- Always check if a subprogram needs to return a value to determine if it should be a function or a procedure
- Use meaningful identifiers for subprogram names to improve code readability
- Ensure parameters are clearly defined and used consistently within the subprogram logic
- Practice tracing code that uses local variables to ensure you understand scope limitations
Common Misconceptions & Mistakes to Avoid
- Confusing the difference between a function (returns a value) and a procedure (does not return a value)
- Incorrectly accessing global variables inside a subprogram instead of passing them as parameters
- Failing to return a value in a function
- Over-reliance on global variables leading to poor code structure
Examiner Marking Points
- Correct use of built-in and user-devised subprograms
- Correct implementation of functions that return values
- Correct implementation of procedures that do not return values
- Appropriate use of parameters within subprograms
- Correct application of local versus global variable scope
- Single entry and exit points from code blocks and subprograms