{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

APCO 1P00 - Week 2 Lecture Slides

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

This preview shows pages 1–15. Sign up to view the full content.

1 Week 2 More Variables, Sounds,       Loops, and Arrays! Some slides are based on  Mark Guzdial’s  at Georgia Tech

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

View Full Document
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!
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

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

View Full Document
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 .
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

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

View Full Document
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”
7 Formal vs. Actual def someProgram ( param1, param2, etc. ): program code goes here >>> someProgram (value1, value2, etc.) Formal parameter list Actual parameter list

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

View Full Document
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!

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

View Full Document
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!
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!

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

View Full Document
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!
13 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 program from being overwritten.

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

View Full Document
14 Passing data in and out We pass information in to a program through the use of parameters (also called arguments in the text book).
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

### Page1 / 41

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

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

View Full Document
Ask a homework question - tutors are online