Day 5: Functions and Modular Programming
Task
Create and use functions in Python.
Learning Resources
I don’t know how functions work in Python, so let’s learn together!
Description
Functions are an essential part of Python, allowing you to break down your code into reusable blocks. Using functions helps to keep your code clean, organized, and easier to manage. Functions can take inputs, process them, and return outputs. By modularizing your code, you can avoid repetition and improve maintainability.
Key Concepts
- Defining Functions: Use the
def
keyword to define a function - Calling Functions: Execute a function by calling its name
Example
1
2
3
4
5
6
# Define a function to greet a user
def greet(name):
return f"Hello, {name}!"
# Calling the function
print(greet("Alice"))
Output:
1
Hello, Alice!