Title: What is a function?

Topic: Functions

Difficulty:

Required Skills: Defining functions, converting data types, math operations

Language: Python

This activity focuses on helping students to gain a deeper understanding of what functions are and how we use them. I believe that many students view functions as things that are only used and discussed in math class. Having a more balanced and well-rounded perspective on what they are and how they can be used may help them to understand the underlying concepts.

What are functions? In your own words, how would you define a function?

Try writing a program that will print a greeting to your teacher, like "Hello Mr. Wheeler".

print("Hello Mr. Wheeler")

Is this a function? Why or why not? Discuss.

Try writing a program that will allow you to write the same greeting but to any person that you want. For example, it could say "Hello Greg" or "Hello Betty".

def greeting_maker(name):
   
print("Hello " + name)

Teaching Note: If you only run this code, what happens? Should be nothing. We have only defined the function. We still need to actually call the function (which is done below with the input of "Bob"). This can be related back to function notation. For example, f(x) = 2x+5 does not tell us the value of the function when x = 4. We need to actually compute f(4) = 2(4)+5 = 13.

greeting_maker("Bob")

Is this a function? Why or why not? Explain.

What happens if we pass in an input of 42 in greeting_maker? Can you connect this to what you know about functions such as 1/x and x^(0.5)?

greeting_maker(42)

What kind of output do you get from our greeting function? Can you also connect this to what you know about functions?

Teaching Note: Looking for students to connect the types of input and outputs that are allowed in our greeting_maker function to domain and range.

Write a function of your own that has at least 2 inputs. Be creative with your function.

Many possible examples:

def greeting_makerV2(name, time_of_day):
   
print("Good " + time_of_day + " " + name + "!")

greeting_makerV2(
"Willy Wonka", "Morning")
greeting_makerV2(
"Willy Wonka", "Morning")

Pretend that you are driving at 60 km/h. Write a function that will find the distance your have travelled based on time (in minutes).

def distance_driven(minutes):
   hours
= minutes/60
   
print(60 * hours)

distance_driven(
120)

Teaching Note: It might be worth discussing the differences between the function above where the result is printed compared to the function below where the result is returned. It will be important to understand if students when students try to print out the ordered pairs in the next part of this activity.

If we print the value within the function it makes it difficult to actually use the resulting value in other functions of calculations.

For example, try this:

 print("I drove " + str(distance_driven(60)) + " km.")

You can see that 60 is printed (this is from the function being called) but it isn’t actually returned so it shows as None in the printed message.

I’ve used the example with students of telling them to complete their homework for tomorrow. Then in class the next day when I ask them to show me their homework they reply with “I did it in my head.” In my instructions I never told them that they needed to write down their answers so that they can be used/looked at later. This is the same with printing the result and not returning it. When we tell the computer to print the result we are telling it to just print it directly on the screen. When we say return, we are telling the computer to give/return the result to wherever is it is being called/asked for.


def distance_drivenV2(minutes):
   hours
= minutes/60
   
return (60 * hours)

print(distance_drivenV2(90))

We can say that distance is a function of time. This means that the distance depends on time.

Now write 3 linear functions. Name them y, f and h (is this more familiar to what you have seen in math class?).

def y(x):
   
return x+2

def f(x):
   
return x**2 + 4

def h(x):
   
return 2*x + 3

print(y(2))
print(f(2))
print(h(2))

Now see if you can try to figure out how to print a list of ordered pairs for these functions.

for x in range(10):
   
print("(" + str(x) + ", " + str(f(x)) + ")")

CHALLENGE - Write a quadratic equation solver

def quadratic_solver(a,b,c):
   x_1
= (-b + (b**2-4*a*c)**0.5) / 2*a
   x_2
= (-b - (b**2-4*a*c)**0.5) / 2*a
   
return(x_1, x_2)

print(quadratic_solver(1,-2,-4))

Teaching Note: This is a very basic version. Are there any values of a, b and c that will break our solver? What if we enter values that have no real solution? Can we extend our function to be able to handle additional inputs and provide appropriate outputs?