LearnFrenzy
Sign in Take a free test

Logic & Functions · Lesson 4 of 6

Python Functions: def, Parameters, Return Values and Scope

Stop repeating code. How to write functions, use default and keyword arguments, return multiple values, and the scope mistake that confuses beginners.

11 min read Beginner Updated Nov 5, 2026 Reviewed by Saurabh Samir

The moment you copy-paste the same block of code twice, it is time for a function. A function gives a name to a piece of logic so you can reuse it, test it and change it in one place.

Defining and calling a function

def percentage(obtained, total):
    """Return the percentage, rounded to 2 decimal places."""
    return round(obtained / total * 100, 2)

print(percentage(436, 500))   # 87.2
  • def starts the function; obtained and total are parameters.
  • The text in triple quotes is a docstring — a short description. Good habit, and interviewers notice it.
  • return sends a value back. A function without return returns None.
def add_print(a, b):
    print(a + b)

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

x = add_print(2, 3)    # shows 5, but x is None
y = add_return(2, 3)   # shows nothing, but y is 5

Use return when the result will be used by other code. Use print only to show something to a person.

Default and keyword arguments

def greet(name, message="All the best for your interview!"):
    return f"Hi {name}, {message}"

print(greet("Arjun"))
print(greet("Arjun", message="Congratulations on the offer!"))

Default values make parameters optional. Keyword arguments (message=...) make calls easier to read.

Classic trap: never use a mutable default like def add(item, items=[]). The same list is reused across calls and keeps growing. Use items=None and create the list inside the function.

Returning more than one value

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

low, high = min_max([72, 91, 64, 88])
print(low, high)   # 64 91

Python actually returns a tuple here, and low, high = ... unpacks it.

Scope: where a variable lives

count = 0

def increment():
    count = count + 1   # UnboundLocalError!

increment()

Assigning to count inside the function makes it a local variable, so Python complains that you used it before giving it a value. The clean fix is to pass values in and return them out:

def increment(count):
    return count + 1

count = increment(count)

(The global keyword also works, but avoid it — functions that quietly change outside variables are hard to debug.)

Small functions are better functions

A good function does one thing and has a name that says what. calculate_gpa(), is_eligible(), read_marks_file() — you can understand the program just by reading the names.

Try it yourself: Write is_palindrome(text) that returns True for "Madam" and "Race car" (ignore case and spaces), and count_vowels(text). Test both with five inputs each, including an empty string.

Common interview questions

Try answering each one aloud before you open it.

1What is the difference between parameters and arguments?

Parameters are the names in the function definition; arguments are the actual values passed when calling the function.

2What are *args and **kwargs?

*args collects extra positional arguments into a tuple; **kwargs collects extra keyword arguments into a dictionary. They let a function accept a variable number of arguments.

3Why should you avoid mutable default arguments?

Default values are evaluated once when the function is defined, so a mutable default (like a list) is shared across calls and keeps changes between them.

Practise this

Build something with it

FAQs

What is a lambda function?

A small anonymous function written in one line, e.g. lambda x: x * 2. Useful for short operations like sorting keys; use def for anything longer.

Can a function call itself?

Yes — that is recursion. It is useful for problems like factorial or tree traversal, but every recursive function needs a base case to stop.

References & further reading

Finished this lesson?

Mark it complete. Create a free account to save it on every device.

Next lesson

Search LearnFrenzy

Esc

Quick links