Use the official Python tutorial on defining functions as your reference: every function starts with def, followed by a name, parentheses for parameters, a trailing colon, and an indented block that runs when the function is called.
Method 1 — Create and Call a Basic Function
def greet():
print("Hello from a function!")
greet()
This establishes the minimal pattern: definition first, call after. Python executes top to bottom, so a function must be defined before it’s called.
Join readers who trust AllThings.How
Add us as a preferred source on Google so our practical guides show up first next time you search.
Add to Google Preferences →Method 2 — Add Parameters and Defaults
def full_name(first, last):
print(first + " " + last)
full_name("Ada", "Lovelace")
def hello(name="World"):
print(f"Hello, {name}!")
hello() # uses default
hello("Python") # overrides default
Defaults simplify callers and keep your API stable when adding optional behavior. See default arguments in the official tutorial.
Method 3 — Return Values
def area(w, h):
return w * h
a = area(3, 4)
print(a)
def point():
return 10, 20
x, y = point()
Details of the return statement are covered in the language reference.
Method 4 — Accept a Variable Number of Arguments
def total(*args):
return sum(args)
print(total(1, 2, 3))
def report(**kwargs):
for k, v in kwargs.items():
print(k, "=", v)
report(name="Keyboard", price=19.99)
This interface lets your function flex to diverse inputs. See “Arbitrary argument lists” in the official tutorial.
Method 5 — Control How Callers Pass Arguments
def echo(x, /):
print(x)
echo(3) # OK
# echo(x=3) # TypeError
def scale(value, *, factor=2):
return value * factor
scale(10, factor=3) # OK
# scale(10, 3) # TypeError
Use these to design clear, safe APIs. See “Special parameters” in the official tutorial.
Method 6 — Document and Organize Your Function
def hypotenuse(a, b):
"""Compute the length of the hypotenuse from legs a and b."""
return (a**2 + b**2) ** 0.5
def main():
print(hypotenuse(3, 4))
if __name__ == "__main__":
main()
Docstring conventions are defined in PEP 257, and the __main__ pattern is covered in the docs on executing modules as scripts.
Method 7 — Use a Lambda for Simple One‑Liners
double = lambda x: x * 2
print(double(5))
def double_def(x):
return x * 2
Lambda syntax is specified in the language reference.
Method 8 — Place Definitions Where They’ll Be Executed Before Use
# Correct: defined before use
def hello():
print("Hello")
hello()
# This will raise NameError at runtime:
# hello()
# def hello():
# print("Hello")
def outer():
def inner():
return "scoped helper"
return inner()
Quick Tips
- “Parameters” are the names in the function header; “arguments” are the values you pass in the call.
- Use
passas a placeholder when stubbing a function body. - Keep functions small and focused; this simplifies testing and reuse.
With def, thoughtful parameters, and clear returns, you’ll write functions that are easy to call, test, and extend. Start simple, then add defaults, *args/**kwargs, and argument rules as your API needs grow.






