Mastering Python Functions: Parameters and Return Values

Mastering Python Functions: Parameters and Return Values


Functions are one of the most useful building blocks in Python. On this page, you’ll learn Python functions with parameters and return values in a beginner-friendly way: what they are, why they matter, how to write a function in Python, how to pass arguments, and how to get results back from your code. By the end, you’ll be able to write clear, reusable functions for school, college, and your own projects. For a broader introduction to Python basics, visit our Python tutorials hub.

What is a function in Python?

Python functions with parameters and return values illustrated as inputs to a function box and a single output arrow
Parameters flow into a function and a single return value comes out.

A function is a named, reusable block of code that performs a specific task. You define a function once and then call it whenever you need it. Functions can take input values (parameters) and can send back a result (a return value).

def greet():
# No parameters, no explicit return
print("Hello, CodDesire learner!") # Prints a message

greet() # Call the function
Output
Hello, CodDesire learner!

In practice, you’ll usually write functions that accept input and give you a result back. That’s where parameters and return values shine.

Why use parameters and return values?

Python functions with parameters and return values shown as a function producing three outputs unpacked into variables
One function call returns multiple values, unpacked into separate variables.
  • Reuse logic with different inputs (don’t copy–paste code).
  • Test and debug easily by isolating tasks.
  • Get clear results back for further processing.
  • Make your code readable, modular, and easier to maintain.

Syntax: how to write a function in Python

Basic syntax

Use the def keyword, a name, parentheses with parameters, and a colon. Return a result with return.

Code
def add(a, b):
    # simple python function with two parameters
    total = a + b
    return total  # return value in python function

result = add(3, 7)
print(result)
Output
10

This is a small but complete example of a Python function that returns a value. The two inputs a and b are parameters, and the returned sum is the function’s result.

Function call flow: from parameters to return value

How a Python function call flows
1. Define
def name(params): …
2. Pass arguments
call like f(2, 3) or f(x=2, y=3)
3. Compute
use parameters inside
4. Return value
return result
5. Use it
store, print, test, or reuse

Tip: return hands data back to your program; print only shows text on screen.

Parameters vs. Arguments (for beginners)

Students often hear “function arguments vs parameters” and wonder what’s the difference. Here’s a quick summary:

Term Meaning Example
Parameter Variable listed in a function’s definition In def add(a, b):, a and b are parameters.
Argument Actual value you pass when calling the function In add(3, 7), 3 and 7 are arguments.

Kinds of parameters in Python (beginner overview)

Python supports five kinds of parameters (in this order):

  1. Positional-only (/)
  2. Positional-or-keyword
  3. Var-positional (*args)
  4. Keyword-only (after *)
  5. Var-keyword (**kwargs)

Don’t worry—you’ll use the middle ones most often. Here’s a quick peek so you know they exist:

Code
def demo(a, /, b, *args, c, **kwargs):
    # a is positional-only(must be passed by position)
    # b can be positional or keyword
    # *args captures extra positional arguments as a tuple
    # c is keyword-only(must be named)
    # **kwargs captures extra keyword arguments as a dict
    return a, b, args, c, kwargs

print(demo(1, 2, 3, 4, c=5, flag=True))
Output
(1, 2, (3, 4), 5, {'flag': True})

As a beginner, focus mainly on “positional-or-keyword” parameters and return values; you’ll add the rest as you grow.

How to pass arguments to a function in Python

You can pass arguments by position or by keyword name. Keyword arguments make code clearer, especially when there are many inputs.

Code
def rectangle_area(width, height):
    return width * height

# Positional arguments
print(rectangle_area(4, 6))

# Keyword arguments(order doesn’t matter when named)
print(rectangle_area(height=6, width=4))
Output
24
24

Beginner guide to default parameters in Python

You can provide defaults so callers don’t have to pass every value:

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

print(power(5)) # uses default exponent 2
print(power(2, 3)) # override default
Output
25
8

Important: default values are created once, at function definition time, not each call. Avoid mutable defaults like lists or dicts.

Code
# Bad: mutable default can accumulate values across calls
def append_item_bad(item, items=[]):
    items.append(item)
    return items

print(append_item_bad('a'))
print(append_item_bad('b'))  # Surprise: previous list is reused!

# Good: use None and create a new list inside
def append_item(item, items=None):
    if items is None:
        items = []
    items.append(item)
    return items

print(append_item('a'))
print(append_item('b'))
Output
['a']
['a', 'b']
['a']
['b']

Keyword-only and positional-only parameters

As you improve your APIs, you can force some parameters to be positional (using /) and some to be keyword-only (using *). This helps readability and future-proofing.

Code
def cylinder_volume(radius, /, height, *, pi=3.14159):
    # radius: positional-only
    # height: positional-or-keyword
    # pi: keyword-only
    return pi * radius * radius * height

print(cylinder_volume(2, 5))
print(cylinder_volume(2, height=5, pi=3.14))
Output
62.8318
62.8

Trying to call cylinder_volume with radius=2 would raise a TypeError because radius is positional-only.

Return values in Python functions

A function always returns a single Python object. If you write return a, b, that single object is a tuple (a, b). If a function reaches the end without a return, it returns None.

