13functions2_3014(2)

13functions2_3014(2) - Lecture File 13 COP 3014 February...

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

View Full Document Right Arrow Icon
Lecture File 13 COP 3014 February 17, 2010 A. Ford Tyson 1 COP 3014 Lecture File 13 ± Functions Part 2 –s t y l e example program grading cpp 1 – example program grading.cpp – incremental testing and debugging with functions – storage classes – introduction to function overloading and default arguments Copyright 1997-present, Ann Ford Tyson Famous Quotes in CS ± Re The Art of Computer Programming by Donald Knuth – Bill Gates: "If you think you're a really good Bill Gates: If you think you re a really good programmer… , read [Knuth's] Art of Computer Programming… " – Steve Jobs: "You should definitely send me a resume if you can read the whole thing." 2 Block Structure and Scope C++ is block-structured: { 3 <declarations>
Background image of page 1

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

View Full DocumentRight Arrow Icon
Lecture File 13 COP 3014 February 17, 2010 A. Ford Tyson 2 Declarations may occur both inside and outside of blocks #include <iostream> // outside all blocks using namespace std; void myfunc ( ); 4 const double PI = 3.14159; int main ( ) { int i, j, k; // inside main block } SCOPE: that portion of a program in which an identifier is accessible ± An identifier must be declared before it can be used in a program 5 ± The scope of an identifier is the block in which it is declared, unless the same name is re-declared in an enclosed block ( name precedence ) ± The same identifier can only be declared once within the same scope Local vs Global ± Local: an identifier declared in a block is local to that block; accessible only in that block – Function parameters and declarations inside functions are local 6 ± Global: an identifier declared outside of all functions is global; accessible in all lines following its declaration – Declarations in included header files are global – Function prototypes are global (as we use them)
Background image of page 2
Lecture File 13 COP 3014 February 17, 2010 A. Ford Tyson 3 namespace scope ± the scope of a variable declared inside a namespace is the body of the namespace ± e.g. namespace std 7 { … <declarations of cin, cout> } ± we extend the scope with a global using directive Scope: Generic Example #include <iostream> // globals using namespace std; const double PI = 3 14159 8 const double PI = 3.14159; int i; void doStuff ( ); double doCalc (double x); Scope: Generic Example p.2 int main ( ) { int j; 9 can access: global i (bad style), global PI (ok), local j, anything in iostream can call: doStuff, doCalc, any funcs in iostream } // note: functions can call themselves ("recursion")
Background image of page 3

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

View Full DocumentRight Arrow Icon
Lecture File 13 COP 3014 February 17, 2010 A. Ford Tyson 4 Scope: Generic Example p.3 void doStuff ( ) { can access: global i (bad style), global PI (ok) 10 anything in iostream can call: doCalc, any funcs in iostream, doStuff } Scope: Generic Example p.4 double doCalc (double x) { int i; 11 can access: global PI (ok), local i , param x, anything in iostream can call: doStuff, any funcs in iostream, doCalc
Background image of page 4
Image of page 5
This is the end of the preview. Sign up to access the rest of the document.

Page1 / 20

13functions2_3014(2) - Lecture File 13 COP 3014 February...

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

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