Python Guide Sidebar

Functions in Python

What is a Function?

A function in Python is a reusable block of code that performs a specific task. Functions help in modular programming by breaking down complex programs into smaller, manageable, and reusable pieces.

Types of Functions in Python

Python provides two main types of functions:

  1. Built-in Functions – Predefined functions like print(), len(), sum(), etc.
  2. User-defined Functions – Functions created by the user to perform specific tasks.

Defining a Function

A function in Python is defined using the def keyword:

def greet(name):
    """This function greets the user."""
    return f"Hello, {name}!"

print(greet("User"))  # Output: Hello, User!

Function Arguments

Functions can accept parameters to make them more dynamic.

1. Positional Arguments

Arguments are passed in the same order as defined in the function.

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

print(add(5, 3))  # Output: 8
2. Default Arguments

Default values can be assigned to parameters.

def power(base, exponent=2):
    return base ** exponent

print(power(3))  # Output: 9
print(power(3, 3))  # Output: 27
3. Keyword Arguments

Arguments are passed with parameter names to avoid ambiguity.

def introduce(name, age):
    return f"My name is {name} and I am {age} years old."

print(introduce(age=22, name="User"))  # Output: My name is User and I am 22 years old.
4. Arbitrary Arguments
  • args allows passing multiple positional arguments as a tuple.
  • kwargs allows passing multiple keyword arguments as a dictionary.
def sum_all(*numbers):
    return sum(numbers)

print(sum_all(1, 2, 3, 4, 5))  # Output: 15

def person_info(**info):
    return info

print(person_info(name="User", age=22, city="Madurai"))
# Output: {'name': 'User', 'age': 22, 'city': 'Madurai'}

Function Scope and Lifetime

Python has four types of variable scope:

  1. Local Scope – Variables declared inside a function.
  2. Enclosing Scope – Variables from outer functions in nested functions.
  3. Global Scope – Variables declared at the top level of the script.
  4. Built-in Scope – Predefined Python variables and functions.

Common Interview Questions on Functions in Python

📌 What is a function in Python? Explain with an example.

🔹 A function is a reusable block of code that executes when called.

Companies: TCS, Infosys, Capgemini

📌 What is the difference between a function and a method?

🔹 A function is independent, while a method is associated with an object.

Companies: Accenture, Wipro, Oracle

📌 What are lambda functions in Python?

🔹 A lambda function is a one-liner function with no name.

Companies: Cognizant, JPMorgan Chase, IBM

📌 Explain recursion with an example.

🔹 Recursion is a function calling itself, useful in mathematical problems.

Companies: Goldman Sachs, Amazon, Cisco

Conclusion

Functions are an essential part of Python programming, improving code readability, reusability, and maintainability. Mastering different types of functions helps in writing efficient and modular programs.

Additional Resources

🔹 Python Official Documentation – Functions

Functions In Python