Interview Q&A · Python · 25 questions in this topic

Functions — Python Interview Q&A

All Python topics

Defining and calling functions, the parameter rules interviewers probe, closures, recursion and the functional tools in the standard library. Part of Functions · 375 questions in the Python bank.

How do you define a function in Python?

Answer

Functions are defined with the def keyword, a name, a parenthesized parameter list, and an indented body. A function returns None implicitly if it has no explicit return statement.

Unlike some languages, Python doesn't require declaring a return type, and a function can return different types from different code paths (since Python is dynamically typed). A return with no value, or falling off the end of the function without hitting return at all, both produce None.

Example

Example.pypython
def add(a, b):
    return a + b
def log_message(msg):
    print(msg)
    # no explicit return -> returns None
result = log_message("hello")
print(result) # None

💡 Mentioning that "a function with no return returns None " unprompted is a small detail that separates careful answers from rushed ones — it comes up constantly when debugging unexpected None values.

What is the difference between parameters and arguments?

Answer

A parameter is the name listed in a function's definition; an argument is the actual value passed in when the function is called. The terms are often used interchangeably in casual conversation, but interviewers sometimes probe the distinction directly.

Think of parameters as the placeholders a function declares it needs, and arguments as what you actually hand it at the call site.

Example

Example.pypython
def greet(name, greeting="Hello"): # name, greeting -> parameters
    return f"{greeting}, {name}!"
print(greet("Ana", "Hi")) # "Ana", "Hi" -> arguments

💡 A quick, precise answer here ("parameters are declared, arguments are passed") signals attention to terminology that experienced interviewers notice.

What are positional arguments, and how does their order matter?

Answer

Positional arguments are matched to parameters strictly by their order in the function call — the first argument fills the first parameter, the second fills the second, and so on.

Getting the order wrong doesn't raise an error if the types happen to be compatible — it just silently produces the wrong result, which makes positional-argument mistakes a common source of subtle bugs, especially with several same-typed parameters in a row.

Example

Example.pypython
def describe_pet(name, species):
    return f"{name} is a {species}"
print(describe_pet("Rex", "dog")) # 'Rex is a dog' -- correct order
print(describe_pet("dog", "Rex")) # 'dog is a Rex' -- swapped! no error, just wrong

Recommended resourcesAffiliate links · may earn us a commission

Classic DSA interview booksThe reference books interviewers themselves learned from — great next to the visualizers.See the books Structured coding coursesVideo courses that pair well with visual practice — pick one language and go deep.Browse courses Cloud hosting for side projectsShip the portfolio project that gets you hired — free credits for new accounts.Claim credits