Skip to content

Lab 03: Functions and Variable Scope

So far, you have used a couple of functions to complete some tasks. In this lab, we will be delving a bit further into the inner parts and workings of a function, how to create one, as well as introduce the idea of variable scope.

Getting Started

To best describe how a function behaves, we could go into how functions work in mathematics and go ahead from there, but I think the Information Processing Cycle would be a better way to describe this. Arguably, using the definition of a computer would be much easier to understand rather than what goes under the hood of your stereotypical function \(f(x)\).

Information Processing Cycle

"What's a Computer?" from Apple (2017)
What's a computer?

If we had to define what a computer is, I'd define it as a device capable of following instructions to accept input, process that input, and produce information (output). This follows a concept what we call the Information Processing Cycle. Input, processing, output - that's generally what it is.

Information Processing Cycle

There are 4 stages in the Information Processing Cycle:

  • input: data or instructions that are entered into a computer
  • process: action(s) or routine(s) performed by a computer to convert input into output
  • output: processed data or information
  • storage: information that is stored for future use and retrieval

How does this relate to a function? Answer: Functions behaves the same way.. mostly.

Structure of a Function

Many functions work by retrieving input(s) to produce output(s). There may be functions which do not require inputs, or return meaningful outputs.

  • These inputs are called parameters. Functions may require these as part of its process, but more often than not they are intentional. There's no limit to how many parameters a function can have, and additionally some can be deemed optional (optional parameters are declared at the very end after all the compulsory ones).
  • Functions return output(s). The keyword here is return, which describes a product being produced upon completing the function's process. However, there are functions that do not return anything (technically they do, see below).
Functions can return nothing??

Well, visually it looks that way (and that's where I'd normally stop at if I was confusing anyone on first pass). However, all functions in Python actually return something. By default, functions will return None by default unless stated otherwise. From the Python shell, this is only obvious if you printed such a function out, not just by calling it.

Python Shell
>>> def f():
...     x = 2
...
>>> f()  # Nothing gets called
>>> print(f())
None

The following visual details the parts of a typical function in Python.

In comparison, let's observe how an arbitrary mathematical function works. Say we have function \(f(x) = 5x+6\). The name of this function here is \(f\), and it requires an input/parameter \(x\). By providing this input \(x\), the expression \(5x+6\) is the process this input goes through to transform into this function's output. For instance, if we provided an input \(x=1\), the output of \(f(1) = 5(1)+6 = 11\). With input \(x=2\), we get \(f(2) = 5(2)+6 = 16\). Inputs \(x=1\) and \(x=2\) here are transformed using function \(f\) to produce outputs \(11\) and \(16\) respectively.

Pretty similar to how functions work in Python so far, right? With mathematical functions like this one or more established ones like the trigonometric functions sine (i.e., \(\sin(x)\)), cosine (i.e., \(\cos(x)\)) and tangent (i.e., \(\tan(x)\)), I believe it's a bit simpler in a sense that inputs are required (and on a more elementary level you mostly see one-parameter functions) and outputs are expected. With functions in Python or programming in general, that rule is not a guarantee.

Function Scope

Let's take a look at this simple program right here.

1
2
3
4
5
6
7
8
def f(x):
    y = (x+1)**2

    return y


a = 3
print(f(a))

Global and Local Variables

In the context of computing, when we say something is...

  • global, it operates or is applied through the whole of a file, program, etc.; e.g., global searches
  • local, it is only available for use in one part of a program (from a computer networking standpoint, a local device is something that can be connected to without use of a network)

Source: New Oxford American Dictionary (available from Apple's Dictionary app)

Lab Activity 01: Let's Create Some Functions!

Lab Activity 02: Calculating Tax

"In this world, nothing can be said to be certain, except death and taxes."
- Benjamin Franklin