Skip to main content

Foundations of Python Programming: Functions First

Section 4.6 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 (recall abstraction).
For example, here is a summary of some functions we have seen already.
  • input takes one parameter, a string. It is displayed to the user. Whatever the user types is returned, as a string.
  • int takes one parameter. It can be of any type that can be converted into an integer, such as a floating point number or a string whose characters are all digits.
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. (Consider practicing this by looking up the printfunctions docstring. 1 )
To build your understanding of any function, you should aim to answer the following questions:
  1. How many parameters does it have?
  2. What data type(s) are being 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…
  • a built in function, look at its docstring to see what it requires and what it returns. For example, len(x), must be passed a sequence data type like a string and will return an integer. x can’t be a number or a Boolean.
  • mathematical operators, then:
    • x - y, x / y, or x ** y: x and y must be numbers (integer or float)
    • x + y: x and y must both be numbers or both be strings
    • x * y: x and y can both be numbers; or one can be a string and the other must be a number
  • logical operators like x and y,x or y, not(x): x and y must be data types that can be evaluated as Booleans. 2 
Check your understanding: decode this function definition

Checkpoint 4.6.1.

    How many parameters does function cyu3 take?
    def cyu3(x: ???, y: ???) -> ???:
      COEFFICIENT = 2.5
      return (x + y) * COEFFICIENT
    
  • 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
  • x and y.
  • 3
  • Count the number of variable names inside the parenetheses on line 1.
  • Can’t tell
  • You can tell by looking inside the parentheses on line 1. Each variable name is separated by a comma.

Checkpoint 4.6.2.

    What are the possible types of variables x and y?
    def cyu3(x: ???, y: ???) -> ???:
      COEFFICIENT = 2.5
      return (x + y) * COEFFICIENT
    
  • integer
  • Addition and multiplication can be performed on both floats and integers.
  • float
  • Addition and multiplication can be performed on both floats and integers.
  • x could be a string and y could be a float
  • Although possible, there’s a more general answer.
  • string
  • Strings cannot be multiplied by a float.
  • Can’t tell
  • You can tell from some of the operations that are performed on them.

Checkpoint 4.6.3.

    What datatype is returned?
    def cyu3(x: ???, y: ???) -> ???:
      COEFFICIENT = 2.5
      return (x + y) * COEFFICIENT
    
  • integer
  • Since the sum of x and y is multiplied by a float, the value can not be an integer.
  • float
  • The sum of x and y is multiplied by a float, meaning the returned value is a float.
  • string
  • Strings cannot be multiplied by floats.
  • boolean
  • There are no boolean operations in the given function.
  • Can’t tell
  • You can tell from some of the operations that are performed on it.

Checkpoint 4.6.4.

    What should this function’s header look like?
    """
    Calculates the combined areas of two squares.
    
    Parameters:
      width_a : _____
      The width of the first square.
      width_b : _____
      The width of the second square.
    
    Returns:
      _____ : The combined areas of the squares.
    """
    
  • def squares_area(width_a: int, width_b: int) -> int:
  • Since both pieces of data are integers, and the formula for calculating the area of a square wouldn’t result in a float given an integer, this would be correct.
  • def squares_area(width_a: float, width_b: float) -> int:
  • Based on the formula for finding the area of a square, and that no rounding is present, by giving this function two floats, an integer would not be returned.
  • def squares_area(width_a: int, width_b: int) -> float:
  • Based on the formula for finding the area of a square, and that the widths are only being multiplied, by giving this function two integers, a float could not be returned.
  • def squares_area(width_a: float, width_b: float) -> float:
  • Since both pieces of data are floats, and the formula for calculating the area of a square would result in a float given a float, this would be correct.
  • def squares_area(width_a, width_b):
  • Although technically correct, type hinting would make this function easier to interpret.
You have attempted of activities on this page.