Unit 07 - Unit07 ScopeandMemory. UnitObjectives

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

View Full Document Right Arrow Icon
Unit 07 Scope and Memory. Unit Objectives   Learn about scope, global and static variables Get under the hood and see where your data is (and why you need to know this) Understand the difference between an address and a pointer (and what they are in the first place. Create functions that use addresses and pointers.   Introduction   The fact is that after just 5 units, you already have the skills necessary to write some very useful  programs. Thanks to your understanding of controls structures and functions, the exercises (and problems  on the last unit test) have been fairly complicated.  And to be honest, I would be tempted to stop there -  except for one major topic we can't afford to ignore in engineering:  arrays . Arrays are fundamental in  order to store  vectors  and  matrices  in your programs and vectors and matrices are very important in  engineering. The next unit is all about arrays. This unit is all about the some the fundamental programming concepts  you will need to know in order to program effectively with arrays. Task 1 - What is  i  now? Just what is  i  if there are two or more variables named  i  in your program? This happens more often than  you think, especially if everyone is sharing all their functions with one another. The  scope  of a declared  variable defines the region (or regions) in a program where a certain variable can be used. Consider this  example similar to the one we learned in the previous unit. #include <iostream> using namespace std;
Background image of page 1

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

View Full DocumentRight Arrow Icon
int Add( int a, int b ) { int c = a + b; return c; } int main(void) { int c = 3, d = 4; cout << Add( c, d ) << endl; return 1; } Notice that both functions contain a variable 'c' and use the variable in different roles.  The  scope  (existence) of these two variables is within just the function where they are declared. Scope  can also exist inside control structures as seen in the next example. #include <iostream> using namespace std; int main(void) { for(int i=0; i<3; i++) cout << i << endl; for(int i=0; i<6; i=i+2) cout << i << endl; return 1; }
Background image of page 2
Believe it or not, this code will compile.  When a variable is declared its scope includes all control  structures contained within the structure in which it is declared, but not larger structures. For example, int main(void) { int x = 2; for(int i=1; i<5; i++) { x = x*i; } cout << x << endl; return 1; } The code above will compile, but the code below  won't . int main(void)
Background image of page 3

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

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

This note was uploaded on 10/02/2011 for the course ME 205 taught by Professor Koen during the Spring '07 term at University of Texas at Austin.

Page1 / 19

Unit 07 - Unit07 ScopeandMemory. UnitObjectives

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

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