DEV Community

Kailas Warade
Kailas Warade

Posted on

Python Functions Explained: 10 Concepts Every Python Developer Should Know

If you are learning Python for Data Analytics, Data Engineering, automation, or backend development, functions are one of the first topics you need to get comfortable with.

But in interviews, knowing only this is not enough:

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

You may be asked what happens when you change the arguments, use "*args", pass keyword arguments, use default values, return multiple values, or access variables outside the function.

Here are 10 Python function concepts worth understanding properly.

  1. Function Definition vs Function Call

A function definition tells Python what the function should do.

def calculate_total(price, quantity):
return price * quantity

Nothing happens when Python reads the definition.

The function runs when you call it:

total = calculate_total(500, 3)

print(total)

Output:

1500

This distinction sounds basic, but it is a common interview question.


  1. Parameters and Arguments Are Not the Same Thing

Consider this function:

def calculate_total(price, quantity):
return price * quantity

Here, "price" and "quantity" are parameters.

When we call:

calculate_total(500, 3)

"500" and "3" are arguments.

A simple way to remember it:

  • Parameter → variable written in the function definition
  • Argument → actual value passed during the function call

This becomes more important when you start working with different types of arguments.


  1. Positional and Keyword Arguments

With positional arguments, Python matches values based on their position.

def create_user(name, age):
print(name, age)

create_user("Rahul", 28)

Here:

name = Rahul
age = 28

With keyword arguments, you explicitly mention the parameter name:

create_user(age=28, name="Rahul")

This can make function calls easier to read, especially when a function has several parameters.

You can also mix them:

create_user("Rahul", age=28)

This is valid because the positional argument comes before the keyword argument.


  1. Default Arguments Make Functions More Flexible

Suppose most users are from India:

def create_user(name, country="India"):
print(name, country)

Now you can call:

create_user("Rahul")

Output:

Rahul India

Or override the default:

create_user("John", "USA")

Output:

John USA

Default arguments are useful when a parameter has a sensible value that will be used most of the time.


  1. What Exactly Does "*args" Do?

Sometimes you don't know how many positional values will be passed.

For example:

def calculate_sum(*args):
return sum(args)

print(calculate_sum(10, 20))
print(calculate_sum(10, 20, 30, 40))

Output:

30
100

Inside the function, "args" is a tuple.

def show_values(*args):
print(args)

show_values(10, 20, 30)

Output:

(10, 20, 30)

The important point is that "*" tells Python to collect additional positional arguments.


  1. What About "**kwargs"?

"**kwargs" is used when you want to accept multiple keyword arguments.

def show_user(**kwargs):
print(kwargs)

show_user(
name="Rahul",
age=28,
city="Pune"
)

Output:

{'name': 'Rahul', 'age': 28, 'city': 'Pune'}

Inside the function, "kwargs" is a dictionary.

This can be useful when the function needs to accept flexible named inputs.

For example:

def create_report(**filters):
for key, value in filters.items():
print(key, value)

create_report(
city="Pune",
department="Sales",
year=2026
)

The names "args" and "kwargs" are conventional. Python does not require those exact names.

For example, this also works:

def show_values(*values):
print(values)

And:

def show_details(**details):
print(details)

The "" and "*" are what matter.


  1. "return" Is Different From "print()"

This is an important concept for beginners.

Consider:

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

The function displays the result, but it does not return the result.

Now compare it with:

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

You can store the returned value:

result = add(10, 20)

print(result)

This matters when functions are combined.

For example:

def calculate_discount(price, discount):
return price - (price * discount / 100)

final_price = calculate_discount(1000, 10)

print(final_price)

The returned value can be stored, passed to another function, or used in a larger calculation.


  1. Can a Python Function Return Multiple Values?

Yes.

def get_employee():
return "Rahul", "Data Engineer", 85000

You can receive the result like this:

name, role, salary = get_employee()

print(name)
print(role)
print(salary)

Python actually returns these values as a tuple.

You can see this directly:

result = get_employee()

print(result)

Output:

('Rahul', 'Data Engineer', 85000)

This is useful when a function needs to return a small group of related values.


  1. Understand Local and Global Variables

Consider:

name = "Rahul"

def show_name():
print(name)

show_name()

The function can read the global variable.

But a variable created inside a function normally belongs to that function:

def show_name():
name = "Rahul"
print(name)

show_name()

print(name) # NameError

The variable "name" created inside the function is local to that function.

You may also see the "global" keyword:

count = 0

def increase():
global count
count += 1

increase()

print(count)

Although "global" is valid, changing global state inside functions should generally be done carefully because it can make code harder to understand and test.


  1. One Interview Question That Tests Your Understanding

What will this code print?

def calculate(a, b=10):
return a + b

print(calculate(5))
print(calculate(5, 20))

The answer is:

15
25

Why?

In the first call:

calculate(5)

Python uses the default value:

a = 5
b = 10

In the second call:

calculate(5, 20)

The supplied value "20" replaces the default.

Questions like this are more useful than simply memorizing the definition of a function.


What Should You Prepare for a Python Interview?

For functions, don't stop at the basic syntax.

Make sure you can explain and write examples for:

  • Function definition and function call
  • Parameters vs arguments
  • Positional arguments
  • Keyword arguments
  • Default arguments
  • "*args"
  • "**kwargs"
  • "return" vs "print"
  • Returning multiple values
  • Local and global scope
  • "global" keyword
  • Lambda functions
  • Nested functions
  • Recursion
  • Decorators

The important part is not just remembering the definition.

Write the code, run it, change the input, and see what Python actually does.

That is how these concepts become much easier to remember during an interview.

More Python Function Interview Questions

If you want a more detailed interview-focused reference, I have covered Python functions with examples and explanations here:

👉 "Python Functions Interview Questions and Answers" (https://www.sankalandtech.com/Tutorials/Python/interview-questions/python-functions-tutorial.html)

The page goes deeper into Python function concepts and can be useful as a revision page before a Python interview.

Final Thought

Functions look simple when you first learn Python.

But once you understand how arguments, return values, scope, "args", "*kwargs", and nested functions work, you start writing much more reusable Python code.

And that is the real purpose of learning functions — not just answering an interview question, but being able to break a real problem into smaller pieces of code that you can reuse and test.

Top comments (0)