Unit 1: Programming Basics

Lesson 2: Writing Useful Functions

Notebook Authors

(fill in your two names here)

Team Roles

Facilitator: (fill in name)
Spokesperson: (fill in name)
Process Analyst: (fill in name)
Quality Control: (fill in name)

If there are only three people in your team, have one person serve as both spokesperson and process analyst for the rest of this activity.

At the end of this Lesson, you will be asked to record how long each Model required for your team. The Facilitator should keep track of time for your team.

Computational Focus: Writing Useful Functions

Model 1: The Math Module

In Python’s math module, you will find a number of useful predefined functions. Table 1 at the end of this lesson lists both constants and several useful functions defined in the math module. This same information is also accessible using Python’s built-in help function.

Type (don't copy and paste) the following code, one line per Jupyter cell, to observe the output (if any):

value = math.cos(0)  
import math  
value = math.cos(0)  
value  
math.cos(value)  
math.pi  
math.cos(math.pi)  
help(math) # please clear the output of this cell after running 
help(math.cos)    
math.cos()

(A hint about help in Jupyter Notebooks, you can also use a ? before a module or function and get some info about it. For example, try ?math.cos for fun. You get slightly less info sometimes and this is a JN thing not a general Python thing.)

Critical Thinking Questions

1. Examine the two lines (of the ten above lines of Python code) that had errors in them.
1a. Explain how to fix the first error.

1b. Explain how to fix the second error.

2. Identify the assignment statement using the math.cos function (from the ten Python lines).
2a. What is the identifier in the assignment statement?

2b. What is the argument in the assignment statement?

3. What are the assumed units (degrees or radians) of the argument passed to the math.cos function?

4. Based on the first two lines of Python code from Model 1, describe two ways using a function defined in the math module differs from using one of Python’s built-in functions like type() or abs() from the last lesson.

Model 2: User Defined Functions

In addition to using Python’s built-in functions, you can write your own functions. Type the following function definition in a single Jupyter code cell (don't copy and paste since it's bad practice, and so you can see how Jupyter automatically indents, the spacing is important!):

def model_two():  
    import math  
    answer = math.cos(0)  
    print(answer)

To execute the code (known as a function call), type the function name and parentheses in another Jupyter code cell:

model_two()

Critical Thinking Questions

5. Modify the model_two function definition so that it uses the input function introduced in the last lesson to ask the user to provide the value of the argument of the math.cos function. Place a comment in your code that briefly explains what you did.


In [ ]:

Model 3: Passing Arguments to Functions

We can write a more flexible function by defining a function that takes a parameter. Define the following function in a single Jupyter code cell (again, don't copy and paste):

# This function takes a parameter 

def model_three(angle):  
    import math  
    print(angle)
    answer = math.cos(angle)  
    print(answer)

Note: A single hash sign (#) indicates the beginning of a comment used for program documentation. All text after the # to the end of the line is ignored. It is good practice to communicate to other programmers (or remind your future self!) what is happening in the code.

When we call the model function, we need to pass the information the function expects (the parameter) as the argument in the function call. The function parameter in the function definition automatically receives the value of the argument in the function call.

To see three examples of this, type in the following statements in four Jupyter code cells:

model_three(5)
model_three(float(input("enter a number: ")))
dozen = 12
model_three(dozen)

In [ ]:

Critical Thinking Questions

6. Consider the first model_three function call.
6a. What is the first function call from the above model?

6b. What is the value of the argument from the first function call?

6c. What is the name of the parameter (from the first line of the function definition)?

6d. What is the value of the parameter?

6e. Write an assignment statement that demonstrates how the parameter directly got its value.

7. Consider the last model_three function call.
7a. What is the value of the argument?

7b. What is the value of the parameter?

7c. When a variable is passed to the function as an argument, does the identifier of the argument need to be the same as the identifier taken by the function as a parameter?

7d. Write an assignment statement showing how the parameter indirectly got its value. Briefly explain your answer.

Model 4: Return Values for Functions

We can write a function that produced an output by using return. Define the following function in a single Jupyter code cell (again, don't copy and paste):

# This function takes a parameter and returns a value

def model_four(angle):
    import math
    print(angle)
    answer1 = math.cos(angle)
    answer2 = math.cos(angle*angle)
    print(answer1, answer2)
    return answer2

</pre>

To see three examples of this, type in the following statements in six separate Jupyter code cells:

model_four(5)  
answer1
answer2
result = model_four(5)
result
result/100

Critical Thinking Questions

8a. When you run the 4th line of code above [result = model_four(5)], does result get its value from answer1 or answer2?

8b. Explain how result gets its value and what happens to answer1.

9. Would it be possible to perform an operation on the variable answer2 (e.g., answer2/100) after calling the function (in a separate code cell)? Explain.

10. Describe how it is possible to perform an operation involving the value of answer2 and not possible to perform an operation involving the value of answer1 after the function call.

11. Copy into a new code cell, edit, and run the model_four definition with the last two lines reversed (return then print). Describe the result. What does this imply about the return statement?

12. Explain why a function that returns the value of a variable is considered more “useful” than a function that simply prints the value of a variable.

Temporal Analysis Report

How much time did it require for your team to complete each Model?

END OF CLASS

Table 1: partial list of math module functions and constants (complete list)

functions functions constants
sin(x) log(x) e
cos(x) log10(x) pi
tan(x) exp(x)
asin(x) ceil(x)
acos(x) floor(x)
atan(x)