Decoding a Function

In general, when you see a function definition you will try figure out what the function does, but, unless you are writing the function, you won’t care how it does it.

For example, here is a summary of some functions we have seen already.

Sometimes, you will be presented with a function definition whose operation is not so neatly summarized as above. Sometimes you will need to look at the code, either the function definition or code that invokes the function, in order to figure out what it does.

To build your understanding of any function, you should aim to answer the following questions:

  1. How many parameters does it have?
  2. What is the type of values that will be passed when the function is invoked?
  3. What is the type of the return value that the function produces when it executes?

If you try to make use of functions, ones you write or that others write, without being able to answer these questions, you will find that your debugging sessions are long and painful.

The first question is always easy to answer. Look at the line with the function definition, look inside the parentheses, and count how many variable names there are.

The second and third questions are not always so easy to answer. In python, unlike some other programming languages, variables are not declared to have fixed types, and the same holds true for the variable names that appear as formal parameters of functions. You have to figure it out from context.

To figure out the types of values that a function expects to receive as parameters, you can look at the function invocations or you can look at the operations that are performed on the parameters inside the function.

Here are some clues that can help you determine the type of object associated with any variable, including a function parameter. If you see…

Check your understanding: decode this function definition

    exceptions-1: How many parameters does function cyu3 take?

    def cyu3(x, y, z):
       if x - y > 0:
          return y -2
       else:
          z.append(y)
          return x + 3
    
  • 0
  • Count the number of variable names inside the parenetheses on line 1.
  • 1
  • Count the number of variable names inside the parenetheses on line 1.
  • 2
  • Count the number of variable names inside the parenetheses on line 1.
  • 3
  • x, y, and z.
  • Can't tell
  • You can tell by looking inside the parentheses on line 1. Each variable name is separated by a comma.

    exceptions-2: What are the possible types of variables x and y?

    def cyu3(x, y, z):
       if x - y > 0:
          return y -2
       else:
          z.append(y)
          return x + 3
    
  • integer
  • x - y, y-2, and x+3 can all be performed on integers.
  • float
  • x - y, y-2, and x+3 can all be performed on floats.
  • list
  • x - y, y-2, and x+3 can't be performed on lists.
  • string
  • x - y and y-2 can't be performed on strings.
  • Can't tell
  • You can tell from some of the operations that are performed on them.

    exceptions-3: What are the possible types of variable z?

    def cyu3(x, y, z):
       if x - y > 0:
          return y -2
       else:
          z.append(y)
          return x + 3
    
  • integer
  • append can't be performed on integers.
  • float
  • append can't be performed on floats.
  • list
  • append can be performed on lists.
  • string
  • append can't be performed on strings.
  • Can't tell
  • You can tell from some of the operations that are performed on it.

    exceptions-4: What are the possible types of the return value from cyu3?

    def cyu3(x, y, z):
       if x - y > 0:
          return y -2
       else:
          z.append(y)
          return x + 3
    
  • integer
  • y-2 or x+3 could produce an integer.
  • float
  • y-2 or x+3 could produce a float.
  • list
  • y-2 or x+3 can't produce a list.
  • string
  • neither y-2 or x+3 could produce a string.
  • Can't tell
  • You can tell from the expressions that follow the word return.
Next Section - Method Invocations