Lecture 4 Slides - Using Functions

1 of 24

Using Functions

1

2 of 24

Functions

Machines that take inputs and produce outputs.

Blocks of code encased inside of a function name

2

3 of 24

f(x) = x + 5

Python functions are just like mathematical functions - they're "machines" that take something as input and give you back some sort of output.

4 of 24

A function is like an oven. You put something in it and you get something (delicious) out.

But there's also lots of options or parameters you can tweak:

  • You can modify the temperature
  • You can set the cooking mode
  • You can change the amount of time it bakes for.

These parameters determine the how oven (function) behaves and what sort of output it produces.

4

5 of 24

Using Functions and Writing our Own

  • Today we'll be talking about using functions. By the end of these videos you should:
    • Understand what a function is
    • Explain what it means to call a function
    • Know how to call a function with its required parameters
    • Understand how to call a function with optional parameters

  • Next time, we'll learn how to design our own functions

5

6 of 24

Built-In Functions

Python comes with a ton of functions "built-in" and we've already used some of them!

  • print(...)
  • type(...)
  • int(...)
  • float(...)
  • str(...)

Turtle Functions:

  • forward(...)
  • left_turn(...)
  • right_turn(...)
  • change_pen_color(...)

7 of 24

How do we use them? By using "function calls"

print("Go Wildcats!")

8 of 24

How do we use them? By using "function calls"

print("Go Wildcats!")

Function name

9 of 24

How do we use them? By using "function calls"

print("Go Wildcats!")

Function Input

Function name

10 of 24

How do we use them? By using "function calls"

print("Go Wildcats!")

Function Input

Function name

11 of 24

What sorts of data can we give to functions?

  • We call the pieces of data that we pass to functions arguments.

  • This example has one argument that is of type string, but functions can be built to accept any number of arguments of any data type!
  • Some functions are also built with optional (or keyword) arguments, or spots for inputs that it can accept, but doesn't need in order to run.

print("Go Wildcats!")

a single string argument

12 of 24

print("Hello how are you doing", end="?\n")

13 of 24

print("Hello how are you doing", end="?\n")

14 of 24

print("Hello how are you doing", end="?\n")

Non-optional arguments go first

15 of 24

print("Hello how are you doing", end="?\n")

Additional arguments are separated by commas.

Non-optional arguments go first

16 of 24

print("Hello how are you doing", end="?\n")

Additional arguments are separated by commas.

Non-optional arguments go first and in a specific order

Optional or keyword

args go last and in any order

Function's name goes first

Sequence of arguments begins and ends with parentheses

17 of 24

How do we know what inputs a function takes?

There are many easy to find the "documentation" for a function:

  • The easiest way is to start typing the function in IDLE and see what hint it gives you
  • Another way is to use the help function on the function you're interested in
  • Finally, Google is your friend for learning how to use a function (There are many good Python "dictionaries" like W3Schools and the official Python documentation)

Example Link to W3 Schools

print_examples.py

18 of 24

Some Functions are "Reporters"

Reporter functions return or report back a particular value.

Other functions cause some other outcome like moving a Turtle or printing output into our interpreter window.

  • print(...)
  • type(...)
  • int(...)
  • float(...)
  • str(...)
  • input(...)

Turtle Functions:

  • forward(...)
  • left_turn(...)
  • right_turn(...)
  • change_pen_color(...)

19 of 24

Asking for Information: input()

The built-in input function is useful if you need your program to ask your user for information. input prompts the user for some data, and reports back (returns) what the user typed as a string, that can be stored in a variable.

  • The data you give to the input function specifies the prompt that will be displayed to the user.

We describe functions using what's called a type signature. It specifies the what types of data function takes as input, and what sort of data a function takes as output.

input: str -> str

# input is a function that accepts a string and returns a

# string with the user's input

19

20 of 24

How do we use them? By using "function calls"

answer = input("Fav number? ")

21 of 24

Some functions are reporters!

answer = input("Fav number? ")

Function name

Function Input

(this is the prompt)

Variable (to store the response)

22 of 24

Practice 1. Exploring Built-In Functions

Write a program that asks the user to input 3 different numbers and then prints out the maximum of the 3 numbers using the built-in max function.

Step 1. Use the input function to get 3 different inputs from the user. Store each in a variable.

Step 2. Remember, input gives us STRINGS not numbers. So we need to convert (or cast) the old variable to a number.

Step 3. Use the three converted variables as input to the max function and print the result.

23 of 24

Practice 2. Exploring Built-In Functions

Write a program that asks the user to input 3 different strings and then prints out the minimum LENGTH of the 3 strings using the built-in min function and len function.

24 of 24

Practice 3. Challenge Problem

Write a program that serves as a tip calculator. It will take in two inputs, the bill total as well as the tip percentage, and then prints two things: the tip amount and the total bill.