Presentation is loading. Please wait.

Presentation is loading. Please wait.

Bellevue University CIS 205: Introduction to Programming Using C++ Lecture 7: Methods.

Similar presentations


Presentation on theme: "Bellevue University CIS 205: Introduction to Programming Using C++ Lecture 7: Methods."— Presentation transcript:

1 Bellevue University CIS 205: Introduction to Programming Using C++ Lecture 7: Methods

2 Today We Are Going To: Introduce the use of methods in C++ programs Introduce the use of methods in C++ programs Examine the components of a method signature: return type, name, parameter list Examine the components of a method signature: return type, name, parameter list Place return statements intelligently to simplify the operation of a method Place return statements intelligently to simplify the operation of a method Identify the importance of variable scope Identify the importance of variable scope

3 Topic Methods

4 Methods Experience has taught programmers to break large, complex problems into smaller, simpler ones Experience has taught programmers to break large, complex problems into smaller, simpler ones In fact, support for those efforts is a defining characteristic of object-oriented languages In fact, support for those efforts is a defining characteristic of object-oriented languages Key tool: method or function Key tool: method or function

5 What is a Method? A method encapsulates a related set of actions so that they are treated as one A method encapsulates a related set of actions so that they are treated as one That code is physically separated from the rest of the code; it is written and tested in isolation That code is physically separated from the rest of the code; it is written and tested in isolation A method is also an essential member of a class; it is a part of the description of a class, defining its capabilities A method is also an essential member of a class; it is a part of the description of a class, defining its capabilities

6 Creating a Method Identify candidate segments of code Identify candidate segments of code Actions you describe when laying out the structure of your program in comments Actions you describe when laying out the structure of your program in comments Actions that are performed more than once Actions that are performed more than once Identify the information which is required to perform the action Identify the information which is required to perform the action Describe the result Describe the result If necessary, reorganize program If necessary, reorganize program

7 Method Signature Compiler identifies a method with its signature, which must be unique Compiler identifies a method with its signature, which must be unique The signature hides the implementation from callers The signature hides the implementation from callers Consists of name and parameters Consists of name and parameters Return type and modifiers are also important, but they do not help to distinguish one method from another (this causes a compiler error) Return type and modifiers are also important, but they do not help to distinguish one method from another (this causes a compiler error)

8 Topic Returning From a Method

9 At method invocation, control over the program’s execution passes to it At method invocation, control over the program’s execution passes to it A return statement is used to give control back to the caller A return statement is used to give control back to the caller The return statement identifies the value which is to be provided to the caller The return statement identifies the value which is to be provided to the caller A method may include several return statements A method may include several return statements

10 Topic Multiple Return Values

11 Value and Variable Parameters By default the values passed into a method are passed by value By default the values passed into a method are passed by value Meaning: value copied to a new variable Meaning: value copied to a new variable ==> original remains unchanged ==> only one value is returned If necessary can modify parameter so that it is passed by reference If necessary can modify parameter so that it is passed by reference ==> allows method to modify value

12 Issues and Oddities Passing by reference has benefits Passing by reference has benefits ==> slight performance advantage ==> allows more than one return value Has some dangers Has some dangers ==> caller not guaranteed to get value back Possible to pass by constant reference Possible to pass by constant reference ==> keeps performance advantage ==> guarantees caller gets value back

13 Topic Method Placement

14 Compilation Order C++ requires that methods be declared before they are invoked C++ requires that methods be declared before they are invoked To satisfy this we will place our methods BEFORE the main method To satisfy this we will place our methods BEFORE the main method Another way to fulfill this requirement is to use a “forward declaration” Another way to fulfill this requirement is to use a “forward declaration” This may improve readability, but for the sake of simplicity we will implement first This may improve readability, but for the sake of simplicity we will implement first

15 Topic Example

16 Speeding Example Go back to our speeding example: any good method candidates? Go back to our speeding example: any good method candidates? Let’s try the prompt loop Let’s try the prompt loop Return type: Boolean Return type: Boolean Required information: none Required information: none

17 First Notes The code inside the method is not ready, so comment it out for now The code inside the method is not ready, so comment it out for now Must return a value that matches the return type specified in the method signature; for now just return false so that the program will stop Must return a value that matches the return type specified in the method signature; for now just return false so that the program will stop

18 Topic Variable Scope

19 Modifying Logic to Use the Method Before we can make this method work we must discuss the concept of scope Before we can make this method work we must discuss the concept of scope Defines where a variable may be used Defines where a variable may be used A variable is visible to blocks nested inside the one in which the variable is declared A variable is visible to blocks nested inside the one in which the variable is declared A variable is not visible to blocks outside A variable is not visible to blocks outside Note this applies to all blocks – loops and decisions Note this applies to all blocks – loops and decisions

20 Local Variables, First Cut Method uses response – not in scope Method uses response – not in scope newFine is not used within the method; that is the value that is returned newFine is not used within the method; that is the value that is returned NOW, note that we can modify the method so that we return the result as soon as it is known NOW, note that we can modify the method so that we return the result as soon as it is known Is reprompt necessary? Is reprompt necessary?

21 Local Variables, Second Cut The answer is no The answer is no The return statements within the switch make it so that reprompt is never used The return statements within the switch make it so that reprompt is never used Delete those in favor of while(true) Delete those in favor of while(true)

22 Today We Have: Added a method to a previously-written program Added a method to a previously-written program Studied method signatures: return type, name, parameter list Studied method signatures: return type, name, parameter list Placed return statements intelligently to simplify the operation of a method Placed return statements intelligently to simplify the operation of a method Identified the importance of variable scope Identified the importance of variable scope


Download ppt "Bellevue University CIS 205: Introduction to Programming Using C++ Lecture 7: Methods."

Similar presentations


Ads by Google