more on Functions (class slides)

CSC 110 - More on Functions in Python

Global vs. Local variables (scope)

  • Every variable that is created has a particular scope
  • The scope of a variable is the region in the code where the variable can be used or modified

Global vs. Local variables (scope)

  • Local Variables have local scope – for example, a variable assigned inside a function can only be used or modified within that function
  • Global Variables have global scope – for example, a variable delcared outside a function can be accessed or modified across multiple functions

Global or Local?

a = 5              
b = 10               

def sum():
    return a + b

def multiply():
    return a * b

def main():
    print(sum())
    print(multiply())

main()
15
50

Global or Local?

def sum(a, b):
    return a + b

def multiply(a, b):
    return a * b

def main():
    a = 5
    b = 10
    print(sum(a, b))
    print(multiply(a, b))
    a = 10
    print(multiply(a, b))

main()
15
50
100

Argument vs. Parameter

  • Never set variables as global variables, pass values to functions when called
  • When a function is defined, the variables you want to pass to the function are called parameter variables
  • When the function is then called, the values you pass to the function are called arguments

Argument or Parameter?

# The parameters are a and b
def add(a, b):
    return a+b
  
# The arguments being passed through are 5 and 4
add(5, 4)

Write functions

Write two functions: sphere_area and sphere_volume that calculates the area and the volume of a sphere:

use 3.1415 for \(\pi\) and return the rounded value.

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

\[ v = {4 / 3} \cdot \pi \cdot radius^3 \] Name your file as sphere.py and submit to gradescope.

Test case:

r = 0.75
print(sphere_area(r)) # 7.07
print(sphere_volume(r)) # 1.77

Write functions – 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 functions

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 functions – 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

Function Comments

  • Every function created is required to have a function comment, including main
  • Function comments are a multi-line string (as opposed to using # for other comments)

Function Comments

'''
Xinchen Yu
CSC110
Class Demonstration
This program has two functions: one to calculate the area of a sphere,
the other to calculate the volume of a sphere.
The main() function is called to print to the standard output the
area and volume of a sphere of radius .75
'''
def sphere_area(radius):
  '''
  This function calculates the area of a sphere of given radius.
  Args:
    radius: integer representing the radius of the sphere
  Returns:
    The float representing the area of a sphere of the given radius
  '''
  area = 4 * 3.1415 * radius**2
  return round(area, 2)

def sphere_volume(radius):
  '''
  This function calculates the volume of a sphere of given radius.
  Args:
    radius: integer representing the radius of the sphere
  Returns:
    The float representing the volume of a sphere of the given radius
  '''
  volume = (1 / 3) * sphere_area(radius) * radius
  return round(volume, 2)

def main():
  '''
  This function prints the area and volume of a sphere of radius .75.
  Args:
    None
  Returns:
    None
  '''
  r = .75
  a = sphere_area(r)
  v = sphere_volume(r)
  print(a, v)
  
main()
7.07 1.77

Function to calculate the volume of a cylinder

Write a function that does the following:

  1. Its name is volume
  2. It takes two integer arguments: radius and height
  3. It calculates the volume of a cylinder, based on radius and height. Volume is area multiplied by height.
  4. It returns the float value for calculated volume.

Function to calculate the volume of a cylinder

def volume(radius, height):
  # calculate the area first
  area = 3.1415 * radius ** 2
  
  # multiply area by height
  vol = area * height
  
  # return calculated volume
  return vol

def main():
  print(volume(1, 2)) # 6.283
  print(volume(6, 10)) # 1130.94
  print(volume(5, 5)) # 392.68750000000006
  
main()