Mini-Lab: Exploring Functions

 


Introduction

In this mini-lab you will practice declaring variables, defining, and using functions in Python. You will explore source code management by developing a main function which will be the driver of your code. At the end of this mini-lab you'll create a source file to keep your functions in. You'll then import these functions the same way you imported jes4py.



Entering and running a Python function

  1. Launch Visual Code and create a new project folder. Name this folder Lab1_Exploring_Functions. In this folder create a python file titled main.py. As you did in the previous mini-lab, import jes4py (i.e., add the statement from jes4py import * at the top of your file). Next, add the following code to your main.py file.
    if __name__ == '__main__':
    print("Hello World!")
  2. Detail the behavior you see.
  3. Now let's add a function to your program. Near the end of the reading on functions, there was a function to pick and show a picture. Add a blank line after your import statement, and then type this function into your file. NOTE: 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 reading.
  4. Now replace print("Hello World!") in the driver portion of your program with this function. It should look like this:
    if __name__ == '__main__':
    pickAndShow()
  5. Now run your program. (Remember, to see the picture, you can do one of two things: 1) add a statement from time import * at the top of your file, and then the statement time.sleep(5) after your the show statement; or 2) Go to the terminal and do a Command-Click on the filename.)

    What happened when you ran the function? 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 run your program again.

    Analysis Questions: Does this new function have any variables?  If so, how many?  Does it call any other functions?
    NOTE: You should include the answers to analysis questions from this mini-lab and any other mini-lab as comments (see next exercise) in your program file.
  6. Finally, move the function pickAndShow below the main driver. What happens when you run the code? Once you've documented your response, undo this change.

  7. 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. Recall from the syllabus the authorship information. This information is required for all assignments. Before the import(s), insert the following:
    # Mini-Lab: Exploring Functions
    # Author: "Your Name"
    # Date: Today's Date
    # Group or partner (If applicable)
    # With Assistance From: "Give credit to the people who aided you."
    
    For each function, add a comment immediately above the definition similar to the following:
    # This function will pick a picture file and show it

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. Test this out by adding the following lines to your main and document the behavior.

  show(myPict)
  newPict = makePicture(myFile)
  show(newPict)
Outside the function you would have access to neither myFile, the chosen filename, nor myPict, the picture object. This is because the scope of these variables is limited to the function. To make it more useful, we will have it return the picture.

  1. Edit the pickAndShow function by adding 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. Next, test the new functionality. In the main, 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.)
     
  3. Run your program again. Do you see anything different happening? (Hint: You shouldn't see any different behavior.)
  4. In the main, we can now do more things with the picture. Add a line of code in the main to either print or show the picture that was returned from the pickAndShow functon. Run your program.
  5. 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. After the pickAndShow function, 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. Add a statement in main to call this new function. Run your program to test it.

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. Add a new function, 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 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. Call your function in main to test it. Remember to pass it a picture; for example,
    showAndPrint(myPict)
    Debugging Tip: Did calling the function give you a NameError error rather than showing the picture? If you have not defined myPict as a picture object in main yet, the function will not recognize the value of myPict. (Refer back to Wednesday's minilab if you have forgotten how to do this.)

    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. Call your function in main 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 and the discussion questions via Kit.