Functions and Context


Context

A community is broken down into households, and each household has its own conventions about what names mean. The person referred to as John in one household is not necessarily the same as a person referred to as John in a different one.

Computer programs are similar. A computer program is broken down into small pieces that we will call contexts, and in each context names can have different meanings.

A function definition is a context. That means the names used in one definition do not interfere in any way or correspond in any way to the names used in a different definition.

For example, suppose that you define two functions trip and trap as follows.

  trip(x) = x + 1
  trap(x) = x - 1
The fact that both use name x does not matter. Each equation is a separate definition. Let's demonstrate by computing the value of expression trip(trap(3)).
  trip(trap(3)) 
    = trip(3 - 1)
    = trip(2)
    = 2 + 1
    = 3
Notice that the fact that trip and trap both use x never even comes up, since we only use one function definition at a time to do a substitution.


Independence of contexts I

Because a function definition is a separate context, the names that you choose for variables cannot affect any other context. For example, suppose that function f(y) is defined as follows.

  f(y) = y*y - 1
Now you write a small straight-line program.
  Let w = f(2).
  Let v = f(w).
Are we allowed to write f(w)? You might object that it is supposed to be f(y). But remember two things.
  1. What f calls its argument is irrelevant to all other contexts. So it cannot possibly affect this small straight-line program in any way.

  2. The argument passed to a function can be any expression. There are no restrictions saying, for example, that it must be something called y.

Let's do a hand simulation of this program. You will see that no problem arises. First, compute the value of w
  w = f(2)
    = 2*2 - 1
    = 4 - 1
    = 3
Now compute v. But remember that we have just found that w = 3, so, in this context, we can replace w by 3.
  v = f(w)
    = f(3)
    = 3*3 - 1
    = 9 - 1
    = 8


Independence of contexts II

Contexts are truly separate from one another. When you are working in one context, you cannot peek at names that are defined in a different context.

To illustrate, suppose that you try to define function g(x) by

  g(x) = x + y
then write the following straight-line program.
  Let y = 1.
  Let z = g(2).
The hope is to use the definition of y when doing a substitution for g(2). But that is not allowed! The definition of y is made in one context (the straight-line program) and so it cannot be used in another context (the definition of g). In fact, when the compiler sees the definition of g, it will complain that there is nothing called y in that context.


Problems

  1. [solve] If function fox is defined by

      fox(x) = x*x + 2*x + 1
    
    do you need to be sure to define what x is in any place where you use fox? For example, to make y = fox(3), do you need to write
      Let x = 3.
      Let y = fox(x).
    
    or would it be acceptable just to say
      Let y = fox(3).
    

  2. [solve] Are definitions

      wolf(x,y) = x + 2*y
    
    and
      wolf(u,v) = u + 2*v
    
    completely equivalent? Is there any way that you could use wolf that would be correct for one of the definitions but not for the other? Are the answers produced by expressions guaranteed to be the same, regardless of which definition you use, no matter what?


Summary

Each function definition is a separate context. The names that you use in it are unrelated to names used in other parts of the program.


Review

Our goal is to create simple and elegant programs and function definitions.

Working out algorithms requires an eye for detail. You can develop an eye for detail with practice.

Evaluate expressions step-by-step from smaller to larger subexpressions.