Laboratory 4: FUNctions

Full name:

R#:

Title of the notebook:

Date:



What is a function in Python?

Functions are simply pre-written code fragments that perform a certain task. In older procedural languages functions and subroutines are similar, but a function returns a value whereas a subroutine operates on data. The difference is subtle but important.

More recent thinking has functions being able to operate on data (they always could) and the value returned may be simply an exit code. An analogy are the functions in MS Excel. To add numbers, we can use the sum(range) function and type =sum(A1:A5) instead of typing =A1+A2+A3+A4+A5

Calling the Function

We call a function simply by typing the name of the function or by using the dot notation. Whether we can use the dot notation or not depends on how the function is written, whether it is part of a class, and how it is imported into a program.

Some functions expect us to pass data to them to perform their tasks. These data are known as parameters( older terminology is arguments, or argument list) and we pass them to the function by enclosing their values in parenthesis ( ) separated by commas.

For instance, the print() function for displaying text on the screen is \called" by typing print('Hello World') where print is the name of the function and the literal (a string) 'Hello World' is the argument.

Program flow

A function, whether built-in, or added must be defined before it is called, otherwise the script will fail. Certain built-in functions "self define" upon start (such as print() and type() and we need not worry about those funtions). The diagram below illustrates the requesite flow control for functions that need to be defined before use.

An example below will illustrate, change the cell to code and run it, you should get an error. Then fix the indicated line (remove the leading "#" in the import math ... line) and rerun, should get a functioning script.

reset the notebook using a magic function in JupyterLab

%reset -f

An example, run once as is then activate indicated line, run again - what happens?

x= 4. sqrt_by_arithmetic = x**0.5 print('Using arithmetic square root of ', x, ' is ',sqrt_by_arithmetic )

import math # import the math package ## activate and rerun

sqrt_by_math = math.sqrt(x) # note the dot notation print('Using math package square root of ', x,' is ',sqrt_by_arithmetic)


Built-In in Primitive Python (Base install)

The base Python functions and types built into it that are always available, the figure below lists those functions.

Notice all have the structure of function_name(), except __import__() which has a constructor type structure, and is not intended for routine use. We will learn about constructors later.


Added-In using External Packages/Modules and Libaries (e.g. math)

Python is also distributed with a large number of external functions. These functions are saved in files known as modules. To use the built-in codes in Python modules, we have to import them into our programs first. We do that by using the import keyword. There are three ways to import:

  1. Import the entire module by writing import moduleName; For instance, to import the random module, we write import random. To use the randrange() function in the random module, we write random.randrange( 1, 10);28
  2. Import and rename the module by writing import random as r (where r is any name of your choice). Now to use the randrange() function, you simply write r.randrange(1, 10); and
  3. Import specific functions from the module by writing from moduleName import name1[,name2[, ... nameN]]. For instance, to import the randrange() function from the random module, we write from random import randrange. To import multiple functions, we separate them with a comma. To import the randrange() and randint() functions, we write from random import randrange, randint. To use the function now, we do not have to use the dot notation anymore. Just write randrange( 1, 10).

The modules that come with Python are extensive and listed at https://docs.python.org/3/py-modindex.html. There are also other modules that can be downloaded and used (just like user defined modules below). In these labs we are building primitive codes to learn how to code and how to create algorithms. For many practical cases you will want to load a well-tested package to accomplish the tasks.

That exercise is saved for the end of the document.

User-Built

We can define our own functions in Python and reuse them throughout the program. The syntax for defining a function is:

def functionName( argument ):
    code detailing what the function should do
    note the colon above and indentation
    ...
    ...
    return [expression]

The keyword def tells the program that the indented code from the next line onwards is part of the function. The keyword returntells the program to return an answer from the function. There can be multiple return statements in a function. Once the function executes a return statement, the program exits the function and continues with its next executable statement. If the function does not need to return any value, you can omit the return statement.

Functions can be pretty elaborate; they can search for things in a list, determine variable types, open and close files, read and write to files.

To get started we will build a few really simple mathematical functions; we will need this skill in the future anyway, especially in scientific programming contexts.


User-built within a Code Block

For our first function we will code $$f(x) = x\sqrt{1 + x}$$ into a function named dusty().

When you run the next cell, all it does is prototype the function (defines it), nothing happens until we use the function.


Example: The Average Function

Create the AVERAGE function for three values and test it for these values:


Example: The KATANA Function

Create the Katana function for rounding off to the nearest hundredths (to 2 decimal places) and test it for these values:


Variable Scope

An important concept when defining a function is the concept of variable scope. Variables defined inside a function are treated differently from variables defined outside. Firstly, any variable declared within a function is only accessible within the function. These are known as local variables.

In the dusty() function, the variables x and temp are local to the function. Any variable declared outside a function in a main program is known as a program variable and is accessible anywhere in the program.

In the example, the variables xvalue and yvalue are program variables (global to the program; if they are addressed within a function, they could be operated on.) Generally we want to protect the program variables from the function unless the intent is to change their values. The way the function is written in the example, the function cannot damage xvalue or yvalue.

If a local variable shares the same name as a program variable, any code inside the function is accessing the local variable. Any code outside is accessing the program variable


As Separate Module/File

In this section we will invent the neko() function, export it to a file, so we can reuse it in later notebooks without having to retype or cut-and-paste. The neko() function evaluates:

$$f(x) = x\sqrt{|(1 + x)|}$$

Its the same as the dusty() function, except operates on the absolute value in the wadical.

  1. Create a text file named "mylibrary.txt"
  2. Copy the neko() function script below into that file.

     def neko(input_argument) :
         import math #ok to import into a function
         local_variable = input_argument * math.sqrt(abs(1.0+input_argument))
         return local_variable
  1. rename mylibrary.txt to mylibrary.py
  2. modify the wrapper script to use the neko function as an external module

In JupyterHub environments, you may discover that changes you make to your external python file are not reflected when you re-run your script; you need to restart the kernel to get the changes to actually update. The figure below depicts the notebook, external file relatonship



Rudimentary Graphics

Graphing values is part of the broader field of data visualization, which has two main goals:

  1. To explore data, and
  2. To communicate data.

In this subsection we will concentrate on introducing skills to start exploring data and to produce meaningful visualizations we can use throughout the rest of this notebook. Data visualization is a rich field of study that fills entire books. The reason to start visualization here instead of elsewhere is that with functions plotting is a natural activity and we have to import the matplotlib module to make the plots.

The example below is code adapted from Grus (2015) that illustrates simple generic plots. I added a single line (label the x-axis), and corrected some transcription errors (not the original author's mistake, just the consequence of how the API handled the cut-and-paste), but otherwise the code is unchanged.


Example- The Hopeless Romantic!

Copy the wrapper script for the plotAline() function, and modify the copy to create a plot of $$ x = 16sin^3(t) $$ $$ y = 13cos(t) - 5cos(2t) - 2cos(3t) - cos(4t) $$ for t raging from [0,2$\Pi$] (inclusive).

Label the plot and the plot axes.



Here are some great reads on this topic:

Here are some great videos on these topics:



Exercise: A Function for Coffee. Because Coffee is life!


Write a pseudo-code for a function that asks for user's preferences on their coffee (e.g., type of brew), follows certain steps to prepare that coffee, and delivers that coffee with an appropriate message. You can be as imaginative as you like, but make sure to provide logical justification for your script.

* Make sure to cite any resources that you may use.