LeetCampus
Interview Question

Can we Pass a function as an argument in Python?

July 24, 2025
0 views
Difficulty: Medium
Popularity: Common
Share on

Question Explanation

Passing a function as an argument in Python is a fundamental concept that showcases the flexibility of this programming language. Interviewers often ask this question to assess a candidate's understanding of first-class functions, which are functions that can be treated like any other variable. By passing functions as arguments, you can create more dynamic and reusable code, enabling higher-order functions—functions that operate on other functions. This is particularly relevant in functional programming paradigms, which are increasingly popular in modern software development. Understanding this concept is crucial because it allows developers to implement callbacks, decorators, and other advanced programming techniques effectively. Common misconceptions include believing that functions must always be defined beforehand or that they cannot be used interchangeably with other data types. In reality, Python treats functions as first-class citizens, allowing for a range of innovative coding patterns. Knowledge of this topic not only enhances coding skills but also prepares candidates for discussions about design patterns and code optimization.

Sample Answers

Example 1: Using a Simple Callback Function

In Python, you can pass functions as arguments quite easily. For instance, let's consider a scenario where you want to apply a certain function to each item in a list. You can define a simple function that performs an operation and then pass it as an argument to another function. Here’s how:

def square(x):
    return x * x

def apply_function(func, data):
    return [func(item) for item in data]

numbers = [1, 2, 3, 4, 5]
result = apply_function(square, numbers)
print(result)  # Output: [1, 4, 9, 16, 25]

In this example, the square function is passed to apply_function, which applies it to each element in the numbers list. This demonstrates how flexible and powerful function passing can be in Python.

Example 2: Creating a Custom Sorting Function

Another practical use case for passing functions as arguments is when you need to customize sorting behavior. Python’s built-in sorted() function allows you to define a custom sorting mechanism using a function. Here’s an illustrative example:

def sort_key(item):
    return item[1]  # Sort by the second element of the tuple

data = [(1, 'apple'), (3, 'banana'), (2, 'cherry')]
sorted_data = sorted(data, key=sort_key)
print(sorted_data)  # Output: [(1, 'apple'), (3, 'banana'), (2, 'cherry')]

In this code, sort_key is a function that specifies how the sorting should be performed. By passing it to sorted(), we customize the sorting process, showcasing how function arguments can enhance the utility of built-in operations.

Example 3: Using Decorators for Function Modification

Decorators are a powerful feature in Python that leverage the ability to pass functions as arguments. A decorator is essentially a function that takes another function and extends its behavior. Here’s a simple decorator example:

def my_decorator(func):
    def wrapper():
        print('Something is happening before the function is called.')
        func()
        print('Something is happening after the function is called.')
    return wrapper

@my_decorator
def say_hello():
    print('Hello!')

say_hello()

In this code, my_decorator takes say_hello as an argument, allowing us to add behavior before and after the function call. This is a clear illustration of how passing functions can lead to cleaner and more maintainable code through the use of decorators.

Keywords

Pythonfirst-class functionshigher-order functionscallbacksfunctional programming

Ready to practice more questions?

Explore our collection of technical interview questions from top companies.

View All Questions