More on Functions (class slides)

CSC 110 - More on Functions in Python

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

'''
Prof Lynam
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

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 declared outside a function can be accessed or modified across multiple functions

Global vs. Local variables (scope)

  • In the previous program we wrote (volume and area of sphere), r, v and a are local variables within the main() function.
  • The variable radius and area are local within the sphere_area(radius) function scope.
  • The variable radius and volume are local within sphere_volume(radius)

Global or Local?

a = 10               # What are the global and local variables?
b = 5                # Is the output of the two programs the same 
                     # or different?
def sum():
  return a + b

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

vs.

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

def main():
  print(sum(10, 5))
  
main()

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 a function

Write a Python function called tell_story that takes three arguments: hero and villain, representing the names of two characters, and repetition, representing the number of times to tell the story. The function should print out the following bedtime story:

hero set out to slay the evil villain!

But he set his sword variable to global.

So villain accessed his sword and set it to None.

Use local variables next time, hero!”

Write the main function, and call the ‘tell_story’ with the following test case: tell_story(“CSc 110 student”, “Professor”, 2)

Write a function – Part 2

  • Make sure all variables are local within the program.

  • Call the test case once with literals (tell_story(“CSc 110 student”, “Professor”, 2)), and once by passing in those values with local variables defined in main

  • Finally, add function comments for main and ‘tell_story’

Write a function

def tell_story_a(hero, villain, repetition):
  '''
  This function prints a bedtime story a specified number of times
  Args:
    hero: string representing the name of the hero
    villain: string representing the name of the villain
    repetition: integer dictating how many times to repeat printing the story
  Returns:
    None
  '''
  story_string = ""
  story_string = hero + " set out to slay the evil " + villain + "!\n"
  story_string = story_string + "But he set his sword variable to global.\n"
  story_string = story_string + "So " + villain + " accessed his sword and set it to None.\n"
  story_string = story_string + "Use local variables next time, " + hero + "!\n"
  story_string = story_string * repetition
  print(story_string, end='')
  
def tell_story_b(hero, villain, repetition):
  '''
  This function prints a bedtime story a specified number of times
  Args:
    hero: string representing the name of the hero
    villain: string representing the name of the villain
    repetition: integer dictating how many times to repeat printing the story
  Returns:
    None
  '''
  for i in range(repetition):
    print(hero + " set out to slay the evil " + villain + "!")
    print("But he set his sword variable to global.")
    print("So " + villain + " accessed his sword and set it to None.")
    print("Use local variables next time, " + hero + "!")

def main():
  '''
  This function calls both tell_story functions to test them out,
  once with local variables and once with literals
  '''
  tell_story_a("CSc 110 student", "Professor", 2)
  parameter_variable_a = "CSc 110 student"
  parameter_variable_b = "Professor"
  parameter_variable_c = 2
  tell_story_b(parameter_variable_a, parameter_variable_b, parameter_variable_c)
  
main()