๐ก Introduction
As programs grow larger, repeating the same code over and over becomes messy and hard to manage.
To solve this, we use functions โ small, reusable code blocks that perform specific tasks.
This concept is known as Modular Programming, where we break a big program into smaller, manageable pieces.
๐งฉ 1. What Is a Function?
A function is a named block of code that performs one particular task.
Once defined, you can call (use) it anytime in your program.
๐ป Example (Python):
def greet():
print("Hello from EduTech!")
greet()
โ Output:
Hello from EduTech!
๐ฌ def is used to define a function, and the code inside runs when the function is called.
โ๏ธ 2. Why Use Functions?
Functions make your code:
- ๐ง Easier to read โ smaller, logical sections
- ๐ Reusable โ you can call the same function multiple times
- ๐งฉ Modular โ break problems into smaller parts
- ๐งน Easier to maintain โ fix or update one part without affecting others
๐งฑ 3. Function Structure
Every function has 3 key parts:
| Part | Description | Example |
|---|---|---|
| Function Name | The name used to identify it | greet() |
| Parameters | Values passed into the function | (name) |
| Return Value | What the function sends back | return result |
๐งฉ Example: Function With Parameters
def greet(name):
print("Hello, " + name + "!")
greet("Alex")
greet("Sara")
โ Output:
Hello, Alex!
Hello, Sara!
๐ฌ Here, name is a parameter, and "Alex" is the argument passed to it.
๐งฎ Example: Function That Returns a Value
def add_numbers(a, b):
return a + b
result = add_numbers(5, 3)
print("The total is:", result)
โ Output:
The total is: 8
๐ก The return keyword sends data back to the main program.
๐ง 4. Modular Programming
Modular Programming is a method where large programs are divided into small, independent modules (functions or files).
Each module handles one specific task โ making the program easier to develop, test, and debug.
๐ป Example:
Instead of writing everything in one place:
โ Without Functions:
print("Area of square:")
side = 5
area = side * side
print(area)
print("Area of circle:")
radius = 3
circle_area = 3.14 * radius * radius
print(circle_area)
โ With Functions:
def area_square(side):
return side * side
def area_circle(radius):
return 3.14 * radius * radius
print("Square area:", area_square(5))
print("Circle area:", area_circle(3))
๐ฌ Cleaner, easier to modify, and reusable!
๐งฉ 5. Built-In vs User-Defined Functions
| Type | Description | Example |
|---|---|---|
| Built-in Functions | Already provided by the language | print(), len(), input() |
| User-defined Functions | Created by you | def greet():, def add(a, b): |
๐ง 6. Good Practices for Functions
โ
Use meaningful names (e.g., calculate_average(), not fun1())
โ
Keep functions short and focused on one task
โ
Add comments to explain complex parts
โ
Use return values instead of printing when possible
โ
Test each function individually
๐ฌ Summary
| Concept | Description |
|---|---|
| Function | A reusable block of code that performs a task |
| Parameter | Input to a function |
| Return Value | Output from a function |
| Modular Programming | Breaking a program into smaller, logical parts |