Week 5: Fruitful functions

A fruitful function is one that returns a value back to the calling program.

Example: Getting valid input

There are many instances where we need the user to provide an integer input, but we want to ensure that the number that they provide is reasonable for the problem at hand. For example, consider the main program shown below. Here we get both an age and a graduation year from the user. We want to be sure that age is between 10 and 100 and that the graduation year is between 2020 and 2023.

def main():
    age = getIntegerInRange("Enter age: ", 10, 100)
    gradYear = getIntegerInRange("Enter graduation year: ", 2020, 2023)
    print("You are %d years old and will graduate in %d" % (age, gradYear))

Let’s write this helper function for getting valid integer input.

def getIntegerInRange(prompt, low, high):
   """
   Purpose: Continually prompts the user until a valid input between low and
   high is provided.
   Parameters:
   prompt    string prompt to print when getting input
   low       int representing the lowest valid input
   high      int representing the highest valid input
   Returns: A valid input provided by the user
   """
   value = input(prompt)
   while value < low or value > high:
      print("Invalid.  Enter a value between", low, "and", high)
      value = input(prompt)
   return value

We use a while loop here because we aren’t sure how many times the user may make a mistake. The loop will only stop when a valid number is provided. After the loop is done, we return the valid number back to the calling program.

Scope of a variable

The scope of a variable is the section of a program where a variable is known.

For example consider the variables low and high in the program above. They are only known within the function getIntegerInRange. If you were to refer to one of these variables in main it would cause a syntax error. Try it!

Similarly, the variables age and gradYear are only known within the main program. If you were to refer to on of these variables in getIntegerInRange it would cause a syntax error.

Using a flag

There are often multiple ways to solve a problem in a programming language. Let’s try solving the problem of getting valid input in a slightly different way.

A common programming pattern when using a while loop is to create a flag. A flag is a boolean variable. Often a flag will be initialized to False and then it’s time for the loop to end will be set to True.

def getIntegerInRange(prompt, low, high):
   """
   Purpose: Continually prompts the user until a valid input between low and
   high is provided.
   Parameters:
   prompt    string prompt to print when getting input
   low       int representing the lowest valid input
   high      int representing the highest valid input
   Returns: A valid input provided by the user
   """
   done = False
   while not done:
      value = input(prompt)
      if value >= low and value <= high:
         done = True
      else:
         print("Invalid.  Enter a value between", low, "and", high)
   return value