-COURSE CODE: IT121 – Computer Programming 2
Module 3: Functions
Week 3: February 9-20, 2026 | 1st Semester, S.Y. 2025-2026
Introduction
Functions play an important role in writing clear, organized, and efficient
computer programs. In programming, large problems are easier to solve
when they are divided into smaller and manageable tasks. Functions allow
programmers to do this by grouping related instructions into reusable parts.
In real-life applications such as grading systems, payroll programs, and
COURSE MODULE
record management systems, the same processes are performed
repeatedly. Instead of writing the same instructions many times, functions
make it possible to create one set of instructions and use it whenever
needed. This makes programs easier to maintain and less prone to errors.
In this module, learners will study how functions are defined, how data is
passed to them, and how results are returned. They will also explore
variable scope, default arguments, multiple return values, and recursion.
These concepts help develop logical thinking and improve program
structure.
Through practical examples and activities, this module aims to strengthen
students’ ability to design organized and efficient programs and prepare
them for more advanced topics in system development.
Intended Learning Outcomes
• Apply functions in solving programming problems.
• Use built-in and user-defined functions effectively.
• Develop modular programs using proper program logic.
Topic 1: Defining and Calling Functions
3.1 Concept of Functions
A function is a block of code that performs a specific task. It helps organize
programs and reduces code repetition.
Functions make programs easier to understand, test, and maintain.
Example:
def greet():
print("Hello, Student!")
greet()
Explanation:
• def is used to define a function
• greet is the function name
• The code runs when the function is called
3.2 Calling Functions
Calling a function means executing the instructions inside it.
Example:
def show():
print("Welcome to Programming")
show()
show()
Explanation:
• The function runs every time it is called
• The same code is reused
Topic 2: Built-in and User-Defined Functions
3.3 Built-in Functions
COURSE MODULE
Built-in functions are ready-made functions provided by Python.
These functions perform common tasks such as displaying output,
counting values, and performing calculations.
Using built-in functions helps programmers save time and write
programs more efficiently.
Example:
print("Hello")
len("Python")
sum([1, 2, 3])
type(10)
Explanation:
• print() displays output on the screen
• len() counts the number of characters
• sum() adds all values in a list
• type() identifies the data type
These functions are available without creating them.
3.4 User-Defined Functions
User-defined functions are functions created by programmers to
perform specific tasks. They are useful when a task needs to be done
repeatedly in a program.
By using user-defined functions, programs become more organized
and easier to understand.
Example:
def square(num):
return num * num
print(square(5))
Explanation:
• def is used to create the function
• square is the function name
• num receives the value 5
• The function returns the square of the number
Topic 3: Passing Arguments to Functions
3.5 Arguments and Parameters
When a function needs data to work with, values are sent to it.
These values are called arguments.
The variables that receive these values inside the function are called
parameters.
In simple terms:
• Argument → value sent to the function
• Parameter → variable that receives the value
.
Example:
COURSE MODULE
def greet(name): # name is a parameter
print("Hello,", name)
greet("Juan") # "Juan" is an argument
Explanation:
• name is the parameter inside the function
• "Juan" is the argument passed to the function
• The value "Juan" is stored in name
• The function uses the value to display the message
Understanding arguments and parameters is important before
learning how values are passed to functions.
3.6 Passing Arguments (By Value and By Reference Concept)
After sending arguments to a function, Python decides how the
data is handled inside the function.
In Python, arguments are passed using object references. This means
that some values change inside the function, while others do not.
• Immutable values (int, float, string) cannot be changed
• Mutable values (list, dictionary) can be modified
Example 1: Immutable Data (Pass-by-Value Behavior)
def change(x):
x = 10
num = 5
change(num)
print(num)
Explanation:
• num is passed as an argument
• x receives the value
• x is changed inside the function
• num remains unchanged outside
This behaves like pass-by-value.
Example 2: Mutable Data (Pass-by-Reference Behavior)
Example:.
def change_list(lst):
[Link](100)
values = [1, 2, 3]
change_list(values)
print(values)
Explanation:
COURSE MODULE
• values is passed as an argument
• lst refers to the same list
• The function modifies the list
• The change affects the original data
This behaves like pass-by-reference.
Topic 4: Return Values
3.7 Returning Values from Functions
After a function processes data, it may send a result back to the
main program.
This is done using the return statement.
Returned values can be stored in variables and used for further
operations.
Example (3D Array):
def add(a, b):
return a + b
result = add(4, 6)
print(result)
Explanation:
• The function receives two arguments
• It adds the values
• The return statement sends the result back
• result stores the returned value
• The value is displayed
Using return makes functions more useful and flexible.
3.8 Difference Between print() and return
It is important to understand the difference between print() and
return.
print() return
Displays output Sends value back
Cannot be reused Can be reused
For viewing only For processing
Example:
def show_sum(a, b):
print(a + b)
def get_sum(a, b):
return a + b
show_sum(2, 3)
result = get_sum(2, 3)
COURSE MODULE
print(result)
Explanation:
• show_sum() only displays the result
• get_sum() returns the result for later use
Topic 5: Scope of Variables
3.9 Scope of Variables
Scope refers to where a variable can be accessed in a program.
Understanding scope helps prevent errors and confusion.
There are two main types:
• Global variables – accessible anywhere
• Local variables – accessible only inside a function
Example:
x = 10 # Global variable
def test():
y = 5 # Local variable
print(y)
test()
print(x)
Explanation:
• x can be used inside and outside the function
• y exists only inside test()
• Using y outside will cause an error
3.10 Using Global Variables Inside Functions
Global variables can be used inside functions using the global
keyword.
Example:
x = 10
def change():
global x
x = 20
change()
print(x)
Explanation:
• global x allows modification of x
• The value of x changes outside the function
Topic 6: Default Arguments
3.11 Default Arguments
Default arguments are parameters that already have assigned values in a
COURSE MODULE
function. These values are used automatically when no argument is
provided during the function call.
Default arguments make programs more flexible and user-friendly because
they prevent errors when users forget to enter values. They also allow
programmers to set common or standard values for a function.
This feature is useful in systems where some information is optional, such as
registration forms, report generation, and system settings.
Example:
def greet(name="Student"):
print("Hello,", name)
greet()
greet("Ana")
Explanation:
• The parameter name has a default value of "Student"
• When greet() is called without an argument, the default value is
used
• When "Ana" is passed, it replaces the default value
• The function adjusts its output automatically
This shows how default arguments make functions more flexible.
Importance of Default Arguments
Default arguments help programmers to:
✔ Avoid missing input errors
✔ Reduce required user input
✔ Make programs easier to use
✔ Improve system reliability
Topic 7: Returning Multiple Values
3.12 Returning Multiple Values
In some programs, a function needs to produce more than one result.
Instead of creating many functions, Python allows a single function to
return multiple values at the same time.
These values are grouped together and can be stored in separate
variables when received.
Returning multiple values is commonly used in financial systems, grading
systems, and statistical analysis programs.
Example:
def compute(a, b):
return a+b, a-b, a*b
sum, diff, prod = compute(6, 3)
print(sum, diff, prod)
Explanation:
• The function calculates three operations
• The values are returned together
• They are assigned to sum, diff, and prod
COURSE MODULE
• Each result can be used separately
This makes programs more efficient and organized.
Topic 8: Recursion
3.13 Recursive Functions
A recursive function is a function that calls itself in order to solve a problem.
Recursion is useful when a problem can be divided into smaller versions of
the same problem.
Instead of using many loops, recursion allows programmers to solve
complex problems using simple repeated steps.
Common applications of recursion include factorial computation, file
searching, and mathematical modeling.
Parts of a Recursive Function
Every recursive function must have two parts:
1. Base Case – the condition that stops the recursion
2. Recursive Case – the part where the function calls itself
Without a base case, the function will repeat forever.
Example:
def factorial(n):
if n == 1:
return 1
return n * factorial(n-1)
print(factorial(5))
Explanation:
• When n is equal to 1, the function stops
• This is called the base case
• If n is greater than 1, the function calls itself
• Each call reduces the value of n
• The final result is returned step by step
This process continues until the base case is reached.
3.14 Common Errors in Recursion
Many beginners experience problems when using recursion.
Common Mistakes
❌ No base case
❌ Wrong stopping condition
❌ Infinite recursion
❌ Memory overflow
Example of Error:
def test(n):
return test(n)
Explanation:
• The function has no stopping condition
• It keeps calling itself
• The program will crash
COURSE MODULE
This shows why a base case is very important.
When to Use Recursion
Recursion should be used when:
✔ The problem has repeated patterns
✔ The solution can be divided into smaller parts
✔ The logic is easier than using loops
Otherwise, loops may be more efficient.
References
Liang, Y. (2013). Introduction to programming using Python. Pearson
Education.
Python Software Foundation. (n.d.). Python documentation.
[Link]
Python Software Foundation. (n.d.). The Python tutorial.
[Link]
Sweigart, A. (2019). Automate the boring stuff with Python (2nd ed.). No
Starch Press.
TutorialsPoint. (n.d.). Python tutorial.
[Link]
Van Rossum, G. (2009). The history of Python. Python Software Foundation.
[Link]