TetSharing
Python tutorial · 22 chapters
  1. Get started
  2. Syntax and comments
  3. Variables
  4. Numbers and math
  5. Strings
  6. f-strings and formatting
  7. Booleans and logic
  8. if, elif, else
  9. Lists
  10. Tuples
  11. Dictionaries
  12. Sets
  13. for loops and range()
  14. while, break, continue
  15. Functions
  16. Default and keyword arguments
  17. Lambda and sorting keys
  18. List comprehensions
  19. Errors and exceptions
  20. Modules and imports
  21. Reading and writing files
  22. Classes and objects

Final Python quiz
Cheat sheet: python string methods
Cheat sheet: python lists dicts

Chapter 15 of 22 · By Ethan Cole · Updated November 21, 2026

Python Functions: def, Parameters and return

A function packages a piece of logic under a name so you can run it whenever you need it, with different inputs each time. Functions are the main tool for keeping programs short, readable and testable. You have been calling built-in ones like print() and len(); now you will write your own.

Defining and calling

def greet(name): starts a definition. name is a parameter: a local variable that receives whatever value the caller passes, which is called an argument. The indented body runs only when the function is called, as in greet("Noah"). Defining a function does not run it.

return sends a value back to the caller and ends the function at once. The caller can print it, store it or pass it on. A function without a return, or with a bare return, gives back the special value None. That is why result = log("saved") stores None: log prints but does not return. Printing and returning are different things, and mixing them up is the classic beginner bug.

Python 3 · editable

Expected output

Hello, Noah!
12
LOG: saved
None
4 16 10.75
Return a friendly greeting.

Returning several values

stats() returns three values separated by commas. Python packs them into a tuple, and the caller unpacks them into low, high and mean. This is the normal way to return more than one result.

Scope

Names assigned inside a function are local: they exist only while the function runs and do not affect variables with the same name outside. A function can read global names, but relying on that makes code harder to follow. Pass data in as arguments and return results instead.

Docstrings

A string literal on the first line of the body is the function's docstring. It documents what the function does, and Python stores it in greet.__doc__, where help(greet) and code editors can find it. Write one for every function that is not obvious from its name.

Common mistakes

  • Forgetting the parentheses: greet refers to the function; greet() calls it.
  • Printing inside a function when the caller needs the value. Return it instead.
  • Calling a function before the def line has run, which raises NameError.

Summary

  • def name(params): defines a function; calling it runs the body.
  • return hands back a value; without it the result is None.
  • return a, b returns a tuple that callers can unpack.
  • Variables created inside a function are local to it.

Quiz: Functions

1. What does a function return if it has no return statement?

Python returns None automatically.

2. In def area(w, h):, what are w and h?

Names in the definition are parameters; the values passed in a call are arguments.

3. What does print(len) show, compared with print(len("abc"))?

Without parentheses you refer to the function itself rather than calling it.

4. How does a function return two values?

Comma-separated return values are packed into a tuple.

Official docs

Written under the pen name Ethan Cole with AI-assisted drafting. Every code example on this page is executed by our test suite before publishing. Found a mistake? Tell us.