functions (slides)

CSc 110 - Functions

Functions

  • Functions are named operations that are available to do tasks
  • Some functions are built-in functions that Python provides
  • Programmers can also define their own functions
  • Functions are called (or invoked)

Function definitions

def two():
  return 2

This function definition has many parts:

  • two is the name of the function
  • () is the parameter list (Here, it is empty)
  • the body (or content) of the function is indented
  • return 2 is a statement that causes the function to cease and produce the value 2

Example of a simple function

def add_one(n):
  return n + 1
  • add_one is the name of the function
  • (n) is the parameter list
  • the body (or content) of the function is indented
  • return n + 1 is a statement that causes the function to cease and produce the value n + 1

Another example

def cook_food(order):
  message = "Your " + order + " is ready!"
  return message

meal = cook_food("pasta")
print(meal)
meal = cook_food("burger")
print(meal)
Your pasta is ready!
Your burger is ready!
  • The function cook_food prepares a meal.
  • Then returns a string that says the meal is ready.
  • The returned message is stored in the meal variable.
  • Finally, print function displays that message to the caller.

Function to calculate area of a circle

Remember this from the last set of slides?

# assign a radius value
radius = 3
# compute the rounded area of a circle
area = round(3.1415 * radius ** 2, 2)
# print the area
print(area)

Function to calculate area of a circle

Function name is calculate_area. Given a radius parameter, it returns the rounded area of the circle.

def calculate_area(radius):
  area = 3.1415 * radius ** 2
  return round(area, 2)

def main():
  print(calculate_area(3)) # 28.27
  print(calculate_area(6)) # 113.09
  
main()

Write a function

Write a function that calculates the volume of a sphere:

  1. Its name is sphere_volume
  2. It takes one argument: radius
  3. It calculates the volume of the sphere (use 3.1415 for \(\pi\)):

\[ v = {4 / 3} \cdot \pi \cdot radius^3 \]

  1. It returns the rounded value for the calculated volume
  2. Test case: sphere_volume(.75) should return 1.77.

Write a function – sphere volume

def sphere_volume(radius):
  "calculates the volume of a sphere of given radius"
  volume = (4 / 3) * 3.1415 * radius**3
  return round(volume, 2)

def main():
  print(sphere_volume(.75)) # 1.77
  print(sphere_volume(2)) # 33.51
  print(sphere_volume(5.5)) # 696.89

main()
1.77
33.51
696.89

Write a function

Write a function that calculates the area of a sphere:

  1. Its name is sphere_area
  2. It takes one argument: radius
  3. It calculates the volume of the sphere (use 3.1415 for \(\pi\)):

\[ a = 4 \cdot \pi \cdot radius^2 \]

  1. It returns the value for the calculated sphere area
  2. Test case: sphere_area(.75) should return 7.07.

Write a function – solutions 1

def sphere_area(radius):
  "calculates the area of a sphere of given radius"
  area = 4 * 3.1415 * radius**2
  return round(area, 2)

def sphere_volume(radius):
  "calculates the volume of a sphere of given radius"
  volume = (4 / 3) * 3.1415 * radius**3
  return round(volume, 2)

def main():
  r = .75
  v = sphere_volume(r)
  a = sphere_area(r)
  print(v, a)

main()
1.77 7.07

Write a function

Comparing two formulas: \[ a = 4 \cdot \pi \cdot radius^2 \]

\[ v = {4 / 3} \cdot \pi \cdot radius^3 \] We can use area when calculating volume: \[ v = {1 / 3} \cdot a \cdot radius \]

Modify your sphere_volume function by calling sphere_area inside the function.

Write a function – solutions 2

def sphere_area(radius):
  "calculates the area of a sphere of given radius"
  area = 4 * 3.1415 * radius**2
  return round(area, 2)


def sphere_volume(radius):
  "calculates the volume of a sphere of given radius"
  volume = (1 / 3) * sphere_area(radius) * radius
  return round(volume, 2)


def main():
  r = .75
  v = sphere_volume(r)
  a = sphere_area(r)
  print(v, a)

main()
1.77 7.07

Write a function

Write a Python function named hypotenuse that takes two arguments: a and b representing the length of the two non-hypotenuse sides of a right triangle. The function calculate the hypotenuse according to the Pythagorean theorem: \(c = \sqrt(a^2 + b^2)\). Return it rounded at two decimals.

Test cases: hypotenuse(3, 4) should return 5.0, hypotenuse(10, 10) should return 14.14

Name your file hypotenuse.py and submit to gradescope.

Write a function

def sqrt(n):
  '''
  This function calculates the square root of a number
  Args:
    n: integer or float
  Returns:
    The square root of n
  '''
  return n**0.5

def hypotenuse(a, b):
  '''
  This function calculates the hypotenuse of a right angle triangle.
  Args:
    a: number (integer or float) representing one of the non-hypotenuse sides
    b: number (integer or float) representing one of the non-hypotenuse sides
  Returns:
    Float representing the length of the hypotenuse given a and b
  ''' 
  h = sqrt(a**2 + b**2)
  return round(h, 2)

def main():
  '''
  This function calls the hypotenuse function to calculate and then
  print out the hypotenuse of a right angle triangle of sides 3 and 4
  and the hypotenuse of a right angle triange of sides 10 and 10
  '''
  result = hypotenuse(3, 4)
  print(result)
  
  result = hypotenuse(10, 10)
  print(result)
  
main()
5.0
14.14