Functions
A function is a block of reusable code that performs a specific
task. Functions make programs modular, reusable, and easier to
maintain.
Types of Functions
A. Built-in Functions
These are pre-defined in Python.
Examples:
print(len("Hello")) # 5
print(max([1, 2, 3])) # 3
print(type(10)) # <class 'int'>
B. User-defined Functions
Functions created by the programmer.
def greet(name):
return f"Hello, {name}!"
print(greet("Adi")) # Output: Hello, Adi!
Aspect Normal Function Lambda Function
Name Defined using def, has a Anonymous, no explicit
name name
Lines of Can span multiple lines Single-line only
Code
Return Requires return explicitly Implicit return
Statement
Use Case For complex, reusable For short, one-time use
logic
Example def square(x): return x*x lambda x: x*x