Functions are reusable blocks of code used to perform specific tasks.
Functions help:
- Reduce code repetition
- Improve readability
- Make programs modular
Defining Functions
Use the def keyword to create a function.
Syntax
def function_name():
# code
Example
def greet():
print("Hello, Welcome to Python")
Calling the function:
greet()
Output:
Hello, Welcome to Python
Function with Parameters
Parameters allow passing data to functions.
def greet(name):
print("Hello", name)
greet("Aditya")
Output:
Hello Aditya
Function Arguments
Arguments are values passed to functions.
Python supports different types of arguments.
1. Positional Arguments
Arguments are assigned based on position.
def add(a, b):
print(a + b)
add(10, 20)
Output:
30
2. Keyword Arguments
Arguments are assigned using parameter names.
def student(name, age):
print(name, age)
student(age=25, name="Aditya")
3. Default Arguments
Default values are used if argument is not provided.
def greet(name="Guest"):
print("Hello", name)
greet()
greet("Rahul")
Output:
Hello Guest
Hello Rahul
4. Variable-Length Arguments
Used when number of arguments is unknown.
*args
Accepts multiple positional arguments.
def total(*numbers):
print(numbers)
total(1, 2, 3, 4)
Output:
(1, 2, 3, 4)
Sum Using *args
def add(*numbers):
total = 0
for num in numbers:
total += num
print(total)
add(1, 2, 3)
**kwargs
Accepts multiple keyword arguments.
def details(**data):
print(data)
details(name="Aditya", age=25)
Output:
{'name': 'Aditya', 'age': 25}
Return Statement
The return statement sends value back from function.
Example
def add(a, b):
return a + b
result = add(10, 20)
print(result)
Output:
30
Multiple Return Values
def calculation(a, b):
return a + b, a - b
sum_value, sub_value = calculation(10, 5)
print(sum_value)
print(sub_value)
Lambda Functions
Lambda functions are anonymous small functions.
Syntax
lambda arguments : expression
Example
square = lambda x: x * x
print(square(5))
Output:
25
Lambda with Multiple Arguments
add = lambda a, b: a + b
print(add(10, 20))
Recursive Functions
A recursive function calls itself.
Used for problems that can be broken into smaller similar problems.
Example: Factorial
def factorial(n):
if n == 1:
return 1
else:
return n * factorial(n - 1)
print(factorial(5))
Output:
120
Example: Fibonacci Series
def fibonacci(n):
if n <= 1:
return n
return fibonacci(n - 1) + fibonacci(n - 2)
for i in range(6):
print(fibonacci(i))
Output:
0
1
1
2
3
5
Scope and Lifetime of Variables
Scope defines where a variable can be accessed.
1. Local Variables
Declared inside function.
Accessible only inside that function.
def test():
x = 10
print(x)
test()
Example: Error Outside Function
def demo():
y = 20
demo()
# print(y) # Error
2. Global Variables
Declared outside function.
Accessible everywhere.
name = "Python"
def show():
print(name)
show()
Modifying Global Variable
Use global keyword.
count = 0
def increase():
global count
count += 1
increase()
print(count)
Output:
1
Lifetime of Variables
- Local variables exist only during function execution.
- Global variables exist throughout program execution.
Nested Functions
Function inside another function.
def outer():
def inner():
print("Inside Inner Function")
inner()
outer()
Practical Example
Calculator Function
def calculator(a, b):
print("Addition:", a + b)
print("Subtraction:", a - b)
print("Multiplication:", a * b)
print("Division:", a / b)
calculator(10, 5)






