1 / 17

CSC 160 Computer Programming for Non-Majors Lecture #4: Defining Variables

CSC 160 Computer Programming for Non-Majors Lecture #4: Defining Variables. Prof. Adam M. Wittenstein Wittenstein@adelphi.edu http://www.adelphi.edu/~wittensa/csc160/. Two more language rules. Today we will see our first keyword: define . It is used in our next two syntax rules:

kathiel
Télécharger la présentation

CSC 160 Computer Programming for Non-Majors Lecture #4: Defining Variables

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. CSC 160Computer Programmingfor Non-MajorsLecture #4: Defining Variables Prof. Adam M. Wittenstein Wittenstein@adelphi.edu http://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: (define VAR-NAME its-value) • Examples: (define PI 3.14) (define NAME “adam”)

  9. 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 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. • 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.

  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 • 0 • 5 • 17 • 1234567890 • The result of (d)

  16. One way • (* 0 0 0) “should be 0” • (* 5 5 5) “should be 125” • (* 17 17 17) “should be a few thousand” • (* 1234567890 1234567890 1234567890) “should be about thirty digits long” • (* 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)

More Related