Advanced programming: modularisation and data structures (Grade 11 focus) – Week 3 focus
Download the Lessonotes Mobile South Africa app for faster lesson access on Android and iPhone.
Subject: Information Technology
Class: Grade 11
Term: 3rd Term
Week: 3
Theme: General lesson support
This page supports the lesson note with a companion video and a short classroom-ready summary.
For class groups and homework, share this lesson page so learners also get the summary, objectives, and full lesson context.
This week, we delve into the powerful concepts of modularisation and data structures in programming. Modularisation allows us to break down complex programs into smaller, manageable, and reusable parts, similar to how different departments in a company (e.g., marketing, finance, operations) handle specific tasks, contributing to the overall success of the organisation. Data structures, on the other hand, are ways of organising and storing data so that it can be used efficiently. Think of a filing cabinet – a well-organized one (using a good data structure) allows you to quickly find the document you need, whereas a disorganized one makes it much harder.
2.1 Modularisation: Modularisation is the process of dividing a large, complex software program into smaller, self-contained, and manageable subprograms called modules, functions, procedures, or subroutines. Each module performs a specific task and can be developed, tested, and debugged independently.
Benefits of Modularisation: Code Reusability: Modules can be reused in different parts of the program or even in other programs, saving time and effort. Imagine writing a function to calculate the VAT (Value Added Tax) on a product. This function can be reused every time a VAT calculation is required, instead of writing the same code repeatedly.
Improved Readability: Smaller modules are easier to understand than large, monolithic blocks of code.
Enhanced Maintainability: Changes or bug fixes can be made to individual modules without affecting the entire program. Think about a website – if one module dealing with user authentication needs updating, the rest of the website can continue running normally while the update is being applied.
Simplified Debugging: Errors are easier to isolate and fix in smaller modules.
Increased Collaboration: Multiple programmers can work on different modules simultaneously, speeding up the development process.
Abstraction: Modules hide the implementation details from the rest of the program, allowing developers to focus on the functionality rather than the underlying code. 2.2 Functions (or Procedures): A function (or procedure, depending on the programming language) is a named block of code that performs a specific task. Functions can accept input values (arguments) and can return a result.
Syntax (Python Example): ```python def function_name(parameter1, parameter2): Code to perform the task result = parameter1 + parameter2 return result Calling the function sum_of_numbers = function_name(5, 3) print(sum_of_numbers) # Output: 8 ``` Explanation: `def`: Keyword used to define a function. `function_name`: The name of the function (choose descriptive names). `(parameter1, parameter2)`: Input values that the function accepts (optional). These are also called arguments when you call the function. `# Code to perform the task`: The actual code that performs the function's task. `return result`: Returns a value from the function (optional). If no `return` statement is present, the function returns `None` by default. 2.3 Local and Global Variables: Local Variables: Variables declared inside a function are called local variables. They are only accessible within that function.
Global Variables: Variables declared outside of any function are called global variables. They are accessible from anywhere in the program, including inside functions.
Example (Python): ```python global_variable = 10 # Global variable def my_function(): local_variable = 5 # Local variable print("Inside function:") print("Global variable:", global_variable) print("Local variable:", local_variable) my_function() print("Outside function:") print("Global variable:", global_variable) print("Local variable:", local_variable) # This will cause an error ``` Explanation: `global_variable` can be accessed both inside and outside the `my_function` function. `local_variable` can only be accessed inside the `my_function` function. Trying to access it outside will result in an error. Important
Note: While global variables are accessible everywhere, it's generally recommended to use them sparingly. Overuse of global variables can make code harder to understand and maintain. Passing data to functions as arguments and returning values is often a better approach. 2.4 Data Structures: Arrays/Lists: An array (or list in Python) is a data structure that stores a collection of elements of the same data type in contiguous memory locations. Each element in an array is identified by its index, which is a numerical value representing its position in the array. Imagine a row of houses, each with a number (the index).
Syntax (Python): ```python my_list = [10, 20, 30, 40, 50] # Creating a list print(my_list[0]) # Accessing the first element (index 0) - Output: 10 print(my_list[2]) # Accessing the third element (index 2) - Output: 30 my_list[1] = 25 # Modifying the second element (index 1) print(my_list) # Output: [10, 25, 30, 40, 50] ``` Explanation: `my_list = [10, 20, 30, 40, 50]`: Creates a list named `my_list` containing five integer elements. `my_list[0]`: Accesses the element at index 0 (the first element). Indices start at 0. `my_list[1] = 25`: Modifies the element at index 1 (the second element) to 25.