Python Revision — Functions — Lesson
1) Hook — A Fun Real-Life Example
Imagine you are the head chef in a busy Indian restaurant in Delhi. Every day, you prepare popular dishes like Paneer Butter Masala and Dal Tadka. Instead of cooking each dish from scratch every time, you create a special recipe card for each dish. Whenever an order comes in, you just follow the recipe card — saving time and ensuring consistency.
In programming, functions are like these recipe cards: they help you write reusable blocks of code that perform specific tasks, making your program organized and efficient.
2) Core Concepts — Python Functions Explained
What is a Function?
A function is a named block of code designed to perform a particular task. It helps avoid repetition and makes programs modular.
| Syntax | Description |
|---|---|
def function_name(parameters):
# code block
return value
|
Defines a function named function_name that optionally takes parameters and returns a value. |
Example 1: Simple Function without Parameters
def greet():
print("Namaste! Welcome to CBSE Python class.")
greet() # Function call
Output: Namaste! Welcome to CBSE Python class.
Example 2: Function with Parameters and Return Value
def add_numbers(a, b):
return a + b
result = add_numbers(10, 20)
print("Sum is:", result)
Output: Sum is: 30
Types of Functions:
| Function Type | Description | Example |
|---|---|---|
| No parameter, no return | Performs task, returns nothing |
def greet():
print("Hello")
|
| With parameter, no return | Takes input, performs task, no output |
def print_name(name):
print(name)
|
| No parameter, with return | Returns value without input |
def get_pi():
return 3.14
|
| With parameter and return | Takes input and returns output |
def square(num):
return num * num
|
Function Call: To execute a function, write its name followed by parentheses and required arguments.
3) Key Formulas / Rules
- Defining a function:
def function_name(parameters): - Indentation: The code block inside the function must be indented (usually 4 spaces).
- Return statement:
return valuesends back a result to the caller. - Function call:
function_name(arguments)executes the function. - Parameters vs Arguments: Parameters are variables in function definition; arguments are actual values passed.
- Default parameters: You can assign default values to parameters like
def greet(name="Student"): - Keyword arguments: Call functions using parameter names
add_numbers(b=5, a=10)
4) Did You Know?
Python functions can be recursive, meaning a function can call itself! This is useful in problems like calculating factorials or Fibonacci numbers — just like how Indian mathematician Pingala used recursion concepts over 2000 years ago in his work on Sanskrit prosody.
5) Exam Tips — Common Mistakes & Board Patterns
- Indentation errors: Always indent the function body properly; otherwise, Python throws an error.
- Missing parentheses: When calling a function, don’t forget the parentheses, even if no parameters are needed.
- Return statement: If you want to get a result back, use
return. Otherwise, the function returnsNone. - Parameter mismatch: Ensure the number and order of arguments match the function definition unless using keyword arguments.
- Board exam pattern: Expect questions like:
- Write a function to perform a specific task (e.g., calculate factorial, check palindrome).
- Identify errors in given function code.
- Explain function concepts or output prediction.
- Write recursive functions.
- Practice previous year questions: For example, CBSE 2023 asked to write a function to find the largest of three numbers using functions.
Python Revision — Functions — Mcq
Python Revision — Functions — Mnemonic
Mnemonic 1: "DEF Your FUNction, Yaar!" 🎉🐍
- Define a function with
def - Enter parameters inside parentheses
( ) - Function body indented properly
- You call it by its name to execute
- FUNction helps avoid repeated code!
Hindi twist: "Def kar, Fir Use Kar!" (Define it, then use it!)
Mnemonic 2: "RAPID Return in Python Functions" 🚀🔄
- Return value to send output
- Arguments pass data inside
- Parameters receive arguments
- Indentation defines the function block
- Def keyword starts function definition
Rhyming line: "Def se start, indent smart, return karo apna part!"
Mnemonic 3: "CALL ME, Python Function!" 📞🐍
- Create function using
def - Arguments inside parentheses
- Logic written inside with indentation
- Lastly, Return statement sends output
- MEans: Make Execution by calling function name
Funny Hindi phrase: "Call kar function ko, milega solution bro!"
Mission: Master This Topic!
Reinforce what you learned with fun activities
Ready to Battle? Test Your Knowledge!
Practice MCQs, build combos, climb the leaderboard!
Start Practice