Return vs print in Python functions (quick comparison)

Action Primary purpose Use when you need Tiny example
return Send data back to the caller Store, reuse, or test the result def add(a,b): return a+b
print Show text to the screen Display messages or debug output print(add(2,3))

Return one value

Code
def average(a, b):
    return (a + b) / 2

print(average(10, 20))
Output
15.0

Return multiple values (tuple)

Code
def divide_with_remainder(a, b):
    # python function that returns a value example(two values as one tuple)
    q = a // b
    r = a % b
    return q, r

quotient, remainder = divide_with_remainder(17, 5)
print(quotient, remainder)
Output
3 2

Return early

Use return to exit a function as soon as you have the answer or to handle invalid input simply.

Code
def safe_sqrt(x):
    if x < 0:
        return None  # signal "no real square root"
    return x ** 0.5

print(safe_sqrt(9))
print(safe_sqrt(-4))
Output
3.0
None

Step-by-step: python function with return statement (from blank to final)

  1. Write the function header with parameters.
  2. Compute the result.
  3. Return the result.
Code
# Step 1: header
def to_celsius(fahrenheit):
    # Step 2: compute
    c = (fahrenheit - 32) * 5 / 9
    # Step 3: return
    return c

print(to_celsius(98.6))
Output
37.0

More examples of functions in Python

Function with two parameters and a clear return value

Code
def full_name(first, last):
    return f"{first} {last}"

print(full_name("Ada", "Lovelace"))
Output
Ada Lovelace

Using *args to accept flexible numbers of inputs

Code
def summarize(*numbers):
    return sum(numbers)

print(summarize(1, 2, 3))
print(summarize())
Output
6
0

Type hints (optional but helpful)

Type hints make your function inputs and outputs clearer to readers and tools. Python doesn’t enforce them at runtime by default, but editors and linters use them to catch mistakes early.

Code
def area_square(edge: float) -> float:
    return edge * edge

print(area_square(4.0))
Output
16.0

In modern Python (3.12+), you can also write simple generic functions with inline type parameters:

Code
def first[T](items: list[T]) -> T:
    return items[0]

print(first([10, 20, 30]))
Output
10

Type hints are great for learning and teamwork, but remember: they guide tools; they don’t change how Python runs your function.

Common mistakes and how to fix them

  • Forgetting to return: If you compute a value but never return it, callers will get None.
def add_bad(a, b):
c = a + b # computed but not returned

print(add_bad(1, 2)) # None
Output
None
  • Using mutable default parameters: Use None sentinel and create inside the function (see earlier example).
  • Mixing up arguments and parameters: Arguments are values you pass; parameters are variables in the function definition.
  • Forgetting keyword-only rules: If you declare a parameter after *, you must pass it by name.
  • Relying on print instead of return: print displays on screen; return gives a value your program can use.

Beginner checklist: Python functions with parameters and return values

  • Pick a clear name that says what the function returns (e.g., total_cost, to_celsius).
  • Choose parameters you truly need; prefer keyword args in calls for clarity.
  • Use safe defaults; avoid mutable defaults (lists/dicts). Use None + create inside.
  • Return data from the function; only print for user-facing messages.
  • Handle edge cases early with a guard return (e.g., invalid input → None or raise).
  • Add a short docstring and optional type hints to show expected inputs/outputs.
  • Keep functions focused: one job, one clear return value.
  • Test quickly with a couple of calls or simple assert checks.

Practice: write and call your own function

  1. Create a function hypotenuse(a, b) that returns the length of the hypotenuse of a right triangle.
  2. Call it twice with different values to check your result.
Code
def hypotenuse(a, b):
    return (a**2 + b**2) ** 0.5

print(hypotenuse(3, 4))
print(hypotenuse(5, 12))
Output
5.0
13.0

FAQ: Python functions with parameters and return values

What is a function in Python for beginners?

A function is a named block of code you can reuse. It can take inputs (parameters) and can send back a result (return value). You define one with def and call it by name.

How do you add parameters to a Python function?

List variable names inside the parentheses in the function definition. Example: def greet(name):. You can also add defaults like def power(base, exponent=2):.

What is the difference between arguments and parameters in Python?

Parameters are the variable names in the function definition; arguments are the actual values you supply when calling the function.

How do you return multiple values from a Python function?

Return a tuple, like return a, b. The caller can unpack it: x, y = func(). Under the hood, it’s one object (a tuple) containing multiple items.

Why use return instead of print in a function?

print shows text to the screen; it doesn’t give data back to your program. return hands a value back so other code can use it, store it, or test it.

Key takeaways

  • Define functions with def, accept inputs via parameters, and produce results with return.
  • Use default values for convenience, but avoid mutable defaults.
  • “Multiple” return values are just a tuple returned as one object.
  • Prefer keyword arguments for clarity when functions have many options.
  • Type hints improve readability and tooling without changing runtime behavior.

Sources / Further reading

Keep practicing by writing your own Python functions with parameters and return values. Explore more beginner topics and examples on our Python learning path.

Subscribe
Notify of
guest
0 Comments
Oldest
Newest Most Voted