Presentation is loading. Please wait.

Presentation is loading. Please wait.

CSC 160 Computer Programming for Non-Majors Lecture #4: Defining Variables Prof. Adam M. Wittenstein

Similar presentations


Presentation on theme: "CSC 160 Computer Programming for Non-Majors Lecture #4: Defining Variables Prof. Adam M. Wittenstein"— Presentation transcript:

1 CSC 160 Computer Programming for Non-Majors Lecture #4: Defining Variables Prof. Adam M. Wittenstein Wittenstein@adelphi.eduhttp://www.adelphi.edu/~wittensa/csc160/

2 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

3 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

4 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)

5 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

6 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

7 Why use variable definitions? In Scheme programming, a variable is just a named constant. (For example, 7, 3.14, and “adam” are constants.) 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, (define NAME “adam”).

8 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: 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: Examples: (define PI 3.14) (define NAME “adam”)

9 Exercise 1: Applying Variables 3x - 6/x 3x - 6/x - Test with X = 2; right answer 3 - Test with X = 2; right answer 3 - Type (define X 2) so DrScheme knows what x is. - Type (define X 2) so DrScheme knows what x is. √(b 2 -4ac) √(b 2 -4ac) Remember to write define statements. Remember to write define statements. Test with A=2, B=-7, C=3; right answer 5 Test with A=2, B=-7, C=3; right answer 5

10 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.

11 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.

12 Exercise 2: Applying Variables Find a picture on your computer, or a website, and define it as a variable. 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. Use your first name in caps as the variable’s name. Find the width and height of the picture. Find the width and height of the picture. Put a solid blue circle on top of the picture. Put a solid blue circle on top of the picture.

13 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.

14 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.

15 Example: Finding cubes of a number a)0 b)5 c)17 d)1234567890 e)The result of (d)

16 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 1234567890) “should be about thirty digits long” e)(* yecch….) “should be about ninety digits long”

17 Better: define variables (define BIG 1234567890) (* BIG BIG BIG) (define BIGGER (* BIG BIG BIG)) (* BIGGER BIGGER BIGGER)

18 In summary… We can set a variable equal to a specific value. As we go through programming this semester, we will see several ways that variables are helpful. One example is not having to insert the same picture several times (in the same file). Next time… A better way to find the cube of a number – Defining functions.


Download ppt "CSC 160 Computer Programming for Non-Majors Lecture #4: Defining Variables Prof. Adam M. Wittenstein"

Similar presentations


Ads by Google