Mini-Lab: Exploring Functions

 


Introduction

In this mini-lab you will gain practice with variables, and defining and using functions in Python.



Writing a function of your own

  1. In the Editor in Spyder (or whichever development environment you are using), create a new file for the functions you will write in this mini-lab.  Then save the new file with a name representative of this minilab. Be sure to add comments with your name and the date, and as well as a comment that describes this file.
     
  2. In your new file, write a new function that will ask the user for two numbers, and then prints out the average of those two numbers. Give the function a name that indicates its purpose (such as average), and add a comment above it that describes its purpose.
    Remember that the input function always returns the user's input as a string unless you convert it to another type.
  3. To test that your function works correctly, click on the Run button (the green arrow icon) at the top of the Editor.
    Stop and Think: What happened when you clicked the green arrow? (If nothing happened, do not fear; nothing should happen yet! Continue to the next exercise.)
  4. You need to call your function in order for it to actually do anything. In the iPython console call your function by typing its name, along with empty parentheses.
    Stop and Think: Did your program work correctly now? If not, what kinds of things did you have to fix, and why?
  5. You may also run your program by putting a call to your function at the bottom of your script and then clicking on the green arrow again. Try this.
    Stop and Think: What are the advantages and disadvantages of running your program in script mode vs interactive mode?
    Debugging Tip: When you call a function, be sure to use the name of the function (the name you gave it in the def line), not the name of the file in which you saved it. Those are two different things, and may have very different names.

Returning a value

The function you just wrote may be frequently handy, but we can add something to it to make it even more useful. As it stands now, you can use it to choose two numbers and display their average, but you would not be able to do anything further with that average once the function has completed its job. Outside the function you would have not have access to the values of either of the numbers, nor the average value. To make it more useful, we will have it return the average.

  1. In the Editor, edit your function by first commenting out the statement that print the average value, and then adding the following return statement as the last line of the function:
         return avg
        
    where avg is the variable that holds the average that you computed. (Or in place of the avg variable, you may have the actual computation of the average.) Make sure that the indentation is the same as for the other lines in the function. Edit the comment above the function to state that it returns the average it has computed.
     
  2. Test your function as before.
    Stop and Think: Did you get the same output? Why or why not? Will you be able to use the average value after your function has finished its job?
  3. Test your function again, but this time save the average value it returns in a variable and then print it out. For example,
         In[5]: average = computeAvg()
        
         In[6]: print("The average was: ", average)
        
    (This is referred to as "capturing" the return value in a variable.)
     
  4. What if you had picked a different variable name to store the average? Try calling your function and print one or two more times, using other variable names.
     

Parameters: Making a function more general

Let's generalize your function so that it will compute the average of any two numbers that get passed to it as a parameters and will return the average of those two numbers.

  1. For this exercise, make a copy of your function and give it a new name. We will make changes to this new function.

  2. Add two parameter names separated by commas between the parentheses in the first line of the function definition.  The names should indicate that the parameters represent numbers.  Have this new function print out the two numbers, along with the average of the two numbers, and then return the average.
     
  3. Test your new function. Remember that when you call your new function, you will need to pass two numbers to it. Try this two ways:

  4. Mad Libs: A Mad Lib is a fun word game, in which you construct a nonsensical short story -- or in our case, just a sentence or two -- by filling in random words in certain spots. See the Wikipedia article for more information.

    Write a new function that takes three words, a noun, a verb, and an adjective, as parameters and displays a short story constructed using the words passed in to the function. A sample session might look like the following:

    In[9]: madlib("monkey", "jump", "happy")
    
           When I was walking to work today I saw a monkey!  It was 
           very happy and it made me want to jump.
    
           OR
    
           It's your turn to jump over the happy monkey.
    

  5. Enhancements/Challenges: (For fun, not required) Think about how you might write another function that solicits the three words from the user. Then write a third function that uses these two functions, getting the input from one function and passing it into the other function. What other ways could you improve on your mad lib?

Submit