CIS 111 Functions

Objectives

  • Create user-defined functions
  • Create a docstring for function documentation
  • Use user-defined functions
  • Display a function's docstring
  • Return a value from a function
  • Return multiple values from a function
  • Create functions with default values for its arguments
  • Create functions with named arguments
  • Create functions with a variable number of arguments
  • Explain how variable scope works
  • Know that Python does not support function overloading

Creating a function

The basic function syntax is: def functionName(parameterList): statements return value # optional

Example: def add(n1, n2): sum = n1 + n2 return sum # returns the result to the calling code

Calling a function

Example: num = add(7, 5) # now num would be 12

Using default values for function arguments

Example: def add(n1 = 10, n2 = 7): sum = n1 + n2 return sum # returns the result to the calling code

  • add() would return 17
  • add(3) would return 10
  • add(3, 2) would return 5

Using named function arguments

Example: def divide(dividend, divisor): return dividend / divisor

  • divide(10, 2) would return 5.0
  • divide(dividend=10, divisor=2) would return 5.0
  • divide(divisor=10, dividend=2) would return 0.2

Using a variable number of arguments

Example: def add(*n): sum = 0 for i in n: sum = sum + i return sum

  • add(3, 5) would return 8
  • add(3, 5, 10) would return 18
  • add(3, 5, 10, -2) would return 16

Returning multiple values from a function

Example: def circleCalculator(radius): area = math.pi * radius**2 circumference = 2 * math.pi * radius return circumference, area

  • circleCalculator(1) returns (6.283185307179586, 3.141592653589793)
  • circleCalculator(2) returns (12.566370614359172, 12.566370614359172)
  • circleCalculator(3) returns (18.84955592153876, 28.274333882308138)
  • circ, area = circleCalculator(3) sets circ and area
    • print(circ) displays 18.84955592153876
    • print(area) displays 28.274333882308138

Variable scope

Variables declared inside a function are only accessible within that function (unless the "global" or "nonlocal" keywords are used). Example: n = 7 x = 6 def change(n): n = 9 x = 21 print(n, x) print(n, x) # displays 7 6 change(n) # displays 9 21 print(n, x) # displays 7 6

Function overloading

Some programming languages allow you to create multiple versions of a function with the same name if they can be differentiated by the number, type, or order of their parameters. That is called function (or method) overloading. Function overloading is NOT supported in Python.

Docstrings

Docstrings are triple quoted strings which can span multiple lines. They are often used to document functions. In that case, the docstring must start at the first line of the function. The docstring can be accessed using the function's __doc__ property.

Example: >>> def average(*n): """Returns the average value of the values passed in""" count = 0 sum = 0 for i in n: count = count + 1 sum = sum + i return sum / count

  • print(average.__doc__) # displays average's docstring
  • average(14, 3, 2, 9, 7) returns 7.0
  • average(1, 1, 1, 1, 7) returns 2.2