Free tools Windows power users keep installed
One-click scans. No signup required.
Define a function with def, a name, parentheses, a colon, and an indented body. Call it by writing its name followed by parentheses containing any required argument values. A function definition makes reusable code available; a call runs that code.
Define a function with def
In Python 3.14.8, the official tutorial explains that the keyword def introduces a function definition. The function name is followed by parentheses around its parameter list, a colon, and an indented body. The definition creates a function object and binds it to that name.
def greet(name):
"""Return a greeting for one person."""
return f"Hello, {name}!"
Here, greet is the function name and name is a parameter: a name the function uses for an input. Indentation is part of Python syntax, so the body must be indented beneath the def line. The string in triple quotes is a docstring that describes the function.
Call the function and use its result
To run the function, write its name with parentheses and supply a value for its parameter. That value is an argument.
#1 Best Overall
message = greet("Ada")
print(message)
The call greet("Ada") runs the body with name set to "Ada". The function returns "Hello, Ada!", which the assignment stores in message; print then displays it. Defining a function does not run its body—the call does.
Parameters and arguments
Parameters are the names in a function definition; arguments are the actual values passed when calling it. In def greet(name):, name is a parameter. In greet("Ada"), "Ada" is an argument. This distinction follows the Python Programming FAQ for Python 3.14.8.
Rank #2
Return a value instead of only printing
A return statement ends the function and makes its expression’s value available to the caller. Returning lets calling code store, inspect, or use that value. Printing displays text, but does not by itself provide that text as the function’s result. If a function reaches its end without a return, or uses a bare return, its result is None.
Choose how to pass arguments
Python supports positional and keyword arguments. Positional arguments are matched by order; keyword arguments identify the parameter by name. Use the form that makes a call clear, and follow the function’s signature.
| Calling form | Example | How it works |
|---|---|---|
| Positional | power(2, 3) |
Values match parameters by their position. |
| Keyword | power(base=2, exponent=3) |
Values match the named parameters; their order can differ from the definition. |
A keyword must match a parameter name accepted by the function. Do not supply the same parameter more than once—for example, once positionally and again by keyword.
Use defaults for optional inputs
A default value lets a caller omit an argument when there is a sensible standard value.
def greet(name, punctuation="!"):
return f"Hello, {name}{punctuation}"
first = greet("Ada")
second = greet("Ada", punctuation="?")
In this example, the first call uses the default punctuation, while the second overrides it. Python evaluates default expressions once, when it defines the function—not afresh on each call. This matters for mutable values such as lists: changes can persist between calls. When each call needs its own list, use None as the default and create a new list inside the function.
Accept a flexible number of arguments
Use *args to collect extra positional arguments into a tuple, or **kwargs to collect extra keyword arguments into a dictionary. In a call, the same operators can unpack an iterable or mapping into arguments.
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 →Best Value
def show_values(*args, **kwargs):
print(args)
print(kwargs)
items = (10, 20)
options = {"unit": "°C"}
show_values(*items, **options)
The call unpacks items into positional arguments and options into keyword arguments. These flexible forms are useful when a function genuinely needs to forward or accept a variable set of values; ordinary named parameters are clearer for a fixed, small set.
Make the intended calling style explicit
Python signatures can restrict whether callers pass a parameter positionally or by keyword. A slash marks parameters before it as positional-only; a bare asterisk marks parameters after it as keyword-only.
def convert(value, /, *, unit):
return f"{value} {unit}"
convert(12, unit="kg")
Here, value must be supplied positionally, while unit must be supplied by keyword. Use these markers when the intended calling convention should be explicit; otherwise, parameters can generally be supplied positionally or by keyword, subject to the function’s definition.
A practical way to start
- Write a small definition: choose a descriptive name, list the inputs it needs, add a colon, and indent the body.
- Decide what the caller needs back: use
returnfor a value that other code should use; useprintonly when displaying text is the goal. - Call it with a concrete value: match each required parameter with an argument, positionally or by keyword.
- Check the result: store or print the return value so you can see what the function produces.
For a free, structured reference, see the Python 3.14.8 tutorial section on defining functions. The Python 3.14.8 Programming FAQ explains the parameter-and-argument distinction. For precise syntax details, consult the Python 3.14.7 language reference.
The Tool Desk
Outbyte PC Repair FREEClear out junk files and repair common Windows errorsFree Scan →Outbyte Driver Updater FREEScan for outdated or missing drivers - takes under a minuteDriver Scan →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.




