📝 Overview
Functions allow you to group code into reusable blocks. In this lesson, you’ll learn how to define and call functions, use parameters, and return values to make your code clean, organized, and modular.
🎯 What You’ll Learn
-
How to define and call a function
-
What parameters and arguments are
-
How to return values from a function
-
The difference between built-in and user-defined functions
📘 1. What is a Function?
A function is a named block of code that performs a specific task. Instead of repeating code, you can call a function whenever you need it.
🛠️ 2. Creating a Function
To use (or “call”) the function:
📌 Output:
🎯 3. Function with Parameters
You can pass data into functions using parameters.
📌 Output:
🔁 4. Function with Return Value
Use return
to send a value back from the function:
📌 Output:
🧠 5. Why Use Functions?
✅ Organize code
✅ Reuse code
✅ Reduce errors
✅ Improve readability
🚀 Practice Task 1
Write a function that takes two numbers and returns the bigger one:
🧠 Mini Challenge
Create a function that:
-
Accepts a name and age
-
Returns a string like
"Hi, Ana! You are 20 years old."
✅ Summary
-
Functions help you reuse code efficiently
-
Use
def
to define a function -
Parameters accept input;
return
sends output -
Functions improve your program’s structure
⏭️ Next Lesson Preview
→ Error Handling in Python (try
, except
)