Concepts / Writing Functions

Writing Functions

The most important visual boundary is the colon followed by the indented block. That block is the function's own body.

  • CORE CONCEPT
  • Programming

Why Give Work a Name

Writing a function means giving a piece of work a defined structure and a name. Instead of treating the work as an unnamed collection of instructions, you create a function definition with a def keyword, a name, a parenthesized parameter list, a colon, and an indented block belonging to that function.

Key point

The most important visual boundary is the colon followed by the indented block. That block is the function's own body.

The Definition Shape

A function definition follows a fixed order. It begins with def, continues with the function name, places parameters inside parentheses, and ends that opening line with a colon. The indented block that follows is the function body.

defstarts the definitionfunction nameidentifies the function(parameters)parenthesized list:opens the blockindented blockfunction bodyfollowed byfollowed byfollowed byopens
How do the def keyword, function name, parameter list, colon, and indented block fit together, and where does the function body begin?

Reading the Parts

  1. 1
    Begin with def. This marks the start of the function definition.
  2. 2
    Choose a function name. The name identifies the function being defined.
  3. 3
    Write the parameter list inside parentheses. This is the place for the function's parameters.
  4. 4
    Add a colon after the parenthesized parameter list. The colon opens the function's own block.
  5. 5
    Write the function body as an indented block beneath the opening line.

The structure is sequential: each part prepares the reader for the next part. The first line announces a definition, the middle identifies it and describes its parameters, and the colon signals that the function's own indented block is about to begin.

Building One on Paper

Worked example

Assembling a Simple Definition

Create a function definition named greet with one parameter named name and an indented body.

  1. 1
    Start the definition

    Place def first because the definition begins with the def keyword.

  2. 2
    Add the name

    Write greet after def so the function has an identifying name.

  3. 3
    Add the parameter list

    Place name inside parentheses to form the function's parameter list.

  4. 4
    Open the block

    Put a colon after the closing parenthesis.

  5. 5
    Add the body

    Place the function body in an indented block beneath the definition line.

Result

The completed structure has the order def, function name, parenthesized parameter list, colon, and indented function body.

What do you think happens?

Before checking the structure, which part tells you where the function body begins?

  • The function name

  • The parameter list

  • The colon followed by the indented block

  • The def keyword alone

Reveal answer
Answer:

The colon followed by the indented block

The source describes the colon as opening the function's own indented block. That indented block is the function body.

Checks Before Moving On

  • Common mistake

    Leaving out one of the defining parts.

    What it looks like

    A proposed definition has a name and parameter list but does not follow the described def, name, parenthesized parameter list, colon, and indented block structure.

    Why it fails

    The source defines a function through that complete arrangement of parts.

    Do this instead

    Check the definition from left to right: def, name, parameters in parentheses, colon, then the indented body.

  • Common mistake

    Treating the parameter list as the function body.

    What it looks like

    The parameters appear in parentheses, but no separate indented block follows the colon.

    Why it fails

    The parameter list and the function's own indented block are different parts of the definition.

    Do this instead

    After the colon, look for the indented block that forms the body.

  • Common mistake

    Writing a separate def block for a function used only once when a one-place custom operation is appropriate.

    What it looks like

    A custom sort operation is needed in only one location.

    Why it fails

    The source identifies a lambda expression as an alternative for creating a function used in only that one place.

    Do this instead

    Consider whether the one-place operation is better represented by a lambda expression.

Best practice

Before writing a longer function, first verify its outer shape. Confirm that the definition starts correctly, that the parameter list is parenthesized, that the colon is present, and that the body is visibly an indented block.

Functions also appear in modules. The source describes a simple module as a file with a .py extension that contains functions and variables. This connects function writing to file organization: a function definition can be part of a larger Python file rather than existing only as an isolated exercise.

Practice the Shape

EASY

Describe the five parts you would include when writing a function definition named calculate with two parameters. Do not write the function body itself; explain where that body belongs.

Hints
  • Begin with the keyword that starts a function definition.
  • Place the two parameters in the parenthesized parameter list.
  • Remember that the colon opens the function's own indented block.
  1. 1
    Name the keyword that begins the definition.
  2. 2
    Identify the position of the function name.
  3. 3
    Describe where the parameters appear.
  4. 4
    State what the colon opens.
  5. 5
    Explain where the function body begins.

Key Takeaways

  • A function definition starts with def and a function name.
  • The parameter list is enclosed in parentheses.
  • A colon follows the parameter list and opens the function's own indented block.
  • The indented block is the function body.
  • A lambda expression can be used for a custom function needed in only one place, such as a one-place custom sort operation.

Related concepts

More concepts starting with W