What is a Function?
The Problem Without Functions
Look at this program:
You're writing the same logic 3 times. Now imagine doing this for 100 rectangles. Nightmare.
Functions solve this problem.
What is a Function?
A function is a reusable block of code that you write once and use as many times as you want.
Real life analogy: Think of a function like a mixer/blender. You built it once. Now whenever you want to blend something — you just put ingredients in and press the button. You don't rebuild the blender every time.
Basic Syntax
defkeyword tells Python "I'm defining a function"function_nameis what you name it:colon at the end — just like if/for/while- Code inside must be indented
Your First Function
Output:
Hello!
Welcome to Python
Defining a function doesn't run it. You have to call it by writing its name with ().
Call it Multiple Times
Output:
Hello!
Welcome to Python
Hello!
Welcome to Python
Hello!
Welcome to Python
Write once, use many times. That's the power.
Parameters — Giving Input to Functions
Right now our function does the same thing every time. What if we want it to greet different people?
We use parameters — variables that receive values when the function is called:
Output:
Hello, Gagan!
Welcome to Python, Gagan!
Hello, Rahul!
Welcome to Python, Rahul!
Hello, Priya!
Welcome to Python, Priya!
Here name is a parameter — it acts like a variable inside the function that receives whatever value you pass when calling.
Multiple Parameters
Output:
Hello Gagan, from Delhi!
Hello Rahul, from Mumbai!
Order matters — first value goes to first parameter, second to second.
Now Fix the Rectangle Problem
Output:
Area = 50
Area = 24
Area = 105
Same logic written once, used 3 times. Clean and simple.
Return — Getting Output from a Function
So far our functions just print things. But what if you want to use the result of a function somewhere else in your code?
Use the return keyword:
Now you can store the returned value:
Output:
The area is 50
Total area = 74
print vs return — Important Difference
This confuses many beginners. Let's clear it up:
Rule of thumb:
- Use
printinside a function only for debugging or when displaying is the only purpose - Use
returnwhen you need to use the result somewhere else — which is most of the time
Default Parameters
You can give parameters a default value — used when caller doesn't provide one:
Output:
Good morning, Gagan!
Good evening, Rahul!
Happy birthday, Priya!
Parameters with defaults must always come after parameters without defaults:
def greet(message="Hello", name): # WRONG
def greet(name, message="Hello"): # CORRECT
Returning Multiple Values
Python functions can return more than one value:
Output:
Min: 2, Max: 8
min() and max() are Python built-in functions that find smallest and largest values.
Scope — Where Variables Live
This is important to understand:
Local variable — created inside a function, only exists inside that function:
Global variable — created outside all functions, accessible everywhere:
Think of it like this — what happens in a function, stays in a function. Local variables are created when the function runs and destroyed when it finishes.
Real World Example — Calculator with Functions
Let's rebuild the calculator properly using functions:
Notice how clean this is — each operation has its own function. If you want to change how addition works, you only change it in one place.
Why Functions Matter — Summary
Without functions:
- Code is repeated everywhere
- Hard to fix bugs — you have to fix the same thing in 10 places
- Hard to read and understand
With functions:
- Write once, use anywhere
- Fix in one place — fixed everywhere
- Code is organized and readable
This principle is called DRY — Don't Repeat Yourself. It's one of the most important principles in programming.
Exercise 🏋️
Build a Student Grade Calculator using functions:
Create these functions:
get_average(marks)— takes a list of 5 subject marks as separate parameters and returns the averageget_grade(average)— takes average and returns grade (A/B/C/D/F using same logic as before)print_report(name, average, grade)— prints a formatted report card
Then write a main program that:
- Asks for student name
- Asks for marks in 5 subjects
- Calls all three functions and displays the report
Expected output:
=== Report Card ===
Student: Gagan
Marks: 85, 90, 78, 92, 88
Average: 86.6
Grade: B
===================
No comments:
Post a Comment