Terminology
- Defining a function means that you are making your own tool. You make up a name for the function. The name is like a variable, but it has parentheses after it, with or without parameters.
- Parameters are the variables that you specify for your function, for example the length of a side of a square. Some functions won't have any parameters.
- Return tells what the function gives back to you. Some functions won't return anything.
- Calling a function happens when you use it somewhere in your program.
statements and expressions you’ll use
| def |
defines a new function |
| return |
used inside a function definition at the end if there is something to be returned to the function |
Try
In my program, anytime I want to give credit to Mark Lutz, I would type:
credits()
Function Notes
- Functions involve stand alone definitions AND calls to the function within a program. The definition must come before the call.
- A function is defined using def, the name you choose, and () with or without parameters for input.
- The function may or may not end with a return statement.
- Variables that you define as parameters for your function don’t have to have the same name as the parameters that you send to the function. Variables in our examples are matched by position.
I define a function
I use the function in my program. Notice how the variable, member, is used in the call and is passed to the variable, person, in the function definition.
A function for our Pypet
Here is a PyPet happy expressions function. What does it do?
Now we use it in a program.
Challenge: Personalize the express_happy function
Modify the express_happy function to take the parameter, owner.
incorporate owner into one of the expressions.
Try calling the function.
Raise your hand for a check when you get to this point.
Here is a definition for a function that computes tips. It takes the parameters of percentage and total and returns the amount to tip. By itself, it does nothing. Put it in a file.
Now call the function.
We still don't see anything. So print the returned tip like this:
Here is a program that uses our tip function to compute a tip when the user inputs bill and percentage. Try it. Make sure the tip function is still in your file above this part. Function definitions always come before function calls.
The whole point of functions are that they are multi-purpose. Here I use the function in a different program that simulates a wait staff tip for an average evening with 8 customers.
Add print statements to print some of the variables along the way and convince yourself that the program is working.
Challenge: Write a program that totals a restaurant bill and suggests tips
Start by setting a variable called total equal to zero.
Prompt the restaurant worker, asking if she/he wants to enter a food price. Store the Y/N answer in a variable called answer.
Start a while loop with a test of answer == "Y".
This means that the worker wants to enter a price, so ask him/her for that price and add it to the total.
Repeat the first prompt, asking if she/he wants to enter a price. This is in the while loop, so it will repeat as long as the answer is Y.
If the user answers N, you'll need to break out of the while loop. Use an if statement here.
After the while loop, you will have your total and can compute tips. Call your tip function on the total for 15 percent and 20 percent.
Then suggest these as possible tips.
Raise your hand for a check when you get to this point.