FoPM3 Functions and Modules
FoPM3 Functions and Modules
Functions and modules are two of the most essential concepts in Python and any programming
language. They allow for more organized, modular, and reusable code. Below, I'll break down each
subtopic and provide explanations, followed by examples to clarify their use in practice.
1. Defining Functions
A function in Python is a block of reusable code that performs a specific task. Functions help break
down programs into smaller, more manageable parts, making the code more readable and reducing
redundancy.
To define a function, you use the def keyword followed by the function name and parentheses ():
def function_name(parameters):
# Function body
return result
• function_name: The name of the function that will be used to call it.
• return: The value that the function will send back to the caller (optional).
Example:
def greet(name):
• Parameters are inputs to the function. When a function is called, you pass values
(arguments) to these parameters. Python allows for various types of parameters, including:
• Return Values: A function can send a value back to the caller using the return keyword. If
no return is used, the function implicitly returns None.
Example:
return a * b
print(result) # Output: 10
print(result) # Output: 15
• Local Variables: These are variables defined inside a function. They are accessible only
within that function and don't affect or interact with variables outside of the function.
• Global Variables: Variables defined outside of any function and can be accessed
throughout the code, including within functions. However, if you want to modify a global
variable inside a function, you must explicitly declare it using the global keyword.
Example:
x = 10 # Global variable
def my_function():
x = 5 # Local variable
print("Inside function:", x)
def modify_global():
global x
x = 20
modify_global()
Python provides numerous built-in functions like print(), len(), sum(), etc., which are available by
default.
To extend Python's functionality, you can import modules. Modules are collections of related
functions and variables grouped into a single file or package. Python provides many standard
modules like math, random, datetime, and more. To use them, you must first import them using the
import keyword.
import math
result = math.sqrt(16)
python
Copy code
A custom module is simply a Python file containing functions, variables, and classes that you can
import into other programs. To create a module, you write a Python file and save it with a .py
extension. You can then import that file into another script to use its functionality.
Example:
# mymath.py
return a + b
return a - b
import mymath
result = mymath.add(10, 5)
print(result) # Output: 15
You can also use from to import specific functions from a custom module:
python
Copy code
6. Introduction to Recursion
Recursion occurs when a function calls itself in its definition. It’s often used to solve problems that
can be divided into similar sub-problems, such as calculating the factorial of a number or finding
the greatest common divisor (GCD).
When writing recursive functions, it’s essential to define a base case to prevent infinite recursion.
def factorial(n):
if n == 1: # Base case
return 1
else:
Exercises:
The greatest common divisor (GCD) of two numbers is the largest positive integer that divides
both numbers without leaving a remainder. You can use recursion to calculate the GCD using
Euclid's algorithm.
Example:
if b == 0:
return a
else:
return gcd(b, a % b)
import math
import random
sqrt_value = math.sqrt(random_number)
Summary:
• Function Parameters can be passed to functions, and return values allow them to send
information back to the caller.
• Scope distinguishes between variables defined locally inside functions and globally in the
program.
• Python provides many built-in functions and modules that extend its functionality.
• You can create your own custom modules to organize code across multiple files.
• Recursion allows for elegant solutions to certain problems but requires a base case to
prevent infinite loops.