We give a brief discussion on the use of functions.

Functions

The distance between any two points and in the plane is given by Suppose you are given the points , , , and are asked to find the maximum distance between any possible pair. To accomplish this in a single program with what we have covered so far would require writing down (essentially) the same code six times, once for each pair and , and , etc.

A function will help us with the above problem in two ways:

  • By compartmentalizing code into a function, we can reuse the same set of steps without repeating ourselves.
  • If there is ever a need to make a change or fix an error, a function provides a singular location where the code can be altered.

A Python function will have the following syntax:

==============================
 
def functionName(inputs):  
        code  
        return variable  
==============================

The key word def denotes the beginning of a function definition. Using the same guidelines for variable names, functionName is your choice for the name of the function and will be used to ‘call’ it as needed. A collection of comma separated values in inputs will act as the inputs to the function. The portion labeled code is simply the set of instructions to be completed whenever the function is called. Finally, we have the line with the keyword return. So far, we have been using print to see the result of a calculation on the screen. In the case of a function, we will likely want the resulting output to be used in another calculation. The keyword return provides us with this ability. Any variable after return represents the output value of the function and can be assigned to another variable or passed into another function without displaying the value on the screen. The keyword print, on the other hand, prints the value to the screen but does not provide us with any data that can be assigned elsewhere. When using print, the computer essentially forgets what it just computed.

Below is an example of the function that computes the distance between two arbitrary points, and .

==============================
 
def dist(x1,y1,x2,y2):  
        d = ((x1-x2)**2+(y1-y2)**2)**(0.5)  
        return d  
==============================

This function, as long as it is defined before it is called, can now be used multiple times with different inputs to compute the distance between several pairs of points. Below we give an example.

Note the importance of whitespace when defining a function. Any lines that are not indented are not part of the function definition. Also of importance is the order in which the variables where passed into the function.

Scope

The scope of a variable is essentially where the variable ‘exists’ and can be referenced. Variables defined in a program outside of a function are global variables and can be used inside of a function as well. Variables defined inside of a function are local variables and cannot be used outside of the function where they are first defined. Below we give some examples to illustrate the differece between these types of variables.

In this example, x is global and y is local.

This example shows that you cannot use a local variable outside of the function that defined it.

This example shows what happens if there are global variables and function inputs with the same name. (This situation should be avoided when possible.)

While the scope of a variable might seem a bit technical, it is an important detail that will keep us from making preventable mistakes in future lessons.

Problems

Consider using the SageCell below as your workspace for answering the following questions.

At this point the problems will no longer ask for or provide flowchart solutions. Flowcharts continue to be a helpful way to visualize an algorithm and it is recommended that you use them before writing code if necessary.

Define a function called square that returns the square of any real number x. Note that the hint for this problem is the solution.
Define a function called avg2 that computes the average of two numbers a and b. Note that the hint for this problem is the solution.
Define a function called crit that computes the critical points(s) of a quadratic polynomial where . Note that the hint for this problem is the solution.
Define a function div5 that determines if an integer is divisible by 5 or not. If the number is divisible by 5, the function should return the boolean value True. If the number is not divisible by 6, the function should return the boolean value False. Note that the hint for this problem is the solution.
We give an example of another solution where the if statment does not make use of a comparison operator..
Suppose that you can rent a car for $150 per day or buy it for $3,500. Define a function called car_option that, given an integer , determines if it is cheaper to rent the car for days or to buy it. If it is cheaper to rent the car, the function should return a 1. If it is cheaper to buy the car, the function should return a 0. Note that the hint for this problem is the solution.
Define a function called num_real_roots that computes the number of distinct real roots of a quadratic polynomial where .
Consider having three inputs for this function, a, b, and c and consider the quantity . Note that the second hint for this problem is the solution.

Workspace