Functions in Python
What is a Function in Python?
• A function in Python is a block of organized,
reusable code that performs a specific task.
• Functions help make code modular, efficient,
and easier to understand.
Function Definition with Example
• Example:
• def greet():
• print("Hello!")
• This is a simple function named 'greet' that
prints a message.
Reusability of Functions
• You can call the same function multiple times
to avoid repetition:
• greet()
• greet()
• This prints 'Hello!' two times without
repeating the print statement.
Modularity with Functions
• Functions help break code into small,
manageable parts:
• def add(a, b):
• return a + b
• def subtract(a, b):
• return a - b
Efficiency and Readability
• Functions make code easy to read and
maintain:
• def calculate_salary(hours, rate):
• return hours * rate
• print("Salary:", calculate_salary(40, 200))