Defining Python Functions

Illustration showing Python function syntax with def, a function name, parentheses, and indented placeholder code.

When writing Python code, you will often find yourself repeating the same steps. Instead of writing them again and again, you can put those steps into a function. A function is a reusable block of code that has a name and can be run whenever you call it.

Function syntax

A basic function looks like this:

  • def is the keyword that tells Python you are defining a function.
  • function_name is the name of your function.
  • The brackets () follow the name. These are used for inputs (called parameters).
  • A colon : starts the function block.
  • Everything indented underneath is what the function does.

To use the function, you simply call it:

Naming conventions

  • Use lowercase letters.
  • Use underscores to separate words, e.g. calculate_total.
  • Pick names that explain what the function does.

Parameters and return values

Functions can take parameters to customise their behaviour:

They can also return values:

Docstrings: explaining your functions

A docstring is a short description written inside triple quotes at the start of a function. It tells others (and your future self) what the function is meant to do.

You can see the docstring by using the built-in help() function:

This is very useful when you come back to your code later or when you are working in a team.

Things to try yourself

  1. Write a function called say_hello that prints your name, and add a docstring.
  2. Create a function called square that returns the square of a number. Use help(square) to read its docstring.
  3. Write a function called is_even that returns True if a number is even. Document it with a docstring that explains the input and the output.

👉 Functions are like building blocks. With docstrings, they are not only reusable but also self-explaining, which makes your code easier to read, share, and maintain.

Main Topic

Python Functions

A flat, landscape-style digital illustration shows a man in an orange shirt drinking from a glass of water. A speech bubble next to him contains the function call drink_water() in bold monospaced text.

A beginner-friendly post explaining Python functions, highlighting clarity, reusability, collaboration, and scalability benefits.

Other Tutorials in this Topic

Two cartoon robots smiling as one passes a yellow box labelled “age” to the other.

Passing Arguments to Python Functions

A beginner-friendly Python tutorial explaining functions, arguments, keyword arguments, and default values, with examples, readability tips, and…

Three smiling robots on a blue background, each smaller than the last, passing a yellow box with speech bubble showing process().

Recursion in Python

Beginner-friendly Python guide introducing recursion, explaining base cases, benefits, and examples like countdown and string reversal, with…

A smiling blue robot opens a red toolbox against a pale green background, holding a silver spanner labelled “lambda” in black.

How to use python Lambda Functions

Beginner-friendly tutorial explaining Python lambda functions, covering syntax, simple examples, practical uses with map, filter, sorted, and…