APCO 1P00 - Week 2 Lecture Slides

APCO 1P00 - Week 2 Lecture Slides - Week 2 More Variables...

Info iconThis preview shows pages 1–14. Sign up to view the full content.

View Full Document Right Arrow Icon
1 Week 2 More Variables, Sounds,       Loops, and Arrays! Some slides are based on  Mark Guzdial’s  at Georgia Tech
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
2 Variables  (Cont’d) Variables are names that are storing information. Enable us to easily access that information by name. Can typically only hold one piece of information at any given time (a new assignment statement will overwrite whatever it is presently holding!). Make programmers lives MUCH easier!
Background image of page 2
3 More examples of Variables >>> myVar = 10 >>> print myVar 10 >>> myVar = 5 >>> print myVar 5 >>> myVar = myVar + 3 >>> print myVar 8 This line essentially says: myVar is going to store whatever is already in myVar, and add three to it
Background image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
4 Last example of simple variable storage: varA = 5 varB = varA * 2 varC = varB / varA varC = varC + 25 Here is what happens: varA stores the value 5 varB stores whatever varA is storing, multiplied by 2 varC stores whatever varB is storing, divided by whatever varA is storing. varC stores whatever varC already stores, plus 25 .
Background image of page 4
5 = 10 / 5 = 2 4. varC = varC + 25 = 2 + 25 = 27 varA varB varC 5 10 2 27 1. varA = 5 2. varB = 10 3. varC = varB / varA
Background image of page 5

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
6 Parameters Act just like variables Are used for passing information in to a program Are only used between the brackets of a program name The receiving brackets hold the “formal parameter” The passing brackets hold the “actual parameter”
Background image of page 6
7 Formal vs. Actual def someProgram ( param1, param2, etc. ): program code goes here >>> someProgram (value1, value2, etc.) Formal parameter list Actual parameter list
Background image of page 7

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
8
Background image of page 8
9 As you can see, we can pass not only numbers, but also strings of text in to our programs. The last execution, shows that we can even pass in the values that other variables are holding!
Background image of page 9

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
10 Scope – not just a mouthwash! The idea of “Scope” in computer science, is simply – where does something exist (or where can it be accessed). For instance, a variable that is declared in programA, can not be seen by, or used by, programB In JES, variables declared in the program area can not be seen by, or used by, the command area. And vice- versa!
Background image of page 10
11 In this example, when we execute the program, it does not know the variable outerVar , since it was declared in the command area, and not the program area!
Background image of page 11

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
12 In this example, when we try to print innerVar in the command area, we get an error. This is because it was declared in the program, and not the command area!
Background image of page 12
While this idea of “Scope” may seem to be an annoyance, it does have its reasons! Scope essentially protects your programs from users accidentally writing over your stored information by using the same variable name you did. It also works the other way, protecting data outside your
Background image of page 13

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Image of page 14
This is the end of the preview. Sign up to access the rest of the document.

Page1 / 41

APCO 1P00 - Week 2 Lecture Slides - Week 2 More Variables...

This preview shows document pages 1 - 14. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online