© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Tutorial 11 – Fundraiser Application: Introducing Scope.

Slides:



Advertisements
Similar presentations
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Tutorial 16 – Shopping List Application: Introducing.
Advertisements

© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Tutorial 10 – Enhancing the Wage Calculator Application:
© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Tutorial 13 – Salary Survey Application: Introducing.
C++ Programming: Program Design Including Data Structures, Third Edition Chapter 7: User-Defined Functions II.
Chapter 7: User-Defined Functions II
Chapter 7: User-Defined Functions II Instructor: Mohammad Mojaddam.
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Outline 14.1 Test-Driving the Fund Raiser Application.
© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Tutorial 4 – Introducing Algorithms, Pseudocode and.
© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Tutorial 14 – Student Grades Application: Introducing.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 5 - Functions Outline 5.1Introduction 5.2Program.
11.3 Function Prototypes A Function Prototype contains the function’s return type, name and parameter list Writing the function prototype is “declaring”
© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Tutorial 17 – Payroll Application: Introducing Inheritance.
© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Tutorial 8 - Interest Calculator Application: Introducing.
© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Tutorial 15 – Digital Clock Application: Building Your.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 15 - C++ As A "Better C" Outline 15.1Introduction.
 2003 Prentice Hall, Inc. All rights reserved. 1 Functions Modules: functions and classes Programs use new and “prepackaged” modules –New: programmer-defined.
C++ Functions CS242 COMPUTER PROGRAMMING T.Banan Al-Hadlaq.
© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Tutorial 7 – Class Average Application: Introducing.
© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Tutorial 9 – Income Tax Calculator Application: Introducing.
1 Functions Modules: functions and classes Programs use new and “prepackaged” modules –New: programmer-defined functions, classes –Prepackaged: from the.
© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Tutorial 6 – Car Payment Calculator Application: Introducing.
© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Tutorial 2 - Welcome Application: Introduction to C++
© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Tutorial 2 - HelloWorld Application: Introduction to.
C Functions Programmer-defined functions – Functions written by the programmer to define specific tasks. Functions are invoked by a function call. The.
© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Tutorial 8 - Interest Calculator Application: Introducing.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. C How To Program - 4th edition Deitels Class 05 University.
© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Tutorial 10 – Enhancing the Wage Calculator Application:
T U T O R I A L  2009 Pearson Education, Inc. All rights reserved Fund Raiser Application Introducing Scope, Pass-by-Reference and Option Strict.
© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Tutorial 4 – Wage Calculator Application: Introducing.
© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Tutorial 5 – Dental Payment Application: Introducing.
C++ Programming: From Problem Analysis to Program Design, Fifth Edition, Fifth Edition Chapter 7: User-Defined Functions II.
© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Tutorial 9 – Income Tax Calculator Application: Introducing.
 2005 Pearson Education, Inc. All rights reserved Methods: A Deeper Look.
© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Tutorial 6 – Car Payment Calculator Application: Introducing.
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Tutorial 17 – Flag Quiz Application Introducing One-Dimensional.
1 CISC181 Introduction to Computer Science Dr. McCoy Lecture 6 September 17, 2009.
Chapter 10 Introduction to Classes
Chapter 4: Subprograms Functions for Problem Solving Mr. Dave Clausen La Cañada High School.
Built-In and user-Defined functions Software Design Concepts Lecture IV Dr. Sothy Vignarajah.
Loops (cont.). Loop Statements  while statement  do statement  for statement while ( condition ) statement; do { statement list; } while ( condition.
© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Tutorial 7 – Class Average Application: Introducing.
Week 6: Functions - Part 2 BJ Furman 01OCT2012. The Plan for Today Comments on midterm exam (next week in lab!) Review of functions Scope of identifiers.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 5 - Functions Outline 5.1Introduction 5.2Program.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Functions Outline 5.1Introduction 5.2Program Modules.
C++ Programming Lecture 9 Functions – Part I By Ghada Al-Mashaqbeh The Hashemite University Computer Engineering Department.
Structure Programming Lecture 8 Chapter 5&6 - Function – part I 12 December 2015.
1 Lecture 14 Functions Functions with Empty Parameter Lists Empty parameter lists  void or leave parameter list empty  Indicates function takes.
1 COMS 261 Computer Science I Title: Functions Date: October 12, 2005 Lecture Number: 17.
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Outline 5.1 Test-Driving the Inventory Application.
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Tutorial 15 – Fund Raiser Application Introducing.
2 Objectives You should be able to describe: Object-Based Programming Classes Constructors Examples Common Programming Errors.
© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Tutorial 3 – Inventory Application: Introducing Variables,
Chapter 3 Functions. 2 Overview u 3.2 Using C++ functions  Passing arguments  Header files & libraries u Writing C++ functions  Prototype  Definition.
CS1201: PROGRAMMING LANGUAGE 2 FUNCTIONS. OVERVIEW What is a Function? Function Prototype Vs Decleration Highlight Some Errors in Function Code Parameters.
Functions Math library functions Function definition Function invocation Argument passing Scope of an variable Programming 1 DCT 1033.
C++ Programming Lecture 13 Functions – Part V The Hashemite University Computer Engineering Department (Adapted from the textbook slides)
Lecture 5 functions 1 © by Pearson Education, Inc. All Rights Reserved.
 2003 Prentice Hall, Inc. All rights reserved. 1 Chapter 3 - Functions Outline 3.15Functions with Empty Parameter Lists 3.16Inline Functions 3.17References.
 Static  Example for Static Field  Example for Static Method  Math class methods  Casting  Scope of Declaration  Method Overloading  Constructor.
 2000 Prentice Hall, Inc. All rights reserved Introduction Divide and conquer –Construct a program from smaller pieces or components –Each piece.
Part III © Copyright by Pearson Education, Inc. All Rights Reserved.
Introduction to Programming Lecture 6. Functions – Call by value – Call by reference Today's Lecture Includes.
Functions in C++ Top Down Design with Functions. Top-down Design Big picture first broken down into smaller pieces.
 2000 Prentice Hall, Inc. All rights reserved Program Components in C++ Function definitions –Only written once –These statements are hidden from.
FUNCTIONS (C) KHAERONI, M.SI. OBJECTIVE After this topic, students will be able to understand basic concept of user defined function in C++ to declare.
C++ Programming Lecture 13 Functions – Part V By Ghada Al-Mashaqbeh The Hashemite University Computer Engineering Department.
Chapter 9: Value-Returning Functions
Chapter 7: User-Defined Functions II
Functions Imran Rashid CTO at ManiWeber Technologies.
Presentation transcript:

© 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

© 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.

© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved Test-Driving the Fundraiser Application

© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved Test-Driving the Fundraiser Application (Cont.) Figure 11.1 Running the completed Fundraiser application. Figure 11.2 Fundraiser application with first donation entered.

© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved Test-Driving the Fundraiser Application (Cont.) Figure 11.3 Making further donations. Total of all donations (minus expenses)

© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved Constructing the Fundraiser Application

© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved 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

© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved Constructing the Fundraiser Application (Cont.) Figure 11.5 Defining a global variable in the application. Defining a global variable

© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved 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

© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved 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

© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved 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; }

© 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: %2Fparam_decl.htm %2Fparam_decl.htm

© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved 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.

© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved 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: Fparam_decl.htm

© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved 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: Fcplr312.htm

© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved Function Prototypes (Cont.) Figure 11.8 Value of donatedAmount converted to double to perform the calculation. Value of donatedAmount implicitly converted to double

© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved 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

© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved Function Prototypes (Cont.) Figure Displaying the donation amount after operating expenses are deducted. Displaying the donation amount after expenses are deducted

© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved Function Prototypes (Cont.) Figure Updating and displaying the total amount raised for charity. Displaying the total amount raised for charity Updating the global variable

© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved Function Prototypes (Cont.) Figure Prompting the user for the next donation. Prompting the user for and inputting the next donation Figure Running the completed application.

© 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

© 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

© 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

© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved Function Prototypes (Cont.) Figure 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 Attempting to access main ’s local variable grossDonation causes a compilation error.

© 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 Always write the question followed by the answer. Remember to highlight the answer. Exercises 11.11, 11.12, and the Programming Challenge For all exercises start with the provided templates. Due next Wednesday