Quick wins for a faster PC:
Fix the driver behind crashes, sound loss and screen glitchesFind Drivers →Repair Windows errors before they cause bigger problemsFix Now →Scan for outdated or missing drivers - takes under a minuteDriver Scan →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.
Repeated statements versus a function
Suppose a program needs to calculate a sales tax amount in several places:
#1 Best Overall
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.
Rank #2
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.
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.
Do these 3 things before closing this tab:
1Clear out junk files and repair common Windows errors2Scan for outdated or missing drivers - takes under a minute3Repair Windows errors before they cause bigger problemstotal = 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.
Best Value
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.
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:
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.
Quick Recap
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.




