Mini-Lab: Exploring Functions

 


Introduction

In this mini-lab you will gain practice with variables, defining and using functions in Python, and the mechanism for saving and loading files in JES.



Entering and running a Python function in JES

  1. Start up JES. In the course notes, you saw a function to pick and show a picture. Type this function into the program area (the upper area) of JES. Indentation and capitalization are very important in Python -- in this case, indentation indicates that the statements are part of the function -- so be sure to indent and capitalize (or not) just as in the notes.
    Analysis Questions: Does this function have any variables?  If so, how many?  Does it call any other functions?
  2. Comments are a critical part of programming.  Functions should always include comments indicating the purpose of a function.  In addition, every file should include information about who created the file, when, and what purpose the functions in the file serve.  Add a set of comments above your function similar to the following:
    # Mini-Lab: Exploring Functions 
    # Your Name
    # Today's Date
    Then leave a blank line and add a comment immediately above your function definition similar to the following:
    # This function will pick a picture file and show it
  3. Before you can use this function (or any other you have defined), you must load it into the command area by clicking on the Load button.  JES automatically prompts you to save the contents of the program area to a file when you load; click OK to do so.  In this case, it does not know what file to save to, so a file chooser window will appear.  Navigate to CS107Labs folder and give the file a name that ends in “.py” to indicate a Python file. For example, you could use functionMiniLab.py. The file will then automatically load.
     
  4. To run the function, type pickAndShow() in the command area and hit enter. What happened? Was it what you expected?
    Debugging Tip: If the function does not work correctly, check that you remembered the colon at the end of the first line of the function definition and that you spelled all of your variable names and function names correctly.  After making any corrections, remember to click on the Load button before calling your function again.

Returning a value

The pickAndShow function will frequently be handy in this class, but we can add something to it to make it even more useful. As it stands now, you can use it to choose a file and display it, but you would not be able to do anything further with the picture once the pickAndShow function has completed its job. Outside the function you would have access to neither myFile, the chosen filename, nor myPict, the picture object. To make it more useful, we will have it return the picture.

  1. In the program area, edit pickAndShow and add the following return statement as the last line of the function:
         return myPict
        
    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 picture it has displayed.
     
  2. Click on the Load button.  This time JES will prompt you to save your work, but without asking you to choose where to save it. It will automatically save it to the same file as last time.
     
  3. Call pickAndShow again in the command area. It should work just as it did last time.
     
  4. Next, test the new functionality. In the command area, call pickAndShow again, but this time save the picture it returns in a variable. For example,
         >>>picture = pickAndShow()
        
    (This is referred to as "capturing" the return value in a variable.)
     
  5. Close the window with the picture in it, if you haven't done so already, and show it from the command area:
         >>>show(picture)
        
  6. What if you had picked a different variable name to store the pictures? Try calling pickAndShow and show one or two more times, using other variable names instead of picture.
     

Writing a function of your own

  1. In the program area, write a new function that will pick a sound, play it, and then return it. Give the function a name that indicates its purpose (such as pickAndPlay), and add a comment above it that describes its purpose.
     
  2. Click on the Load button.
     
  3. Call your function in the command area to test it.
    Debugging Tip: When you call a function in the command area, 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.
    Debugging Tip: A common error in JES is to forget to click on the Load button before calling a function.

Parameters: Making a function more general

Let's generalize the pickAndShow function so that it will show any picture that gets passed to it as a parameter and will print the dimensions of that picture, and the full path of the file that was used to make that picture. (See Wednesday's minilab if you forgot how to do this.)

  1. In the program area, add a new function with a new name, such as showAndPrint.  (You can copy and paste your first pickAndShow function as a starting point.)  Add a parameter name between the parentheses in the first line of the function definition.  The name should indicate that the parameter represents a picture.  Have the new function show the picture that is represented by the parameter and then print the full path and dimensions of that picture. (Remember the print command from Wednesday's minilab?)
     
  2. Click on the Load button.  Did JES ask you where to save your work? Why, or why not?
     
  3. Call your function in the command area to test it. Remember to pass it a picture; for example,
    showAndPrint(myPict)
    Debugging Tip: Did calling the function give you an error and tell you Name not found globally? rather than showing the picture? If you have not defined myPict as a picture object in the command area yet, the function will not recognize the value of myPict. (Refer back to Wednesday's minilab if you have forgotten how to do this.)

    Debugging Tip: Remember to click on the Load button anytime you make a change to your program definition.

    Shortcut: You can use the up arrow in the command area to go back to the previous command and then hit enter to run that command.

    Analysis Questions: How many times do you see the full path name and dimensions of the picture printed? Why does this happen?


Multiple parameters

Let's make a function that shows a picture and plays a sound.

  1. Create a new function that takes two parameters: one representing a picture, and one representing a sound.  This function should show the picture that gets passed to the parameter and should play the sound that gets passed as a parameter.
     
  2. Load the file and call your function in the command area to test it. Remember to pass it a picture and a sound; for example,
    showAndPlay(myPict, mySound)
    will show the picture represented by the myPict variable and will play the sound represented by the mySound variable.
     
  3. When you are done, submit this file via Moodle.