Skip to ContentGo to accessibility page

6.1 Defining functions

Learning objectives

By the end of this section you should be able to

  • Identify function calls in a program.
  • Define a parameterless function that outputs strings.
  • Describe benefits of using functions.

Calling a function

Throughout the book, functions have been called to perform tasks. Ex: print() prints values, and sqrt() calculates the square root. A function is a named, reusable block of code that performs a task when called.

Checkpoint

Example: Simple math program

Example: Simple math program; ch 6, video 1

Concepts in Practice

Identifying function calls

1.
Which line has a function call?
1
input_num = 14
2
offset_num = input_num - 10
3
print(offset_num)
  1. line 1
  2. line 2
  3. line 3
2.
How many times is print() called?
print("Please log in")
username = input("Username:")
password = input("Password:")
print("Login successful")
print("Welcome,", username)
  1. 1
  2. 3
  3. 5
3.
How many function calls are there?
# Use float() to convert input for area calculation
width = float(input("Enter width:"))
height = float(input("Enter height:"))
print("Area is", width*height)
  1. 3
  2. 5
  3. 6

Defining a function

A function is defined using the def keyword. The first line contains def followed by the function name (in snake case), parentheses (with any parameters—discussed later), and a colon. The indented body begins with a documentation string describing the function's task and contains the function statements. A function must be defined before the function is called.

Checkpoint

Example: Welcome message function

Example: Welcome message function; ch 6, video 2

Concepts in Practice

Defining functions

4.
What's wrong with the first line of the function definition?
def water_plant:
  1. A docstring should go after the colon.
  2. Parentheses should go after water_plant.
  3. def should be define before water_plant.
5.
What is the output?
def print_phone_num():
  print("Phone: (", 864, ")", 555, "-", 0199)

print("User info:")
print_phone_num()
  1. Phone: ( 864 ) 555 - 1000
    User info:
    Phone: ( 864 ) 555 - 1000
  2. User info:
  3. User info:
    Phone: ( 864 ) 555 - 1000
6.
Which statement calls a function named print_pc_specs()?
  1. print_pc_specs
  2. print_pc_specs()
  3. print_pc_specs():
7.
Which is an appropriate name for a function that calculates a user's taxes?
  1. calc_tax
  2. calculate user tax
  3. c_t

Benefits of functions

A function promotes modularity by putting code statements related to a single task in a separate group. The body of a function can be executed repeatedly with multiple function calls, so a function promotes reusability. Modular, reusable code is easier to modify and is shareable among programmers to avoid reinventing the wheel.

Checkpoint

Improving a program with a function

Improving a program with a function; ch 6, video 3

Concepts in Practice

Improving programs with functions

Consider the code above.

8.
If the points were changed from floats to integers, how many statements would need to be changed in the original and revised programs respectively?
  1. 3, 1
  2. 4, 4
  3. 12, 4
9.
How many times can calc_distance() be called?
  1. 1
  2. 3
  3. many

Try It

Cinema concession stand

Write a function, concessions(), that prints the food and drink options at a cinema.

Given:

    concessions()

The output is:

    Food/Drink Options:
    Popcorn: $8-10
    Candy: $3-5
    Soft drink: $5-7

Try It

Terms and conditions prompt

Write a function, terms(), that asks the user to accept the terms and conditions, reads in Y/N, and outputs a response. In the main program, read in the number of users and call terms() for each user.

Given inputs 1 and "Y", the output is:

    Do you accept the terms and conditions?
    Y
    Thank you for accepting.

Given inputs 2, "N", and "Y", the output is:

    Do you accept the terms and conditions?
    N
    Have a good day.
    Do you accept the terms and conditions?
    Y
    Thank you for accepting.
Citation/Attribution
Reuse and redistribution of this content in digital or print format:
  • This book may not be used in the training of large language models or otherwise be ingested into large language models or generative AI offerings without OpenStax's prior written permission.
  • This book uses the Creative Commons Attribution-NonCommercial-ShareAlike License, which means that you can reuse and modify the material only for noncommercial purposes, must attribute OpenStax, and must distribute any derivative works under the same license.
  • Any commercial printing of this textbook, including using a local or custom printer, must be approved by OpenStax, and proper citation provided.
  • OpenStax-copyrighted images, activities, assessments, and similar components of this book are subject to the same licensing – CC-BY-NC-SA. They can be used for noncommercial purposes with attribution. Commercial use requires permission.
  • Permission requests: Anyone who intends to incorporate this content (including text, images, and other components) into large language models, use it in AI offerings, use it commercially (including in print), and/or has questions about another use case is welcome to complete our reuse request form.
Attribution information
  • If you are redistributing all or part of this book in a noncommercial print format, then you must include on every physical page the following attribution:

    Access for free at https://openstax.org/books/introduction-python-programming/pages/1-introduction

  • If you are redistributing all or part of this book in a noncommercial digital format, then for every page that includes OpenStax content, you must license the derivative work under the same CC-BY-NC-SA license as the original, and include on every digital page view the following attribution:

    Access for free at https://openstax.org/books/introduction-python-programming/pages/1-introduction

Citation information

The information below includes the information needed to generate citations in most major styles (APA, MLA, etc.); you must reformat and organize the information as needed to fit the requirements of the style. Use the information below to generate a citation. We recommend using a citation tool such as this one.

© Apr 23, 2026 OpenStax. Textbook content produced by OpenStax is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike License. The OpenStax name, OpenStax logo, OpenStax book covers, OpenStax CNX name, and OpenStax CNX logo, and Rice University name, and Rice University logo trademarks, or wordmarks are not subject to the Creative Commons license and may not be reproduced without the prior and express written consent of Rice University.

This book utilizes the OpenStax Python Code Runner. The code runner is developed by Wiley and is All Rights Reserved.