CS 107: Pictures and Sounds: Programming with Multimedia

Kalamazoo College

Spring 2008

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 your text, the first function you saw was to pick and show a picture. (See Recipe 1 on page 29 of your text). 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 book.
    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:
    # Recipe 1 from p. 29: 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 pickAndShow.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 a new line to the end of it:
         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.
     
  7. Print your modified function using the Print option on the File menu.

Writing a function of your own

  1. Create a second file for the other functions you will write in this mini-lab.  First, choose New from the File menu, so that you don’t overwrite your previous file.  In the program area, write comments with a description of the file, your name, and the date.
     
  2. In the program area, write a new function that will print "Hello, yourname" (where yourname should actually be your name!) . Give the function a name that indicates its purpose (such as printGreeting), and add a comment above it that describes its purpose.
     
  3. Click on the Load button.  Since this is a new file, JES will once again ask you to choose where to save it when it prompts you to save your work. Choose a name that indicates what will be in this file (eventually you will have several greeting functions), and remember to put a .py on the end. For example, you might call it greetings.py.
     
  4. 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

You have written a nice, friendly function, but it is only friendly to you.  Now let's generalize it to be friendly to anyone.

  1. In the program area, add a second function with a new name, such as greetAnyone or printGreetingTo.  (You can copy and paste your first greeting 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 person's name.  Have the new function print "Hello, parameterName" (where parameterName should be the actual name of the parameter to your function). Add a comment above the function that describes its purpose.
     
  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 name; for example,
    printGreetingTo("Chris")
    Debugging Tip: Did the function print "Hello, parameterName" rather than "Hello, Chris" or "Hello, AnotherName"?  If the parameter name is within the quotation marks in your print statement, then your function will print the parameter name rather than the value passed in as a parameter.  Remember that you can use the plus sign (+) to join two strings.  (The parameter value is a string.)

    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.


Multiple parameters

You can generalize your function a little more to give it an international flavor.

  1. Edit your parameterized function (or create a new version of the function) to take two parameters: one representing the greeting to use, and one representing the name.  The print statement should join the two parameters together, with a comma and space in between (the comma and space must be in quotation marks).
     
  2. Load the file and call your function in the command area to test it. Remember to pass it a greeting and a name; for example,
    greet("Bonjour", "Alyce")      prints      Bonjour, Alyce
    greet("Guten Tag", "Pam")      prints      Guten Tag, Pam
  3. When you are done, print the functions in this file using the Print option on the File menu. Hand in both this printout and your previous printout of the pickAndShow function.

If you have time: Parameters that are not constants

In the previous exercises, you passed constant string values to functions as parameters. It is also possible to use variables or the values returned from function calls as parameters to functions.  You can only use a function return value as the parameter to another function, though, if the parameter function has a return statement.

  1. Type in the showNamed function on p. 33 of the textbook and load it.
     
  2. Passing a variable as a parameter: In the command area, create a variable name representing the file name returned by a call to pickAFile. Then call the showNamed function, passing it your new variable.
     
  3. Using a function return value as a parameter: In the command area, experiment with the following variation on the "pick and show" functionality you implemented earlier:
    show(makePicture(pickAFile()))
    Analysis Questions: What is the value being passed as a parameter to makePicture? What is being passed as a parameter to show?
  4. You do not need to hand in any results from this exercise.