VJC Chapter 5 Functions
Uploaded by cheesemuffin · 10 December 2025
Preview
VJC/H2Computing/9569 Chapter 5: Functions Contents 1. Introduction to Subroutines 1.1. Advantages of Subroutines 1.2. Basic Syntax 1.3. Examples of Functions 1.4. Docstrings 2. Defining Subroutines: Functions and Procedures 2.1. A procedure that does nothing 2.2. A procedure without both input parameter and return statement 2.3. A function with input parameters 2.4. A function with both input parameter and return statement 2.5. A function with default arguments 2.6. A function with a variable number of arguments 3. Variable Scope: Global vs Local 3.1. Local Variables 3.2. Global Variables 4. Referenced Readings Syllabus Learning Outcomes 2.2 Programming Elements and Constructs 2.2.4 Use functions and procedures to modularise problems into chunks of code. 1. Introduction to Subroutines Subroutines allow us to manage and structure our programs. This allows us to break up programs into smaller chunks of codes. This is called modular design. Such modular design allows you to debug easily, especially when you are dealing with a large program. There are two types of subroutines. ● Functions: Subroutines that allow us to have both input and output. Functions accept inputs and process them into output. They must return at least one value, aka “return value”. ● Procedures: Subroutines that do not return values. 1.1. Advantages of Subroutines The following are some of the advantages of using subroutines.
VJC/H2Computing/9569 ● Organization: Subroutines help break programs into smaller and modular chunks. They are almost like a mini-program that we can write separately from the main program, without having to think about the rest of the program while we write it. This allows us to reduce a complicated program into smaller, more manageable chunks, which reduces the overall complexity of our program. ● Abstraction: We can use a subroutine at any time, using its unique name and appropriate inputs. ● Reusability: It supports code reusability and reduces repetitive code. Subroutines can also be shared with other programs, reducing the need to code from scratch. ● Modifying: When we need to extend our program to handle a case it didn’t handle before, subroutines allow us to make the change in one place and have that change take effect every time the subroutine is called. ● Testing: The program is easier to test and debug since each subroutine is self-contained. Furthermore, once a subroutine is working properly, there is
Content continues in the PDF.
Related notes
- VJC Chapter 21 SQLite with PythonNotes/Practices · 2025
- VJC Chapter 23 Web Applications PrinciplesNotes/Practices · 2025
- VJC Chapter 10 RecursionNotes/Practices · 2025
- VJC Chapter 20 SQLNotes/Practices · 2025
- VJC Chapter 16 Hash TableNotes/Practices · 2025
- VJC Chapter 22 NoSQLNotes/Practices · 2025

