Using Functions
1
Functions
Machines that take inputs and produce outputs.
Blocks of code encased inside of a function name
2
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.
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:
These parameters determine the how oven (function) behaves and what sort of output it produces.
4
Using Functions and Writing our Own
5
Built-In Functions
Python comes with a ton of functions "built-in" and we've already used some of them!
Turtle Functions:
How do we use them? By using "function calls"
print("Go Wildcats!")
How do we use them? By using "function calls"
print("Go Wildcats!")
Function name
How do we use them? By using "function calls"
print("Go Wildcats!")
Function Input
Function name
How do we use them? By using "function calls"
print("Go Wildcats!")
Function Input
Function name
What sorts of data can we give to functions?
print("Go Wildcats!")
a single string argument
print("Hello how are you doing", end="?\n")
print("Hello how are you doing", end="?\n")
print("Hello how are you doing", end="?\n")
Non-optional arguments go first
print("Hello how are you doing", end="?\n")
Additional arguments are separated by commas.
Non-optional arguments go first
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
How do we know what inputs a function takes?
There are many easy to find the "documentation" for a function:
print_examples.py
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.
Turtle Functions:
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.
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
How do we use them? By using "function calls"
answer = input("Fav number? ")
Some functions are reporters!
answer = input("Fav number? ")
Function name
Function Input
(this is the prompt)
Variable (to store the response)
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.
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.
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.