Python_Functions_Class_Notes
Python_Functions_Class_Notes
Definition:
A function is a reusable block of code that performs a specific task. Functions help reduce repetition,
Syntax:
def function_name(parameters):
# code block
Example:
def greet(name):
print("Hello", name)
- Code reusability
Types of Functions:
Return Statement:
Example:
return a + b
result = add(5, 3)
print(result) # Output: 8
Variable-length Arguments:
Example:
def show(*args):
print(arg)
Recursion:
Example:
def factorial(n):
if n == 1:
return 1
return n * factorial(n - 1)