π Working with Functions
πΉ 1. Introduction to Functions
π What is a Function?
A function is a block of reusable code that
performs a specific task.
π Instead of writing the
same code again and again, we use functions.
π Advantages of Functions
- Code
reusability
- Reduces
redundancy
- Improves
readability
- Easy
debugging
- Modular
programming
π Types of Functions
- Built-in
functions → print(), len()
- User-defined
functions
- Recursive
functions
- Lambda
(anonymous) functions
πΉ 2. Creating User-Defined Functions
π Syntax:
def function_name(parameters):
# body of function
return value
π Example:
def greet():
print("Hello Students")
π Calling function:
greet()
πΉ 3. Function Arguments (VERY IMPORTANT)
➤ 1. Required (Positional)
Arguments
- Must
be passed in correct order
def add(a, b):
print(a + b)
add(5, 3)
➤ 2. Default Arguments
- Assign
default value
def
greet(name="Guest"):
print("Hello", name)
greet()
greet("Bhav")
➤ 3. Keyword Arguments
- Values
passed using parameter names
add(b=10,
a=5)
➤ 4. Variable Length Arguments
πΈ *args (Non-keyword)
def total(*nums):
print(sum(nums))
total(1,2,3,4)
πΈ **kwargs (Keyword
arguments)
def info(**data):
print(data)
info(name="Bhav", age=20)
πΉ 4. Return Statement
π Purpose:
- Returns
result to caller
- Ends
function execution
def square(x):
return x*x
π Multiple Return Values
def calc(a, b):
return a+b, a-b
sum1, diff = calc(10, 5)
πΉ 5. Scope of Variables
π Local Variable
- Defined
inside function
- Accessible
only within function
def demo():
x = 10
π Global Variable
- Defined
outside function
- Accessible
everywhere
x = 20
def show():
print(x)
π Using global Keyword
x = 5
def change():
global x
x = 10
πΉ 6. Recursion (IMPORTANT)
π Definition:
Function calling itself
π Example: Factorial
def factorial(n):
if n == 1:
return 1
return n * factorial(n-1)
π Key Points:
- Must
have base condition
- Otherwise
→ infinite recursion
πΉ 7. Lambda Functions (Anonymous Functions)
π Definition:
Small one-line
function without name
π Syntax:
lambda arguments: expression
π Example:
square = lambda x: x*x
print(square(5))
π Use Cases:
- Short
operations
- Used
with map(), filter()
πΉ 8. Built-in Functions (Common)
|
Function |
Use |
|
len() |
Length |
|
sum() |
Total |
|
max() |
Maximum |
|
min() |
Minimum |
πΉ 9. Functions with Lists
def total(lst):
return sum(lst)
print(total([1,2,3]))
πΉ 10. Functions with Strings
def count_vowels(s):
count = 0
for ch in s:
if ch.lower()
in "aeiou":
count += 1
return count
πΉ 11. Passing Arguments (Important Concept)
π Call by Value vs
Reference (Python Concept)
- Immutable
→ passed by value (int, string)
- Mutable
→ passed by reference (list)
def modify(lst):
lst.append(100)
l = [1,2]
modify(l)
print(l)
πΉ 12. Docstring in Functions
π Purpose:
- Describe
function
def add(a, b):
"""This function returns sum"""
return a + b
πΉ 13. Good Programming Practices
✔ Use meaningful function names
✔ Keep functions short
✔ Use comments/docstrings
✔ Avoid global variables
π§ Important Viva Questions
- What
is a function?
- Difference
between parameter and argument?
- What
is recursion?
- What
is lambda function?
- Difference
between local and global variable?
- What
is default argument?
- What is *args and **kwargs?
No comments:
Post a Comment