Writing Functions
A function is defined with the `def` keyword, followed by a name, a parenthesized parameter list, and a colon that opens the function's own indented block.
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.
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.
Reading the Parts
- Begin with def. This marks the start of the function definition.
- Choose a function name. The name identifies the function being defined.
- Write the parameter list inside parentheses. This is the place for the function's parameters.
- Add a colon after the parenthesized parameter list. The colon opens the function's own block.
- 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
Assembling a Simple Definition
Create a function definition named greet with one parameter named name and an indented body.
Start the definition: Place def first because the definition begins with the def keyword.
Add the name: Write greet after def so the function has an identifying name.
Add the parameter list: Place name inside parentheses to form the function's parameter list.
Open the block: Put a colon after the closing parenthesis.
Add the body: Place the function body in an indented block beneath the definition line.
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?
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
Leaving out one of the defining parts.
The source defines a function through that complete arrangement of parts.
Fix:
Check the definition from left to right: def, name, parameters in parentheses, colon, then the indented body.Treating the parameter list as the function body.
The parameter list and the function's own indented block are different parts of the definition.
Fix:
After the colon, look for the indented block that forms the body.Writing a separate def block for a function used only once when a one-place custom operation is appropriate.
The source identifies a lambda expression as an alternative for creating a function used in only that one place.
Fix:
Consider whether the one-place operation is better represented by a lambda expression.
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
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.
- Name the keyword that begins the definition.
- Identify the position of the function name.
- Describe where the parameters appear.
- State what the colon opens.
- 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.