Skip to main content
Library homepage
 

Text Color

Text Size

 

Margin Size

 

Font Type

Enable Dyslexic Font
Engineering LibreTexts

6.1: Defining Functions

( \newcommand{\kernel}{\mathrm{null}\,}\)

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.
  • Parentheses should go after water_plant.
  • 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
  • 4, 4
  • 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.

    This page titled 6.1: Defining Functions is shared under a CC BY 4.0 license and was authored, remixed, and/or curated by OpenStax via source content that was edited to the style and standards of the LibreTexts platform.

    Support Center

    How can we help?