Unformatted text preview:

Using C++ FunctionsObjectivesSlide 3Using Functions and Include FilesSlide 5Slide 6Slide 7Slide 8Using Procedural AbstractionSlide 10Main() Function Employing Procedural AbstractionSlide 12Understanding ScopeDistinguishing Between Local and Global VariablesSlide 15Demonstrating ScopeSlide 17An Attempted Misuse of Variables Outside Their ScopeSlide 19Using the Scope Resolution OperatorSlide 21Slide 22Constructing Function Headers and PrototypesSlide 24Program That Prototypes DisplayLogo()Program That Calls askUserForInitial()Returning Values from FunctionsSlide 28Slide 29Slide 30Two Versions of findLarger()Passing Values to FunctionsSlide 33Slide 34Slide 35Program Listing for HoursAndRate3.cppUsing Classes and Objects as Arguments to Functions and as Return Types of FunctionsUsing the Customer Class with FunctionsSlide 39Passing Addresses to FunctionsSlide 41A Program That Calls Two Functions to Get Two ResultsA Program That Calls One Function to Get Two ResultsSlide 44Slide 45Using Reference Variables with FunctionsDeclaring Reference VariablesSlide 48Slide 49Passing Variable Addresses to Reference VariablesComparing Pointers and References in a Function HeaderSlide 52Using a Constant ReferenceSlide 54Passing Arrays to FunctionsPassing an Array to a FunctionSlide 57Inline FunctionsUsing an Inline FunctionSlide 60Slide 61Using Default ArgumentsSlide 63Examples of Legal and Illegal Use of Functions with Default ParametersOverloading FunctionsSlide 66Three Overloaded Functions That Perform Similar TasksSummarySlide 69Slide 70Slide 71Using C++ FunctionsObject-Oriented Programming Using C++Second Edition4Objectives•In this chapter, you will learn:•About using function•How to use procedural abstraction•About scope rules•How to construct function headers and prototypes•How to return values from, and pass values to, functions•How to use classes and objects as arguments to functions and as return types of functions4Objectives•In this chapter, you will learn:•How to pass addresses to functions•How to use reference variables•How to pass arrays to functions•How to use inline functions•How to use default arguments•How to overload functions4Using Functions and Include Files•Functions are modules that perform a task or group of tasks•In other programming languages, the counterpart to a function is known as a subroutine, procedure, or method•You can write new C++ functions, and you can use functions that other programmers have written•Any statement allowed in the main( ) function of a C++ program can be used in any other function4Using Functions and Include Files•In Figure 4-1, the statement displayLogo(); is known as the call to the function•When the call is made, control of the program is transferred to displayLogo() and statements written in displayLogo() execute•When displayLogo() is completed, control of the program returns to main(), which then proceeds to the next instruction4Using Functions and Include Files4Using Functions and Include Files•You are never required to place functions in their own files•The program file shown in Figure 4-4 compiles and executes in the same manner as the one shown in Figure 4-14Using Functions and Include Files•In the set of steps on pages 111 and 112 of the textbook, you write three functions—two functions that cannot execute alone, and the main() function that calls them4Using Procedural Abstraction•When you write functions that you include in other programs, you gain several benefits:–The functions can be written once and subsequently included in any number of programs–When you need to change the contents of the functions you write, you make the change in one location, and all the programs that use the function automatically receive the change–When you write a main() program, you can condense many related actions into as single function call, making the main() program easier to write and simpler to read–When you use functions that already have been written for you, you gain an additional benefit: you do not need to worry about how the function works; you simply call it and let it work for you4Using Procedural Abstraction•Using functions is one way to employ procedural abstraction in C++•When you write a main() function, you can use the names of other functions that perform a variety of tasks4Main() Function Employing Procedural Abstraction4Using Procedural Abstraction•Alter the myInfo() function you wrote in the last set of steps so that it includes additional information by using the processes outlined on pages 114 and 115 of the textbook4Understanding Scope•Some variables can be accessed throughout an entire program, while others can be accessed only in a limited part of the program•The scope of a variable defines where it can be accessed in a program•To adequately understand scope, you must be able to distinguish between local and global variables4Distinguishing Between Local and Global Variables•Celebrity names are global because they are known to people everywhere and always refer to those same celebrities•Global variables are those that are known to all functions in a program•Some named objects in your life are local•You might have a local co-worker whose name takes precedence over, or overrides, a global one4Distinguishing Between Local and Global Variables•Variables that are declared in a block are local to that block and have the following characteristics:–Local variables are created when they are declared within a block–Local variables are known only to that block–Local variables cease to exist when their block ends•Variables declared within a function remain local to that function •In contrast, variables declared within curly braces within any function are local to that block4Demonstrating Scope4Distinguishing Between Local and Global Variables•Programmers would say that c is accessible only within braces•No variable can be accessed outside its scope, that is, beyond the block in which it is declared•The program shown in Figure 4-10 demonstrates that you can declare variables prior to or anywhere within a function, but the program does not demonstrate good technique for declaring variables•C++ programmers usually declare most of their variables inside and at the beginning of the function that uses the variables4An Attempted Misuse of Variables Outside Their Scope4Distinguishing Between Local and Global Variables4Using the Scope Resolution


View Full Document

WPU CS 2300 - Using C++ Functions

Download Using C++ Functions
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view Using C++ Functions and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Using C++ Functions 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?