Вход

Забыли?

вход по аккаунту

код для вставкиСкачать
```CSC 160
Computer Programming
for Non-Majors
Lecture #4: Defining Variables
[email protected]
Two more language rules
• Today we will see our first keyword: define.
• It is used in our next two syntax rules:
- Syntax Rule #2: Defining Variables
- Syntax Rule #3: Defining Functions
What is a variable?
• A variable, like X, is a placeholder in
an expression that stands for some
fixed unknown quantity.
• X can be used to represent any
number. If we find a value for it, (such
as 7), we can evaluate the expression
• 6*X+2
What is a variable?
• A variable, like X, is a placeholder in
an expression that stands for some
fixed unknown quantity.
• X can be used to represent any
number. If we find a value for it, (such
as 7), we can evaluate the expression
• 6 * 7 + 2 (remember to use the order
of operations, also known as
PEMDAS)
What is a variable?
• A variable, like X, is a placeholder in
an expression that stands for some
fixed unknown quantity.
• X can be used to represent any
number. If we find a value for it, (such
as 7), we can evaluate the expression
• 42 + 2
What is a variable?
• A variable, like X, is a placeholder in
an expression that stands for some
fixed unknown quantity.
• X can be used to represent any
number. If we find a value for it, (such
as 7), we can evaluate the expression
• 44
Why use variable definitions?
• In Scheme programming, a variable is just a
named constant. (For example, 7, 3.14, and
• When a number appears repeatedly, it makes
enormous sense to give a name and use it by
name. Pi is an obvious example.
• (define PI 3.14) or (define PI 3.14159)
depending on how much accuracy we need.
• This can be done with words (and other data
types) as well. For example,
Syntax Rule #2:
Defining a Variable

Whether you have numbers, words,
or any other type of data, variables
are defined in the same general way:
(define VAR-NAME its-value)
Examples:
(define PI 3.14)

Exercise 1: Applying Variables
3x - 6/x
- Test with X = 2; right answer 3
- Type (define X 2) so DrScheme
knows what x is.
 √(b2-4ac)
 Remember to write define
statements.
 Test with A=2, B=-7, C=3; right

Defining a variable as a picture
• Just as we put 3.14 or “adam” into a
variable, we can do the same thing with an
image. Here is an example with a picture.
• (define ME …)
• Fill in … with a picture.
ME is defined as the picture
• (define ME
)
• Being able to define variables as pictures
is especially helpful because if you want to
use the same picture several times (in the
same file), you only have to find and insert
it once.
Exercise 2: Applying Variables


Find a picture on your computer, or a website,
and define it as a variable.
Use your first name in caps as the variable’s
name.

Find the width and height of the picture.

Put a solid blue circle on top of the picture.
How can we square the number 5?
• We can do (* 5 5).
• We can do (expt 5 2).
• We can do (sqr 5).
• Recall that *, expt, and sqr are all
predefined functions.
How can we cube the number 5?
• We can do (* 5 5 5).
• We can do (expt 5 3).
• Note that the cube function is not
predefined.
Example: Finding cubes of a number
a) 0
b) 5
c) 17
d) 1234567890
e) The result of (d)
One way
a) (* 0 0 0)
“should be 0”
b) (* 5 5 5)
“should be 125”
c) (* 17 17 17) “should be a few thousand”
d) (* 1234567890 1234567890