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

Slides:



Advertisements
Similar presentations
Programming Logic and Design Fourth Edition, Introductory
Advertisements

Chapter 5 Functions.
Road Map Introduction to object oriented programming. Classes
1 Programming for Engineers in Python Autumn Lecture 5: Object Oriented Programming.
Chapter 8 (Control Structure) Slide 1 Control Structures Control structures are used by the programmer to incorporate the desired sequence of execution.
1 Introduction to Computers and Programming Quick Review What is a Function? A module of code that performs a specific job.
Bellevue University CIS 205: Introduction to Programming Using C++ Lecture 6: Loop Control Structures.
Creating Packages. 2 home back first prev next last What Will I Learn? Describe the reasons for using a package Describe the two components of a package:
1 Introduction to C++ Programming Concept Basic C++ C++ Extension from C.
 2008 Pearson Education, Inc. All rights reserved JavaScript: Control Statements II.
C++ Object Oriented 1. Class and Object The main purpose of C++ programming is to add object orientation to the C programming language and classes are.
METHODS Introduction to Systems Programming - COMP 1005, 1405 Instructor : Behnam Hajian
Chapter 8 More Object Concepts
Beginning C++ Through Game Programming, Second Edition by Michael Dawson.
© The McGraw-Hill Companies, 2006 Chapter 4 Implementing methods.
Programming in Java Unit 2. Class and variable declaration A class is best thought of as a template from which objects are created. You can create many.
ASP.NET Programming with C# and SQL Server First Edition Chapter 3 Using Functions, Methods, and Control Structures.
Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Programming Logic & Design First Edition by Tony Gaddis.
CSCI-383 Object-Oriented Programming & Design Lecture 13.
Introduction to Programming Using C Modularity. 2 Contents Modularity Functions Preprocessor Comments Global variables.
Lecture Set 11 Creating and Using Classes Part B – Class Features – Constructors, Methods, Fields, Properties, Shared Data.
SE: CHAPTER 7 Writing The Program
An Object-Oriented Approach to Programming Logic and Design Fourth Edition Chapter 6 Using Methods.
CPS120: Introduction to Computer Science Decision Making in Programs.
CPS120: Introduction to Computer Science Functions.
ADTs and C++ Classes Classes and Members Constructors The header file and the implementation file Classes and Parameters Operator Overloading.
CPS120: Introduction to Computer Science Lecture 14 Functions.
An Object-Oriented Approach to Programming Logic and Design Chapter 3 Using Methods and Parameters.
Branches and Program Design
Chapter 3 Functions, Events, and Control Structures JavaScript, Third Edition.
3. Controlling Program Flow Methods, parameters, and return values Boolean expressions Conditional branching Loops.
Liang, Introduction to C++ Programming, (c) 2010 Pearson Education, Inc. All rights reserved Chapter 6 Advanced Function Features.
Covenant College November 27, Laura Broussard, Ph.D. Professor COS 131: Computing for Engineers Chapter 5: Functions.
I Power Higher Computing Software Development High Level Language Constructs.
1 CSCD 326 Data Structures I Software Design. 2 The Software Life Cycle 1. Specification 2. Design 3. Risk Analysis 4. Verification 5. Coding 6. Testing.
JavaScript, Fourth Edition
1 Methods Introduction to Methods Passing Arguments to a Method More About Local Variables Returning a Value from a Method Problem Solving with Methods.
CSC 212 Object-Oriented Programming and Java Part 2.
Chapter 10: Classes and Data Abstraction. Objectives In this chapter, you will: Learn about classes Learn about private, protected, and public members.
L what are predefined functions? l what is? n function name n argument(s) n return value n function call n function invocation n nested function call l.
 Control Flow statements ◦ Selection statements ◦ Iteration statements ◦ Jump statements.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved Chapter 5 Methods.
Chapter 5 Methods 1. Motivations Method : groups statements that perform a function.  Level of abstraction (black box)  Code Reuse – no need to reinvent.
ITERATION. Iteration Computers are often used to automate repetitive tasks. Repeating identical or similar tasks without making errors is something that.
Chapter 1 Java Programming Review. Introduction Java is platform-independent, meaning that you can write a program once and run it anywhere. Java programs.
Chapter 10: Classes and Data Abstraction. Classes Object-oriented design (OOD): a problem solving methodology Objects: components of a solution Class:
1 Introduction to Object Oriented Programming Chapter 10.
Lecture 4 – Function (Part 1) FTMK, UTeM – Sem /2014.
COP 2220 Computer Science I Topics –Breaking Problems Down –Functions –User-defined Functions –Calling Functions –Variable Scope Lecture 4.
JAVA Programming (Session 2) “When you are willing to make sacrifices for a great cause, you will never be alone.” Instructor: รัฐภูมิ เถื่อนถนอม
4 - Conditional Control Structures CHAPTER 4. Introduction A Program is usually not limited to a linear sequence of instructions. In real life, a programme.
1 Chapter 8 Scope, Lifetime, and More on Functions CS185/09 - Introduction to Programming Caldwell College.
BIL 104E Introduction to Scientific and Engineering Computing Lecture 4.
Predefined Functions Revisited
Java Programming: Guided Learning with Early Objects
Chapter 3: Using Methods, Classes, and Objects
About the Presentations
Functions Inputs Output
Starting Out with Programming Logic & Design
Chapter 6 Methods: A Deeper Look
Chapter 4 void Functions
6 Chapter Functions.
Group Status Project Status.
Dr. Bhargavi Dept of CS CHRIST
Week 4 Lecture-2 Chapter 6 (Methods).
Starting Out with Programming Logic & Design
Based on slides created by Bjarne Stroustrup & Tony Gaddis
Predefined Functions Revisited
Controlling Program Flow
Corresponds with Chapter 5
Presentation transcript:

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

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

Topic Methods

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

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

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

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)

Topic Returning From a Method

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

Topic Multiple Return Values

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

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

Topic Method Placement

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

Topic Example

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

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

Topic Variable Scope

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

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?

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)

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