more on functions
play

More on Functions Chapter 10 1 2 Global Variables Prefer Locals - PDF document

3/16/2009 For Next Time Read Chapter 10 More on Functions Chapter 10 1 2 Global Variables Prefer Locals Space for local variables occupied only when Local variables: declared within functions the function is executing


  1. 3/16/2009 For Next Time  Read Chapter 10 More on Functions Chapter 10 1 2 Global Variables Prefer Locals  Space for local variables occupied only when  Local variables: declared within functions the function is executing  “Local” to that function  Local variables in one function are completely  Scope is limited to function independent of local variables in another function (they cannot interact or influence each  Names do not conflict with any other variables other across function boundaries)  Global variables: declared outside any function  Local variables “start fresh” each time the  “Global” to the file and/or program function is called  Scope is entire file and/or program  A function that uses no global variables can be  Declared static : scope limited to file used as is in other programs 3 4 Locals Hide Globals Locals vs. Globals  If a local variable has the same name as a  Locals are uninitialized global variable, the local variable is the one  Globals are automatically initialized to used within the function “zero”  To access the like-named global, use the scope  Numbers are 0 resolution operator, ::  Booleans are false  ::x  Other types (to be seen) are “zero -ish ”  The compiler resolves names as follows  If it is declared locally, it’s local  If it is not declared within the function, it checks for global declaration  If it is not local and it is not global, it is undefined 5 6 1

  2. 3/16/2009 Static Locals Static Globals  If a local variable is qualified as static , it lives  If a global variable is qualified as static , its between function invocations scope is limited to the file in which it is declared  The static qualifier renders a variable “file  Its value is retained between function calls local”  Space for static locals is allocated when the  Functions can also be declared static program begins executing and is deallocated when the program terminates  Such functions cannot be used outside of the file in which they are declared 7 8 Function Signature Function Overloading  A function’s signature consists of its name and  C++ allows definition of multiple functions with types of parameters the same name  A function’s signature can be determined from  Two functions within the same program that its prototype and/or its definition have the same name are said to be overloaded  void f(int x, double y) { … }  Examples:  void f(int, double);  void f(int x, double y) { … }  A function’s return type is not included in a  void f(int ) { … } function’s signature  Overloaded functions must have different signatures 9 10 Factorial C++ Recursive Factorial  One definition of the mathematical factorial int factorial(int n) function: { if ( n == 0 ) return 1;  Another, recursive, definition: else return n * factorial(n – 1); } 11 12 2

  3. 3/16/2009 How it Works How it Works factorial(6) = ? factorial(6) = 6 * factorial(5) 13 14 How it Works How it Works factorial(6) = 6 * factorial(5) factorial(6) = 6 * factorial(5) = 6 * 5 * factorial(4) = 6 * 5 * factorial(4) = 6 * 5 * 4 * factorial(3) 15 16 How it Works How it Works factorial(6) = 6 * factorial(5) factorial(6) = 6 * factorial(5) = 6 * 5 * factorial(4) = 6 * 5 * factorial(4) = 6 * 5 * 4 * factorial(3) = 6 * 5 * 4 * factorial(3) = 6 * 5 * 4 * 3 * factorial(2) = 6 * 5 * 4 * 3 * factorial(2) = 6 * 5 * 4 * 3 * 2 * factorial(1) 17 18 3

  4. 3/16/2009 How it Works How it Works factorial(6) = 6 * factorial(5) factorial(6) = 6 * factorial(5) = 6 * 5 * factorial(4) = 6 * 5 * factorial(4) = 6 * 5 * 4 * factorial(3) = 6 * 5 * 4 * factorial(3) = 6 * 5 * 4 * 3 * factorial(2) = 6 * 5 * 4 * 3 * factorial(2) = 6 * 5 * 4 * 3 * 2 * factorial(1) = 6 * 5 * 4 * 3 * 2 * factorial(1) = 6 * 5 * 4 * 3 * 2 * 1 * factorial(0) = 6 * 5 * 4 * 3 * 2 * 1 * factorial(0) = 6 * 5 * 4 * 3 * 2 * 1 * 1 19 20 How it Works How it Works factorial(6) = 6 * factorial(5) factorial(6) = 6 * factorial(5) = 6 * 5 * factorial(4) = 6 * 5 * factorial(4) = 6 * 5 * 4 * factorial(3) = 6 * 5 * 4 * factorial(3) = 6 * 5 * 4 * 3 * factorial(2) = 6 * 5 * 4 * 3 * factorial(2) = 6 * 5 * 4 * 3 * 2 * factorial(1) = 6 * 5 * 4 * 3 * 2 * factorial(1) = 6 * 5 * 4 * 3 * 2 * 1 * factorial(0) = 6 * 5 * 4 * 3 * 2 * 1 * factorial(0) = 6 * 5 * 4 * 3 * 2 * 1 * 1 = 6 * 5 * 4 * 3 * 2 * 1 * 1 = 6 * 5 * 4 * 3 * 2 * 1 = 6 * 5 * 4 * 3 * 2 * 1 21 22 How it Works How it Works factorial(6) = 6 * factorial(5) factorial(6) = 6 * factorial(5) = 6 * 5 * factorial(4) = 6 * 5 * factorial(4) = 6 * 5 * 4 * factorial(3) = 6 * 5 * 4 * factorial(3) = 6 * 5 * 4 * 3 * factorial(2) = 6 * 5 * 4 * 3 * factorial(2) = 6 * 5 * 4 * 3 * 2 * factorial(1) = 6 * 5 * 4 * 3 * 2 * factorial(1) = 6 * 5 * 4 * 3 * 2 * 1 * factorial(0) = 6 * 5 * 4 * 3 * 2 * 1 * factorial(0) = 6 * 5 * 4 * 3 * 2 * 1 * 1 = 6 * 5 * 4 * 3 * 2 * 1 * 1 = 6 * 5 * 4 * 3 * 2 * 1 = 6 * 5 * 4 * 3 * 2 * 1 = 6 * 5 * 4 * 3 * 2 = 6 * 5 * 4 * 3 * 2 23 24 4

  5. 3/16/2009 How it Works How it Works factorial(6) = 6 * factorial(5) factorial(6) = 6 * factorial(5) = 6 * 5 * factorial(4) = 6 * 5 * factorial(4) = 6 * 5 * 4 * factorial(3) = 6 * 5 * 4 * factorial(3) = 6 * 5 * 4 * 3 * factorial(2) = 6 * 5 * 4 * 3 * factorial(2) = 6 * 5 * 4 * 3 * 2 * factorial(1) = 6 * 5 * 4 * 3 * 2 * factorial(1) = 6 * 5 * 4 * 3 * 2 * 1 * factorial(0) = 6 * 5 * 4 * 3 * 2 * 1 * factorial(0) = 6 * 5 * 4 * 3 * 2 * 1 * 1 = 6 * 5 * 4 * 3 * 2 * 1 * 1 = 6 * 5 * 4 * 3 * 2 * 1 = 6 * 5 * 4 * 3 * 2 * 1 = 6 * 5 * 4 * 3 * 2 = 6 * 5 * 4 * 3 * 2 = 6 * 5 * 4 * 6 = 6 * 5 * 4 * 6 25 26 How it Works How it Works factorial(6) = 6 * factorial(5) = 6 * 5 * factorial(4) factorial(6) = 6 * factorial(5) = 6 * 5 * 4 * factorial(3) = 6 * 5 * factorial(4) = 6 * 5 * 4 * 3 * factorial(2) = 6 * 5 * 4 * factorial(3) = 6 * 5 * 4 * 3 * 2 * factorial(1) = 6 * 5 * 4 * 3 * factorial(2) = 6 * 5 * 4 * 3 * 2 * 1 * factorial(0) = 6 * 5 * 4 * 3 * 2 * factorial(1) = 6 * 5 * 4 * 3 * 2 * 1 * 1 = 6 * 5 * 4 * 3 * 2 * 1 * factorial(0) = 6 * 5 * 4 * 3 * 2 * 1 = 6 * 5 * 4 * 3 * 2 * 1 * 1 = 6 * 5 * 4 * 3 * 2 = 6 * 5 * 4 * 3 * 2 * 1 = 6 * 5 * 4 * 6 = 6 * 5 * 4 * 3 * 2 = 6 * 5 * 24 = 6 * 5 * 4 * 6 = 6 * 5 * 24 27 28 How it Works How it Works factorial(6) = 6 * factorial(5) factorial(6) = 6 * factorial(5) = 6 * 5 * factorial(4) = 6 * 5 * factorial(4) = 6 * 5 * 4 * factorial(3) = 6 * 5 * 4 * factorial(3) = 6 * 5 * 4 * 3 * factorial(2) = 6 * 5 * 4 * 3 * factorial(2) = 6 * 5 * 4 * 3 * 2 * factorial(1) = 6 * 5 * 4 * 3 * 2 * factorial(1) = 6 * 5 * 4 * 3 * 2 * 1 * factorial(0) = 6 * 5 * 4 * 3 * 2 * 1 * factorial(0) = 6 * 5 * 4 * 3 * 2 * 1 * 1 = 6 * 5 * 4 * 3 * 2 * 1 * 1 = 6 * 5 * 4 * 3 * 2 * 1 = 6 * 5 * 4 * 3 * 2 * 1 = 6 * 5 * 4 * 3 * 2 = 6 * 5 * 4 * 3 * 2 = 6 * 5 * 4 * 6 = 6 * 5 * 4 * 6 = 6 * 5 * 24 = 6 * 120 29 30 5

  6. 3/16/2009 How it Works How it Works factorial(6) = 6 * factorial(5) factorial(6) = 6 * factorial(5) = 6 * 5 * factorial(4) = 6 * 5 * factorial(4) = 6 * 5 * 4 * factorial(3) = 6 * 5 * 4 * factorial(3) = 6 * 5 * 4 * 3 * factorial(2) = 6 * 5 * 4 * 3 * factorial(2) = 6 * 5 * 4 * 3 * 2 * factorial(1) = 6 * 5 * 4 * 3 * 2 * factorial(1) = 6 * 5 * 4 * 3 * 2 * 1 * factorial(0) = 6 * 5 * 4 * 3 * 2 * 1 * factorial(0) = 6 * 5 * 4 * 3 * 2 * 1 * 1 = 6 * 5 * 4 * 3 * 2 * 1 * 1 = 6 * 5 * 4 * 3 * 2 * 1 = 6 * 5 * 4 * 3 * 2 * 1 = 6 * 5 * 4 * 3 * 2 = 6 * 5 * 4 * 3 * 2 = 6 * 5 * 4 * 6 = 6 * 5 * 4 * 6 = 6 * 120 = 6 * 120 = 720 31 32 Correct Recursion Making Functions Reusable A correct recursive function must  Commercial C++ programs often consist of  Call itself within its definition hundreds of separate C++ files that are compiled  This is the recursive case separately and linked together to make the  Provide a way that it does not call itself executable file  This is the base case  A function defined in one file may be used by code  Provide some sort of conditional construct to in many other files select between the recursive and base cases  A function must have exactly one definition, in one  if/else or switch statement file  Each recursive call must move the execution  Since each file has to compiled separately, how closer to the base case can we use the same function in multiple files?  Otherwise infinite recursion (stack overflow) will result 33 34 Interface vs. Implementation Variables  Put the function’s prototype in a .h header file All variables have a int x = 3;  #include the header file in all files that use the  name function  type  Define the function in one .cpp file to be linked in 3  value with all the other compiled files  The .h header file specifies the function’s  location in memory interface  The .cpp source file provides the function’s To this point we have not been concerned about the variable’s memory location implementation 35 36 6

Download Presentation
Download Policy: The content available on the website is offered to you 'AS IS' for your personal information and use only. It cannot be commercialized, licensed, or distributed on other websites without prior consent from the author. To download a presentation, simply click this link. If you encounter any difficulties during the download process, it's possible that the publisher has removed the file from their server.

Recommend


More recommend