Transcript so just x x

CSC 160
Computer Programming
for Non-Majors
Lecture #7: Variables Revisited
Prof. Adam M. Wittenstein
[email protected]
http://www.adelphi.edu/~wittensa/csc160/
REVIEW: What are variables?
• A variable is a connection between a name and
a value.
• For example, in (define PI 3.14), we say PI is a
name and 3.14 is a value.
• In this course, we will use variables in only one
way, as a named constant.
• This means that the name X will always
correspond to one specific value within a single
Scheme file. Often, we even state this, at the
top of the Definitions Window, as in (define X 7).
I. What’s going on when
you define variables?
Order of Variable Definitions
• When you are defining only functions, the
order of definitions does not matter.
• However, the order for variable definitions
is significant.
• This is because DrScheme evaluates the
right-hand side immediately, without
looking at the remaining definitions.
This is allowed!
(define RADIUS 5)
(define DIAMETER (* 2 RADIUS))
• This is perfectly legal.
• RADIUS is set equal to the number 5.
• Then DIAMETER is equal to 2 times a
known number, RADIUS.
This is not allowed!
(define DIAMETER (* 2 RADIUS))
(define RADIUS 5)
• This produces the error “reference to
undefined identifier: RADIUS”.
• DrScheme does not yet know the
definition of RADIUS when it tries to
evaluate (* 2 RADIUS).
What happens with variables in
variable definitions?
• The fact that X = 7, as in (define X 7), is
meaningless until we call upon that name,
as in (+ X 2).
• Then, the X is assigned its value (7), and
added to 2, for a total of 9.
What happens with parameters in
function definitions?
• Suppose, you define the function:
(define (square num)
(* num num))
• When we invoke the square function, with
a particular argument like 6, as in:
(square 6)
the parameter num is replaced by the
number 6, and it has that value until the
function is done working, and produces
the answer of 36.
What happens with parameters in
function definitions?
• If after getting the answer, you invoke the
square function again as in:
(square 2)
then now you are assigning a new value to
num, that is num = 2.
• However, from the time you type an
example into the Interactions Window, until
you get its answer, the parameter (in this
case num) has the same value.
A Word About Other Programming
Languages…
• In some other programming languages like
Java or BASIC, you usually change the
value of the same variable frequently,
even for fairly small programs.
• Changing the value of a variable can be
done in Scheme too, but we can write
many varied functions in Scheme without
doing this.
So what is functional programming?
• During the first week of class, the readings
mentioned “functional programming”.
• We have now seen that writing programs is just
writing functions.
• Short programs, like most of the ones we have
done so far, consist of one function. Normally,
programs consist of many functions.
So what is functional programming?
• In high school, many of you evaluating numeric
functions, as in f(x) = x + 3.
• To find f(5), you replaced x with 5 and added 3
to get 8.
• The value of x does not change. It is still 5.
• The number 8 is given a different name, like f(x)
of y.
II. How Little People Do Variables
Parameters and Arguments
• Here is the square function with a test case,
(define (square x)
(* x x))
(square 3)
• Here x is a parameter. And in the test case,
x is replaced by the argument 3.
• A variable is the association in the little
person’s mind between a parameter (like x)
and an actual argument (like 3).
Variables vs. Parameters
• We started today by saying a variable is a name
associated with a value.
• In the case of a function definition, the “name” is
the parameter.
• The value is what we associate with it.
• So one variable is the parameter r set equal to
the number 5.
• And another variable is the parameter r set equal
to the number 2. (This second parameter can
just as easily be called num, or anything else.)
A Word about Slang
• We now know that a variable is technically
a name (sometimes a parameter)
associated with a value.
• Often people get lazy and call the
parameter by itself a variable, which is
technically wrong. (I have even slipped a
couple of times this semester. Try and
catch me if I do it again!)
Little people are not mindreaders!
• Here are two Scheme functions:
(define (f x)
(g 6))
(define (g y)
(+ x y))
• Suppose you clicked Run and then type
(f 4) into the Interactions Window.
• What will happen?
Little people are not mindreaders!
In Simply Scheme mode:
The function f indicates that (f x) = (g 6).
• The Stepper will replace the x with the 4, so it
becomes (f 4) = (g 6).
• The function g indicates that (g y) = (+ x y).
• The Stepper will replace the y’s with 6’s, so it
becomes: (g 6) = (+ x 6).
• You will then get an error message like:
ERROR – VARIABLE X IS UNBOUND.
• Why are you getting this error message?
Little people are not mindreaders!
In Beginning Student mode:
• The function g will return a Syntax Error
because there is a parameter, x, used in the
function body that is not named in the
function header.
Little people are not mindreaders!
• A parameter, like x, is what we call a local
variable. It only has meaning within the
function whose header it is part of.
• Once we leave the f function, and go to
work on the g function, x is no longer a
paramter.
• So the g function has no idea what x is.
• How can we adjust the two-function
program consisting of f and g to make it
work?
Spelling it out for the little people!
• We hire two little people Franz and Gloria.
• We saw that Gloria ran into the problem of
not knowing what x is.
• To fix this, x needs to be one of the
parameters in Gloria’s function, g.
• So we adjust g to become:
(define (g x y)
(+ x y))
Spelling it out for the little people!
• It is very important to remember that to
Scheme, Gloria’s parameter x and Franz’s
parameter x are often two different things.
• Franz’s x can be 3 and Gloria’s x can be 5.
• They may both be 3 in a particular
example, but that is nothing more than
coincidence.
Franz and Gloria Working Together
(define (f x)
(g x 6))
(define (g x y)
(+ x y))
; 1) Notice the body of Franz’s function changed so
; that g has two arguments.
; 2) Since Franz uses the g function in his function
; body, he must adhere to the g function’s header.
; 3) Otherwise, the g function would be confused and
; return an error message.
Franz and Gloria Working Together
(define (f x)
(g x 6))
(define (g x y)
(+ x y))
(f 4)
• The function f indicates that (f x) = (g x 6).
• The Stepper will replace the x with the 4, so it becomes
(f 4) = (g 4 6).
• The function g indicates that (g x y) = (+ x y).
• The Stepper will replace the x’s with 4’s and the y’s with
6’s, so it becomes: (g 4 6) = (+ 4 6).
• This gives the answer of 10.
III. Global and Local Variables
Two types of naming
• When defining functions, we have:
– a function name
• global variable (permanent)
• used throughout the Scheme file
– one or more parameter names
• local variable (temporary)
• applies only to the specific function it is in
Local Variables
• We have already seen this.
• In the square function,
(define (square x) (* x x))
x is a local variable (a.k.a. parameter).
• When we test the square function, we type
something like (square 3).
• In this test of this function only, the x is
replaced by a 3.
Local Variables
• A local variable is:
the association of
a formal parameter (name)
with
an actual argument (value).
• Technically, we “associate the value 3 with the
name x”.
• It is easier to just say “x has the value 3”, but
make sure you are aware that this only applies
within one test of one function, and not
throughout the program or Scheme file.
Global Variables
• Apply throughout the Scheme file
anywhere below their definition.
• In (define NUM 7),
NUM will permanently represent the
number 7.
• In (define (square x) (* x x)), the function
name square will permanently refer to the
function body (* x x).
Global Variables
• If we have (define X 7), then every X in
the entire Scheme file is replaced with 7.
• If we have (define (square x) (* x x)),
then every (square x) in the Scheme file is
replaced with (* x x).
A warning…
• Technically, if a global variable and local
variable have the same (exact) name, the
name will apply to the local variable within
its specific function, and the name will
apply to the global variable everyone else.
• It is very confusing to someone using your
program to have different things named
the same, so don’t do it!
IV. Preparing for Next Class
In summary…
• Variables are the association of a name and a
value.
• The order of variable definitions is very
important.
• There are two types of variables – local and
global.
• When writing functions with more than one
program, be aware that global variable names
apply everywhere, but local variable names only
apply within a specific function.
Next time…
• Animations (which are multi-function
programs)