📢
Admissions Open for August 2026 Batch | Free Career Counselling | Limited Scholarships
Register Now →

Learning Guides

Functions in Python: Complete Guide with Examples

Quick answer: Learn Python functions including definition, parameters, default arguments, return values, scope, and lambda functions, with practical examples.

What is a Function in Python?

A function is a reusable block of code that performs a specific task. Instead of repeating the same logic, you define it once and call it whenever needed.

def greet(name):
    return f"Hello, {name}!"

print(greet("Fireblaze"))   # Hello, Fireblaze!

Parameters and Arguments

def add_numbers(a, b):
    return a + b

print(add_numbers(5, 3))   # 8

Default arguments

def greet(name, greeting="Hello"):
    return f"{greeting}, {name}!"

print(greet("Priya"))                  # Hello, Priya!
print(greet("Priya", "Good morning"))  # Good morning, Priya!

Keyword arguments

def describe(name, age):
    return f"{name} is {age} years old"

print(describe(age=25, name="Aman"))   # order does not matter with keywords

Return Values

A function without an explicit return statement returns None. A function can also return multiple values as a tuple.

def min_max(numbers):
    return min(numbers), max(numbers)

lowest, highest = min_max([4, 9, 1, 7])

Variable Scope

A variable defined inside a function is local to that function and is not visible outside it.

def set_value():
    x = 10   # local variable
    return x

set_value()
print(x)   # NameError: x is not defined outside the function

*args and **kwargs

Use *args to accept any number of positional arguments and **kwargs to accept any number of keyword arguments.

def total(*numbers):
    return sum(numbers)

total(1, 2, 3, 4)   # 10

def print_info(**details):
    for key, value in details.items():
        print(key, value)

print_info(name="Fireblaze", city="Nagpur")

Lambda Functions

A lambda is a small, unnamed function written on a single line, typically used for short operations passed to another function.

square = lambda x: x ** 2
print(square(5))   # 25

numbers = [1, 2, 3, 4]
doubled = list(map(lambda x: x * 2, numbers))   # [2, 4, 6, 8]

Common Interview Questions

What is the difference between a parameter and an argument?

A parameter is the name listed in the function definition. An argument is the actual value passed in when the function is called.

Can a Python function return multiple values?

Yes, by returning them as a tuple, which can then be unpacked into separate variables by the caller.

When would you use a lambda instead of a regular function?

For short, throwaway operations, especially when passing a simple function to another function such as map, filter or sorted's key argument.

FAQ

Frequently Asked Questions

What is a function in Python?

A function is a reusable, named block of code that performs a specific task, defined once with def and called wherever needed.

What is the difference between a parameter and an argument?

A parameter is the name listed in the function's definition. An argument is the actual value supplied when the function is called.

What does a Python function return if there is no return statement?

It returns None by default.

What is a lambda function used for?

A lambda is a small, unnamed function for short operations, commonly passed directly into functions like map, filter or sorted.

Want This Mapped to Your Own Background?

A free counselling session will tell you which path fits, and will tell you honestly if none of ours does.

Book Free Career Counselling

Keep Reading

Related Articles