Functions in Python — Python Technical Interview Questions & Answers
Functions in Python
Functions in Python
- A function is a reusable block that does one job. You write it once with def, then call it. square(5) should return 25 — not only print it, if the caller needs the number.
- Shape: def square(n): return n * n. Colon, indented body. Call: print(square(5)). Output 25. Without return, the function gives None. Freshers print inside and wonder why x = square(5) is None.
- Defaults: def greet(name="Guest"). Call greet() or greet("Asha"). Keep one job per function. Name it after what it does: total_marks, not do_stuff.
- Let’s do it on the board. def square(n): return n * n. print(square(5)) → 25. Change to square(6) in your head → 36. That dry-run is the example.
- Functions in Python — output: 25. square(5) returns 25. Change to square(6) → 36.
def square(n):
return n * n
│ square(5)
▼
25Technical Interview Tip: Explain return vs no return.
Code Examples for Functions in Python
Example
# Functions
def square(n):
return n * n
print(square(5))
Functions in Python — output: 25. square(5) returns 25. Change to square(6) → 36.
How to Explain Functions in Python to an Interviewer
Explain Functions as if you are teaching a junior — definition, then one tiny script.
A function is a reusable block that does one job. Shape: def square(n): return n * n.
What does the example print, and why?
Functions in Python — output: 25
What mistake do freshers make with Functions?
Defaults: def greet(name="Guest").