Download presentation
Presentation is loading. Please wait.
Published byMary Gallagher Modified over 9 years ago
1
© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Tutorial 11 – Fundraiser Application: Introducing Scope and Function Prototypes Outline 11.1 Test-Driving the Fundraiser Application 11.2 Constructing the Fundraiser Application 11.3 Function Prototypes 11.4 Wrap-Up
2
© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Objectives In this tutorial, you will learn to: –Create variables that can be used in all the application’s functions. –Use argument coercion to implicitly convert the value of an argument to a function. –Use function prototypes to validate your function calls.
3
© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 11.1 Test-Driving the Fundraiser Application
4
© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 11.1 Test-Driving the Fundraiser Application (Cont.) Figure 11.1 Running the completed Fundraiser application. Figure 11.2 Fundraiser application with first donation entered.
5
© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 11.1 Test-Driving the Fundraiser Application (Cont.) Figure 11.3 Making further donations. Total of all donations (minus expenses)
6
© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 11.2 Constructing the Fundraiser Application
7
© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 11.2 Constructing the Fundraiser Application (Cont.) Scope is the portion of the application where a variable can be accessed Global variables have file scope –File scope is from the variable declaration to the end of the file –Should be avoided (i.e., never used) to prevent subtle logic errors or used as a constant Local variables have block scope –Block scope is from the variable declaration to the end of the block (the closing right brace) –Local variables hide any global variables with the same name while in scope
8
© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 11.2 Constructing the Fundraiser Application (Cont.) Figure 11.5 Defining a global variable in the application. Defining a global variable
9
© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 11.2 Constructing the Fundraiser Application (Cont.) Figure 11.6 Defining a local variable in the Fundraiser application. Definition of a local variable is the beginning of the variable’s scope Right brace ends the scope of a local variable
10
© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 11.2 Constructing the Fundraiser Application (Cont.) Figure 11.7 Defining a second local variable in the Fundraiser application. Definition of netDonation is the beginning of the variable’s scope Right brace ends netDonation ’s scope
11
© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 11.3 Function Prototypes A Function Prototype contains the function’s return type, name and parameter list –Writing the function prototype is “declaring” the function. float square (float x); –In a function declaration, or prototype, specifying an identifier (parameter name) is optional. Thus, the following example is legal in a function declaration. float square (float); –Writing the function body is “defining” the function float square (float x){ return x * x; }
12
© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Function Declarations and Definitions The distinction between a function declaration and function definition is similar to that of a data declaration and definition. The declaration establishes the names and characteristics of a function but does not allocate storage for it. Thus, the identifier declared in this example: float square(float x);// do not allocate storage The function definition contains a function declaration and the body of a function. The body is a block of statements that perform the work of the function. The identifiers declared in this example allocate storage. float square(float x) { return x*x; } Declarations are typically placed in header files, while definitions appear in source files. Source: http://publib.boulder.ibm.com/infocenter/comphelp/v8v101/index.jsp?topic=%2Fcom.ibm.xlcpp8a.doc%2Flanguage%2Fref %2Fparam_decl.htm http://publib.boulder.ibm.com/infocenter/comphelp/v8v101/index.jsp?topic=%2Fcom.ibm.xlcpp8a.doc%2Flanguage%2Fref %2Fparam_decl.htm
13
© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 11.3 Function Prototypes (Cont.) Function prototype allows the compiler to validate the function call. A prototype is not necessary if a function is defined before it is called (the header acts as the prototype). Argument coercion occurs when arguments do not match the data types specified in the prototype. float square (float); int main() { cout << square( 4 ) } In the example, the function prototype forces the compiler to convert the integer argument 4 to a float value 4.0 before the value is passed to square.
14
© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 11.3 Function Prototypes (Cont.) In C++, the parameter list of a function is referred to as its signature. The name and signature of a function uniquely identify it. Your textbook defines the function signature as the function name and parameter list. As the word itself suggests, the function signature is used by the compiler to distinguish among the different instances of overloaded functions. Source: http://publib.boulder.ibm.com/infocenter/comphelp/v8v101/index.jsp?topic=%2Fcom.ibm.xlcpp8a.doc%2Flanguage%2Fref%2 Fparam_decl.htm
15
© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 11.3 Function Prototypes (Cont.) Consider a function print, which displays an int. void print(int i) { cout << " Here is int " << i << endl; } You can overload the function print to display other types, for example, double. void print(double f) { cout << " Here is float " << f << endl; } Here we have two functions with the same name, each performing a similar operation on a different data type. int main() { print(10); print(10.10); } For this to work, the signature (parameter list = number of parameters and data type of each) of the function must be different. Source: http://publib.boulder.ibm.com/infocenter/comphelp/v8v101/index.jsp?topic=%2Fcom.ibm.xlcpp8a.doc%2Flanguage%2Fref%2 Fcplr312.htm
16
© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 11.3 Function Prototypes (Cont.) Figure 11.8 Value of donatedAmount converted to double to perform the calculation. Value of donatedAmount implicitly converted to double
17
© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 11.3 Function Prototypes (Cont.) Function prototype errors –Forgetting the semicolon after a function prototype is a syntax error –Invoking a function before the function prototype has been declared or the function has been defined is a syntax error Figure 11.9 Declaring a function prototype for the calculateDonation function. Declaring a function prototype
18
© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 11.3 Function Prototypes (Cont.) Figure 11.10 Displaying the donation amount after operating expenses are deducted. Displaying the donation amount after expenses are deducted
19
© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 11.3 Function Prototypes (Cont.) Figure 11.11 Updating and displaying the total amount raised for charity. Displaying the total amount raised for charity Updating the global variable
20
© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 11.3 Function Prototypes (Cont.) Figure 11.12 Prompting the user for the next donation. Prompting the user for and inputting the next donation Figure 11.13 Running the completed application.
21
© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Fundraiser.cpp (1 of 3) Definition of a local variable is the beginning of the variable’s scope Define a global variable Declare a function prototype
22
© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Fundraiser.cpp (2 of 3) Definition of netDonation is the beginning of the variable’s scope Display the donation amount after expenses are deducted Update global variable
23
© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Fundraiser.cpp (3 of 3) Right brace ends netDonation ’s scope Right brace ends the scope of the local variables Value of donatedAmount implicitly converted to double
24
© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 11.3 Function Prototypes (Cont.) Figure 11.15 Attempting to access main ’s local variable grossDonation when it is out of scope. Trying to access the grossDonation local variable, which is out of scope Figure 11.16 Attempting to access main ’s local variable grossDonation causes a compilation error.
25
© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Lab and Homework Assignment Tutorial 11 − Interest Calculator Application. Turn in annotated source file with your own comments. Answer and Turn-in Tutorial 11 Questions 11.1 to 11.10. Always write the question followed by the answer. Remember to highlight the answer. Exercises 11.11, 11.12, and the Programming Challenge 11.16. For all exercises start with the provided templates. Due next Wednesday
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.