16-Scope-n-Array (1).pptx - CMPSC 201 Lecture 16 Scope and...

Info icon This preview shows pages 1–9. Sign up to view the full content.

View Full Document Right Arrow Icon
CMPSC 201 Lecture 16 Scope and Arrays as Arguments Book 5.3, 8.2 (some slides adapted from Jenn Plasterr)
Image of page 1

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

View Full Document Right Arrow Icon
Objectives Review the concept of variable scopes Be able to pass array elements to functions Be able to pass the entire array to functions Be able to use const to make an array read-only after passing to functions Martin K.-C. Yeh 2
Image of page 2
Scope of Variables A variable that is declared within a block is “local” to that block and may only be accessed within that block. Therefore, a variable declared in a function definition (either heading or body) is local to that function. Several functions may use the same identifiers as variable names, but each is stored in a different memory space (remember call stack?). Global variables are declared outside all functions and may be accessed from anywhere. Do not use in this course – except for const! Martin K.-C. Yeh 3
Image of page 3

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

View Full Document Right Arrow Icon
Scope Rules 1. A name that is declared local to a block is available in that block. 2. All other names are inherited from the immediately surrounding region const int x = 35; const double PI = 3.14159265358979; int main() { double x; x = 9.7; cout << x << endl; // what is printed? return 0; } Martin K.-C. Yeh 4
Image of page 4
Scope Rules 9.7 is printed in the previous example The local variable x in main() is used (printed) instead of the global variable x When a local variable and a global variable share an identical name, the local variable is always the one that is being used. Martin K.-C. Yeh 5
Image of page 5

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

View Full Document Right Arrow Icon
Common Exam Question What output is produced by the following code: int number = 2; { int number = 10; cout << number << " "; } cout << number; a. 2 10 b. 10 2 c. 2 2 d. 10 10 e. None of the above. Martin K.-C. Yeh 6
Image of page 6
const int BOUND = 100; double x; int p( double ); int k( int ); int main() { double m = 14.6; for (int x=0; x<BOUND; ++x ) { int z; z = k( x ); if ( z == 0 ) { cout << x << endl; } } cout << p(m) << endl; return 0; } int p(double k) { x = k – 0.5; int i = k * 3; return i; } int k(int m) { return ( m % 10 ); } Martin K.-C. Yeh 7
Image of page 7

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

View Full Document Right Arrow Icon
Name Visible in main (excluding for loop) Visible in main’s for loop Visible in Function p Visible in Function k BOUND (global const) Yes Yes Yes Yes x (global var) Yes No Yes Yes p (function) Yes Yes Yes Yes k (function) Yes Yes No Yes m (var in main) Yes Yes No No x (loop counter) No Yes No No z No Yes No No k (param for p) No No Yes
Image of page 8
Image of page 9
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

What students are saying

  • Left Quote Icon

    As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

    Student Picture

    Kiran Temple University Fox School of Business ‘17, Course Hero Intern

  • Left Quote Icon

    I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern