October DealsAmazon USOctober deal check: compare before you payAmazon US: current deals, useful picks and tech finds.Check DealsPC HealthRecommendedCrashes, freezes, slowdowns? Check your PC nowSpot repairable issues before they interrupt work.Check PCOctober DealsAmazon USDeal season is back - check today's better picksAmazon US: current deals, useful picks and tech finds.See Picks×
Skip to content
World desk4 min

Python Functions: What Happens to Names, Values, and Results

Python functions package behavior under a name, but calls, returns, local scope, mutation, and default parameters can all affect what your code does.
Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Putting code in a Python function changes how that code is organized and how its names and results behave. A function gives a block of behavior a name you can call again; it does not automatically return a value, update a caller’s variables, or make a program faster.

What changes when you define a function?

The Python 3.14.8 tutorial says, “The keyword def introduces a function definition.” A def statement creates a function object and binds it to a name. The body runs when that name is called, not simply because the definition was reached.

As an Amazon Associate I earn from qualifying purchases.

Parameters name the inputs a function expects; arguments are the values supplied when calling it. One function can be called from several places, so you can write a behavior once instead of copying its statements. That can make repeated logic easier to update, provided the function has a clear purpose and interface.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Repeated statements versus a function

Suppose a program needs to calculate a sales tax amount in several places:

price = 20.00
 tax = price * 0.08
print(tax)

price = 35.00
 tax = price * 0.08
print(tax)

Moving the calculation into a function gives it a name and makes the rate explicit as an input:

def sales_tax(price, rate):
    return price * rate

print(sales_tax(20.00, 0.08))
print(sales_tax(35.00, 0.08))

The function reduces duplicated calculation code and can be reused with different arguments. For a one-off, obvious statement, a function may add needless indirection; the useful boundary is one that makes behavior easier to understand or reuse.

Why did my function not give me a value?

return passes a result back to the caller. print displays something, but it does not provide that displayed value for another part of the program to store. A function that reaches its end without a return expression returns None.

Free tools Windows power users keep installed

One-click scans. No signup required.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

For example, this function displays a number but does not return it:

def double_and_print(number):
    print(number * 2)

result = double_and_print(4)
print(result)  # None

If the caller needs to use the result, return it and assign the call’s value:

def double(number):
    return number * 2

result = double(4)
print(result)  # 8

When refactoring, check what the original statements did: they may have printed output, changed an object, or calculated a value. Putting them inside a function does not turn printing or mutation into a returned result.

Why is my variable different inside a function?

Each function call has a local namespace. Python looks for a name first among local names, then in enclosing function scopes, then in the module’s global namespace, and finally among built-ins. An assignment in a function binds a local name by default, so it does not normally replace a caller’s variable with the same name.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
total = 10

def update_total():
    total = 25  # A local name, not the module-level total

update_total()
print(total)  # 10

Arguments are passed by assignment: the parameter is a local name referring to the object passed by the caller. Reassigning that parameter does not reassign the caller’s name. But if the object is mutable, changing it in place can be visible to the caller because both names refer to the same object.

def reassign(items):
    items = ["new"]  # Rebinds only the local parameter

def append_item(items):
    items.append("new")  # Mutates the shared list

values = ["old"]
reassign(values)
print(values)  # ["old"]
append_item(values)
print(values)  # ["old", "new"]

For multiple output values, the Python Programming FAQ describes returning a tuple as “almost always the clearest solution.” For example, return subtotal, tax lets a caller write subtotal, tax = calculate(...). Python also provides global and nonlocal declarations to target certain outer bindings, but returning results or mutating an intentionally shared object is often easier to follow.

Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Support on Ko-Fi

Can a function keep a value between calls?

Yes, if a default parameter is mutable. Python evaluates a default argument expression once, when the function definition executes, not afresh on every call. This means a list used as a default is shared across calls:

def add_item(item, items=[]):
    items.append(item)
    return items

print(add_item("a"))  # ["a"]
print(add_item("b"))  # ["a", "b"]

If each call should start with a fresh list, use None as the default and create the list inside the function:

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
def add_item(item, items=None):
    if items is None:
        items = []
    items.append(item)
    return items

Python supports positional and keyword arguments, as well as positional-only and keyword-only parameters. Keyword-only parameters can make calls easier to read when a function has several optional settings. Function annotations are optional metadata stored in __annotations__; they do not enforce types at runtime.

When is moving code into a function useful?

  • Use a function for repeated behavior: one definition gives multiple call sites the same implementation.
  • Return a value when the caller must use a result: reserve printing for output intended to be displayed.
  • Keep the interface clear: use parameters for inputs and avoid surprising state changes, especially shared mutable defaults.
  • Do not add a function just for its own sake: a function is helpful when its name and boundary clarify the program, not because every block needs one.

For structured beginner practice beyond the free official tutorial, Eric Matthes’s Python Crash Course, 4th Edition is a project-based introduction listed by its publisher in print format (ISBN 9781718505148). It is optional; learning this topic does not require a book.

Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply.

Leave a Reply

Your email address will not be published. Required fields are marked *

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

More from the Wire

  1. World desk4 min
    How to Spot an AI Voice Scam Before Sending MoneyDon’t rely on how a caller sounds. Pause, call back through a known number, and verify the emergency with another trusted person before sending money.
  2. Mountain View desk4 min
    Google’s SynthID Detector: How to Check AI-Generated Images, Video and AudioGoogle’s SynthID Detector looks for an embedded watermark in supported images, video and audio. Here is what its results do—and do not—show.
  3. Redmond desk20 min
    How to create a link to File or Folder in Windows 11Windows 11 gives you several ways to point to a file or folder without moving or duplicating it. You can create a desktop shortcut,…
Recommended PC Tool
Recommended PC Tool
PC Slower Than It Used to Be?Free scan - under a minute
Outdated Drivers Are Slowing You DownFree scan - exact matches

Two free Windows tools

One Free Minute Could Fix That PC

Before you go - each of these free tools takes about a minute and tackles what quietly slows a Windows PC down.

Special offer. View Outbyte info, uninstall instructions, EULA, and Privacy Policy.