VJC Chapter 5 Functions
Uploaded by cheesemuffin · 10 December 2025
Preview
Text from the first pagesVJC/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 no need to test the subroutine again unless it has been modified 1.2. Basic Syntax ● Keyword def marks the start of a function. ● Every function has a name as uniquely identification. ● It may take in optional values, known as parameters, before running the code block. ● It may return vale, as a result, using a return statement. ○ If no return statement, the function will return None at the end of the function. ● Optional documentation string (docstring) describes what the function does. It may be multiple lines of string. ● All statements must be equally identified, which is usually 4 spaces. Note: Do not indent your program with a mix of spaces and tabs. 1.3. Examples of Functions We have used a few functions when we learned about Python basic data types. Can you recall the following functions?
VJC/H2Computing/9569 ● How do you check data type of an object? ● How do you type cast a value from one data type to another? ● How do you print out the value(s) or output of your programs? 1.4. Docstrings Recall what we get when we run help(len) or ?len. Docstring is actually the documentation that gets returned when we run the help function. 2. Defining Subroutines: Functions and Procedures Let's get familiar with how to define the following procedures and functions. ● A procedure that does nothing ● A procedure without both input parameter and return statement ● A function with input parameters ● A function with both input parameter and return statement ● A function with default arguments ● A function with a variable number of arguments 2.1. A procedure that does nothing Let's define the simplest function which does nothing. ● Keyword pass is used to indicate nothing to be done in the function body. ● Usually used when we want to define a function to do something, but we are not ready to code it yet.
VJC/H2Computing/9569 2.2. A procedure without both input parameter and return statement Let's define a basic hello_world function without input parameter and return statement. ● It simply prints "Hello World". ● This is an example of a procedure. 2.3. A function with input parameters Let's use an input parameter to make the hello function more flexible. ● It takes in an input value who, and prints "Hello {who}".
VJC/H2Computing/9569 2.4. A function with both input parameter and return statement Let's define a function my_max which returns the greater value of 2 input values. The return statement will exit the function. Consider the following function. Let's define a function my_min which returns the smaller value of 2 input values.
VJC/H2Computing/9569 Note the difference between invoking the function in Python Notebook (left image) and Python IDLE (right image). For the latter, you need to call the print function to display the output of the function. Let us create a function add_them() that will take two input parameters a and b and: ● return the sum of numbers if the input parameters are integers or floats, ● return a joint string if the input parameters are strings, ● return a joint list if the input parameters are lists, ● return an error message if otherwise
VJC/H2Computing/9569 Now, consider a function mul_them(a, b) that will take 2 input parameters a and integer b and: ● return the product if the input parameter a is an integer or float ● return the string a for b times if a is a string 2.5. A function with default arguments Python allows function arguments to have default values. If the function is called without the argument, the argument gets its default value. A parameter is a variable defined in the function definition, while an argument is an actual value passed to the function.
VJC/H2Computing/9569 2.6. A function with a variable number of arguments We can pass a variable number of arguments to a function using: ● *args (Non-keyword arguments) ● **kwargs (Keyword arguments) We use *args and **kwargs as an argument when we are unsure about the number of arguments to pass in the functions. This is not in the A-level syllabus. You may refer to https://www.programiz.com/python-programming/args-and-kwargs for a simple overview. 3. Variable Scope: Global vs Local The scope of a va
Content continues in the PDF. Download PDF
Related notes
- NYJC 2026 Prelim P2Exam Papers · 2026
- NYJC 2026 Prelim P1Exam Papers · 2026
- DHS 2026 Y6 H2 Computing Prelim Paper 2_finalExam Papers · 2026
- ACJC 2026 JC2 Computing Prelim Paper 2 (Practical)Exam Papers · 2026
- 2026_NJC Prelim_Computing_P2.pdfExam Papers · 2026
- 2026_JPJC_Computing_Prelim_P2_finalExam Papers · 2026
- 2026_JPJC_Computing_Prelim_P1_markschemeExam Papers · 2026
- 2026_JPJC_Computing_Prelim_P1_finalExam Papers · 2026
- 2026 ACJC Prelim Computing Paper 2Exam Papers · 2026
- 2024 ACJC Computing PromoExam Papers · 2024
- 2023 ACJC Promo QPExam Papers · 2023
- 2022 ACJC Computing Promo Paper 2Exam Papers · 2022
- See all H2 Computing notes

