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?
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).
# No parameters, no explicit return
print("Hello, CodDesire learner!") # Prints a message
greet() # Call the function
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?
- 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.
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)10This 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
def name(params): …
call like f(2, 3) or f(x=2, y=3)
use parameters inside
return result
store, print, test, or reuse
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):
- Positional-only (
/) - Positional-or-keyword
- Var-positional (
*args) - Keyword-only (after
*) - Var-keyword (
**kwargs)
Don’t worry—you’ll use the middle ones most often. Here’s a quick peek so you know they exist:
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))(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.
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))24
24Beginner guide to default parameters in Python
You can provide defaults so callers don’t have to pass every value:
return base ** exponent
print(power(5)) # uses default exponent 2
print(power(2, 3)) # override default
25
8Important: default values are created once, at function definition time, not each call. Avoid mutable defaults like lists or dicts.
# 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'))['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.
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))62.8318
62.8Trying 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 |
| Show text to the screen | Display messages or debug output | print(add(2,3)) |
Return one value
def average(a, b):
return (a + b) / 2
print(average(10, 20))15.0Return multiple values (tuple)
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)3 2Return early
Use return to exit a function as soon as you have the answer or to handle invalid input simply.
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))3.0
NoneStep-by-step: python function with return statement (from blank to final)
- Write the function header with parameters.
- Compute the result.
- Return the result.
# 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))37.0More examples of functions in Python
Function with two parameters and a clear return value
def full_name(first, last):
return f"{first} {last}"
print(full_name("Ada", "Lovelace"))Ada LovelaceUsing *args to accept flexible numbers of inputs
def summarize(*numbers):
return sum(numbers)
print(summarize(1, 2, 3))
print(summarize())6
0Type 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.
def area_square(edge: float) -> float:
return edge * edge
print(area_square(4.0))16.0In modern Python (3.12+), you can also write simple generic functions with inline type parameters:
def first[T](items: list[T]) -> T:
return items[0]
print(first([10, 20, 30]))10Type 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
returnit, callers will getNone.
c = a + b # computed but not returned
print(add_bad(1, 2)) # None
None- Using mutable default parameters: Use
Nonesentinel 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
printinstead ofreturn:printdisplays on screen;returngives 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 →
Noneor 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
assertchecks.
Practice: write and call your own function
- Create a function
hypotenuse(a, b)that returns the length of the hypotenuse of a right triangle. - Call it twice with different values to check your result.
def hypotenuse(a, b):
return (a**2 + b**2) ** 0.5
print(hypotenuse(3, 4))
print(hypotenuse(5, 12))5.0
13.0FAQ: 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 withreturn. - 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
- Python Tutorial: Defining functions, defaults, return, and special parameters — docs.python.org/3/tutorial/controlflow.html
- Python Language Reference: Function definitions — docs.python.org/3/reference/compound_stmts.html
- PEP 3102: Keyword-Only Arguments — peps.python.org/pep-3102
- PEP 570: Positional-Only Parameters — peps.python.org/pep-0570
- PEP 695: Type Parameter Syntax (generics) — peps.python.org/pep-0695
- typing — Support for type hints — docs.python.org/3/library/typing.html
Keep practicing by writing your own Python functions with parameters and return values. Explore more beginner topics and examples on our Python learning path.


