👋 Hey Python Learners!
Welcome to **Day 6** of our Python journey! 🚀 Today, we're diving into one of the most fundamental and powerful concepts in programming: **Functions**.
Functions are reusable blocks of code that perform a specific task. They help organize your code, make it more readable, and save you from repeating yourself (DRY - Don't Repeat Yourself principle!). Think of them as mini-programs within your main program. Let's see how to define and use them!
```python
# Define a simple function to greet someone
def greet_user(name):
"""This function takes a name and prints a greeting."""
print(f"Hello, {name}! Welcome to Day 6. 👋")
# Define a function that adds two numbers and returns the result
def add_numbers(num1, num2):
"""This function adds two numbers and returns their sum."""
return num1 + num2
# Call the greet_user function
greet_user("Alice")
greet_user("Bob")
# Call the add_numbers function and store the result
result = add_numbers(10, 5)
print(f"The sum of 10 and 5 is: {result}")
# You can also call it directly in a print statement
print(f"Adding 7 and 3 gives: {add_numbers(7, 3)}")
```
Output example:
```
Hello, Alice! Welcome to Day 6. 👋
Hello, Bob! Welcome to Day 6. 👋
The sum of 10 and 5 is: 15
Adding 7 and 3 gives: 10
```
---
### 🔥 Small Challenge:
Write a Python function called `calculate_area` that takes two arguments: `length` and `width`. The function should return the product of these two numbers (the area of a rectangle). Then, call your function with example values and print the result!
---
💡 Keep practicing, and you'll master functions in no time! See you tomorrow for Day 7! #PythonLearning #Functions #PythonBasics #CodingChallenge
Welcome to **Day 6** of our Python journey! 🚀 Today, we're diving into one of the most fundamental and powerful concepts in programming: **Functions**.
Functions are reusable blocks of code that perform a specific task. They help organize your code, make it more readable, and save you from repeating yourself (DRY - Don't Repeat Yourself principle!). Think of them as mini-programs within your main program. Let's see how to define and use them!
```python
# Define a simple function to greet someone
def greet_user(name):
"""This function takes a name and prints a greeting."""
print(f"Hello, {name}! Welcome to Day 6. 👋")
# Define a function that adds two numbers and returns the result
def add_numbers(num1, num2):
"""This function adds two numbers and returns their sum."""
return num1 + num2
# Call the greet_user function
greet_user("Alice")
greet_user("Bob")
# Call the add_numbers function and store the result
result = add_numbers(10, 5)
print(f"The sum of 10 and 5 is: {result}")
# You can also call it directly in a print statement
print(f"Adding 7 and 3 gives: {add_numbers(7, 3)}")
```
Output example:
```
Hello, Alice! Welcome to Day 6. 👋
Hello, Bob! Welcome to Day 6. 👋
The sum of 10 and 5 is: 15
Adding 7 and 3 gives: 10
```
---
### 🔥 Small Challenge:
Write a Python function called `calculate_area` that takes two arguments: `length` and `width`. The function should return the product of these two numbers (the area of a rectangle). Then, call your function with example values and print the result!
---
💡 Keep practicing, and you'll master functions in no time! See you tomorrow for Day 7! #PythonLearning #Functions #PythonBasics #CodingChallenge