Skip to ContentGo to accessibility pageKeyboard shortcuts menu
OpenStax Logo

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

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

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

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

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 permission.

Want to cite, share, or modify this book? This book uses the Creative Commons Attribution License and you must attribute OpenStax.

Attribution information
  • If you are redistributing all or part of this book in a 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 digital format, then you must 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

© Jul 30, 2024 OpenStax. Textbook content produced by OpenStax is licensed under a Creative Commons Attribution License . The OpenStax name, OpenStax logo, OpenStax book covers, OpenStax CNX name, and OpenStax CNX logo 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.