Function Basics
Defining and calling functions
Interview Relevant: Fundamental concept
Function Basics
Functions are reusable blocks of code.
Code Examples
Basic function definition and calling.
python
1# Define function
2def greet(name):
3 """Greet a person by name."""
4 return f"Hello, {name}!"
5
6# Call function
7result = greet("Alice")
8print(result) # Hello, Alice!
9
10# Function with no return
11def say_hello():
12 print("Hello!")
13# Returns None implicitly
14
15# Multiple return values
16def min_max(numbers):
17 return min(numbers), max(numbers)
18
19lo, hi = min_max([1, 5, 3])
20
21# Pass function as argument
22def apply(func, value):
23 return func(value)
24
25apply(len, "hello") # 5