Notes on Functions

Harold Nelson

9/14/2020

Basic Builtin Functions

There are many functions available to you as soon as you start python. You have used the following and perhaps others:

These all have the fundamental property that mathematicians require a function. If you put in the same input, the output of the function will be the same every time.

Math. functions.

There are many common mathematical functions which are not immediately available but can be obtained easily.

The function which takes square roots is one of these. To use this function you must do two things.

  1. import the math module.
  2. Refer to it as math.sqrt()

Exercise:

In Cocalc, do four things

  1. Try to take sqrt(2)
  2. import math
  3. Try to take sqrt(2) again
  4. calculate math.sqrt(2)

Exercise:

Refer to the documentation for the math module at https://docs.python.org/3/library/math.html.

Then compute the following after importing math:

\[log_{10}(23)\] ## Answer

import math
math.log(23,10)
## 1.3617278360175928

Exercise:

Compute \[ 2^{3.7}\]

Answer

math.pow(2,3.7)
## 12.99603834169977

Exercise

Compute the radian measure of 75 degrees.

Answer


math.radians(75)
## 1.3089969389957472

Exercise

Compute \(10!\), the factorial of 10. This could be loosely defined as the product of all of the integers from 1 to 10 inclusive.

Answer

math.factorial(10)
## 3628800

Exercise

Compute \[2*\pi\].

Answer

2 * math.pi
## 6.283185307179586

Now read Chapter 3 of PY4E beginning with Adding New Functions down to Parameters and Arguments.

Exercise:

Copy the code for print_lyrics() into a cell in Cocalc and run it. Did you see anything happen?

Answer

Nothing should have been noticeable when you executed the cell. A function does not execute when it is define. It executes only when it is called.

You should call it in the following cell just by typing its name as an expression.

There are several things to observe about defining functions.

  1. You always begin with “def”.

  2. Next is a name followed by parentheses, which may contain nothing.

  3. The line ends with a colon.

  4. The following lines are indented and define the action performed by the function when it is called.

Exercise:

Do a few things wrong to violate these rules and observe what happens.

Answer

It depends on what you messed up. Just remember not to freak out when these error messages occur.

The purpose of this simple function was just to allow you to display a “message” multiple times without retyping it every time.

Parameters and Arguments

Most functions are created with parameters, which allow their behavior to change depending on the needs of the user.

Here is an example of a function which computes the area of a circle with a given radius.

def circle_area(r):
    area = math.pi * r**2
    return area

We can use this function to calculate the area of a circle with radius 2. The value 2 is referred to as an argument. When the function is called with an argument, the body of the function is executed with the argument replacing the parameter.

Exercise:

Enter the function above into a cell in your notebook. Then call it in two ways.

  1. Just use the function as a command.
  2. Obtain the value computed by the function. Put the call to the function on the right side of an assignment statement.

Answer

Here’s the command version

circle_area(2)
## 12.566370614359172

The following code gets the value but doesn’t print it.

my_area = circle_area(2)

We can get the value by printing the variable with the result.

print(my_area)
## 12.566370614359172

Exercise

Create a function fahr(c). It computes the Fahrenheit temperature for a given Celsius temperature. Use it in both of the modes above to convert 95 degrees Celsius to a Fahrenheit equivalent. This is the correct temperature for brewing coffee.

Answer

def fahr(c):
    t = (9 / 5) * c + 32
    return t
    
print(fahr(95))
## 203.0
coffee_temp = fahr(95)
print(coffee_temp)
## 203.0

Fruitful and Void

A function which returns a value, like the two examples above, is called a fruitful. Some functions return no value. An example is the builtin funtion print(). If a function does not return a value it is called void. Void functions can be useful. Take the circle_area function above delete the retun statement. Replace it with a print statement. Test it with an input of 2.

Answer

def circle_area(r):
    area = math.pi * r**2
    print("The area is", area)

circle_area(2)
## The area is 12.566370614359172

Now try to use this function on the right side of an assignment statement. What is the type of the vasriable on the left side of this statement?

Answer

my_area = circle_area(2)
## The area is 12.566370614359172
print(type(my_area))
## <class 'NoneType'>
print(my_area)
## None