Week 4-5 Functions and Modular Programming
Week 4-5 Functions and Modular Programming
26 SEPTEMBER 2024
PREPARED BY BÜLENT
HERDEM
Introduction
What are Functions?
Functions are reusable blocks of code designed to perform a specific task.
They help to break down complex problems into smaller, manageable parts.
Real-World Examples
Using predefined functions like print(), len(), or modules like math.Large applications such as web
servers or operating systems are built with modular programming.
Why Use Functions?
Code Organization: Functions help divide code into meaningful sections, making the program easier to understand
and manage.
Avoid Repetition: Functions prevent code duplication by allowing you to reuse the same block of code multiple
times with different inputs.
Easier Debugging: Debugging and testing are simplified because errors can be isolated and fixed in specific
functions without affecting the entire program.
Improves Collaboration: In larger projects, different team members can work on separate functions, making
collaboration smoother and more efficient.
Modularity: Breaking a program into functions promotes modular design, making it easier to maintain and update
specific parts of the codebase without affecting other parts.
Code Readability: Using well-named functions improves the readability of the code, making it more intuitive for
others (and yourself) to understand what the code is doing.
Abstraction: Functions allow you to hide complex logic and expose only what is necessary, making the codebase
more abstract and easier to work with at a higher level.
Scalability: With well-structured functions, it's easier to scale your program and add new features without rewriting
large parts of the code.
Function Basics
Defining Functions
Keywords and Syntax
def function_name(parameters):
return String, Number or Boolean etc. #You do not have to use return
Example -1 Example -2
# Global variable
x = 10 # global variable
message = "Hello from the global scope! "
def foo():
def greet():
x = 5 # local variable
global message # Referencing the global variable inside the
print(x) # Output: 5
function
message = "Hello from the local scope!"
print(x) # Output: 10 print("Inside the function:", message)
foo()
print("Before function call:", message)
greet()
print("After function call:", message)
MODULAR PROGRAMMING
Organizes code: Grouping all operations in a single module makes the code
cleaner and more organized.
Reusable: You can reuse the functions in the module across different projects.
Function-2
..
..
..
Function-n
MODULES, CLASSES, FUNCTIONS
Module – Think of a module as a bakery
Class – Within the bakery, you have different teams that specialize in different types of tasks.
Recursion is a programming technique where a function calls itself directly or indirectly to solve a
problem. The function typically works by breaking down a problem into smaller, more manageable
subproblems, solving each of those subproblems, and combining their results to arrive at a
solution for the original problem.
Base Case: This is a condition under which the recursive function stops calling itself. It
prevents infinite recursion and eventual program crash due to stack overflow. The base
case usually represents the simplest version of the problem.
Recursive Case: This is the part of the function that includes the recursive call. It reduces
the problem size with each call, moving closer to the base case.
Recursion Function Example
def fibonacci(n):
""" Calculate the nth Fibonacci number using recursion.
Parameters: n (int): A non-negative integer Returns: int:
The nth Fibonacci number """ Formula:
# Base cases: Value(0) = 0
if n == 0: Value(1) = 1
return 0
elif n == 1: Value(n) = Value(n-1) + Value(n-
return 1 2)
# Recursive case:
else: return fibonacci(n - 1) + fibonacci(n - 2) Fibonacci Series
0, 1, 1, 2, 3, 5, 8, 13, 21, 34 …
# Example usage
result = fibonacci(6)
print(f"The 6th Fibonacci number is {result}")
Homework time
HW-3: Factorial Calculator Using Recursion and Iteration :
Week 6-7:
Lists
Tuples
Dictionaries
Thank You!
Any Question?
[email protected]