Exposure C++ Chapter VIII Program Modularity and Functions.

Slides:



Advertisements
Similar presentations
1 Lecture 16:User-Definded function I Introduction to Computer Science Spring 2006.
Advertisements

C++ Basics March 10th. A C++ program //if necessary include headers //#include void main() { //variable declaration //read values input from user //computation.
Copyright © 2002 W. A. Tucker1 Chapter 3 Lecture Notes Bill Tucker Austin Community College COSC 1315.
Starting Out with C++: Early Objects 5/e © 2006 Pearson Education. All Rights Reserved Starting Out with C++: Early Objects 5 th Edition Chapter 6 Functions.
Computer Science 1620 Functions. Given a number n, the factorial of n, written n!, is computed as follows: note: 0! = 1 examples: n! = n x (n-1) x (n-2)
Your First C++ Program Aug 27, /27/08 CS 150 Introduction to Computer Science I C++  Based on the C programming language  One of today’s most.
Copyright © 2008 Pearson Addison-Wesley. All rights reserved. Chapter 12 Separate Compilation Namespaces Simple Make Files (Ignore all class references.
1 9/1/06CS150 Introduction to Computer Science 1 What Data Do We Have? CS 150 Introduction to Computer Science I.
1 September 6, 2005CS150 Introduction to Computer Science I What Actions Do We Have Part 1 CS150 Introduction to Computer Science I.
Chapter 6. 2 Objectives You should be able to describe: Function and Parameter Declarations Returning a Single Value Pass by Reference Variable Scope.
CS 201 Functions Debzani Deb.
Lesson 6 Functions Also called Methods CS 1 Lesson 6 -- John Cole1.
Guide To UNIX Using Linux Third Edition
Computer Science 1620 Lifetime & Scope. Variable Lifetime a variable's lifetime is finite Variable creation: memory is allocated to the variable occurs.
Introduction to Methods
C++ Basics CSci 107. A C++ program //include headers; these are modules that include functions that you may use in your //program; we will almost always.
Chapter 6: Functions.
 2003 Prentice Hall, Inc. All rights reserved. 1 Introduction to Classes and Objects Outline Introduction Classes, Objects, Member Functions and Data.
A First Book of C++: From Here To There, Third Edition2 Objectives You should be able to describe: Function and Parameter Declarations Returning a Single.
Exposure C++ Chapter VII Program Input and Output.
C++ Basics Structure of a Program. C++ Source Code Plain text file Typical file extension .CPP Must compile the C++ source code without errors before.
Copyright © 2012 Pearson Education, Inc. Chapter 6: Functions.
Chapter 06 (Part I) Functions and an Introduction to Recursion.
Project 1 Due Date: September 25 th Quiz 4 is due September 28 th Quiz 5 is due October2th 1.
CHAPTER 5 FUNCTIONS I NTRODUCTION T O C OMPUTER P ROGRAMMING (CSC425)
Functions Top-down design Breaking a complex problem into smaller parts that we can understand is a common practice. The process of subdividing a problem.
C++ Basics C++ is a high-level, general purpose, object-oriented programming language.
CPS120: Introduction to Computer Science Lecture 14 Functions.
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 6: User-Defined Functions I.
Chapter 6 User-Defined Functions I. Objectives Standard (predefined) functions What are they, and How to use them User-Defined Functions Value returning.
Built-In and user-Defined functions Software Design Concepts Lecture IV Dr. Sothy Vignarajah.
1 A simple C++ program // ======================================================= // File:helloworld.cpp // Author:Vana Doufexi // Date:1/4/2006 // Description:Displays.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 5 - Functions Outline 5.1Introduction 5.2Program.
1 CS161 Introduction to Computer Science Topic #9.
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.
KIC/Computer Programming & Problem Solving 1.  Introduction  Program Modules in C  Math Library Functions  Functions  Function Definitions  Function.
C++ Basics. Compilation What does compilation do? g++ hello.cpp g++ -o hello.cpp hello.
DOCUMENTATION SECTION GLOBAL DECLARATION SECTION
Chapter 3 Top-Down Design with Functions Part II J. H. Wang ( 王正豪 ), Ph. D. Assistant Professor Dept. Computer Science and Information Engineering National.
CS Class 03 Topics  Sequence statements Input Output Assignment  Expressions Read pages Read pages 40 – 49 for next time.
1 Brief Version of Starting Out with C++, 4th Brief Edition Chapter 6 Functions.
A FIRST BOOK OF C++ CHAPTER 6 MODULARITY USING FUNCTIONS.
Chapter Functions 6. Modular Programming 6.1 Modular Programming Modular programming: breaking a program up into smaller, manageable functions or modules.
1 MODULAR DESIGN AND ABSTRACTION. 2 SPECIFYING THE DETAILS OF A PROBLEM INTO A RELATED SET OF SMALLER PROBLEMS.
Chapter 3 Functions. 2 Overview u 3.2 Using C++ functions  Passing arguments  Header files & libraries u Writing C++ functions  Prototype  Definition.
Chapter 6 Functions. Topics Basics Basics Simplest functions Simplest functions Functions receiving data from a caller Functions receiving data from a.
Functions Chapter 6. Modular Programming Modular programming: breaking a program up into smaller, manageable functions or modules Function: a collection.
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 6: User-Defined Functions I.
PROGRAM ESSENTIALS. TOKENS  SMALLEST UNITS OF A PROGRAM LANGUAGE  Special Symbols  Mathematical Operators  Punctuation  Word Symbols  Key Words.
Chapter 9 Separate Compilation and Namespaces. Copyright © 2005 Pearson Addison-Wesley. All rights reserved. Slide 2 Overview Separate Compilation (9.1)
Copyright © 2008 Pearson Addison-Wesley. All rights reserved. Chapter 12 Separate Compilation and Namespaces.
A First Book of ANSI C Fourth Edition Chapter 6 Modularity Using Functions: Part I.
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 6: Functions.
Chapter 05 (Part II) Control Statements: Part II.
CSIS 113A Lecture 5 Functions. Introduction to Functions  Building Blocks of Programs  Other terminology in other languages:  Procedures, subprograms,
Introduction to Programming Lecture 6. Functions – Call by value – Call by reference Today's Lecture Includes.
Programming Fundamentals Enumerations and Functions.
Chapter 6 Functions. 6-2 Topics 6.1 Modular Programming 6.2 Defining and Calling Functions 6.3 Function Prototypes 6.4 Sending Data into a Function 6.5.
Functions Chapter 5. Function A set of instructions that are designed to perform specific task. A complete and independent program. It is executed by.
1 8/30/06CS150 Introduction to Computer Science 1 Your First C++ Program.
Functions Procedural Abstraction Flow of Control INFSY 307 Spring 2003 Lecture 4.
Basic concepts of C++ Presented by Prof. Satyajit De
CSC113: Computer Programming (Theory = 03, Lab = 01)
Functions Declarations CSCI 230
User Defined Functions
6 Chapter Functions.
A First Book of ANSI C Fourth Edition
Chapter 6: User-Defined Functions I
C++ Basics CSci 107. A C++ program //include headers; these are modules that include functions that you may use in your //program; we will almost always.
Presentation transcript:

Exposure C++ Chapter VIII Program Modularity and Functions

Divide and Conquer with Modules Divide a program into modules that focus on a singular task that is manageable. Regardless of size, continue to divide any program segment that is too complex until a manageable task is reached. A popular computer science saying says: One task --- one module Ideally one program module should print on one piece of paper. If a module becomes too large, divide it into smaller modules.

Function Components void Example() // function heading {// start of function body cout << endl; cout << "Hello There"; }// end of function body

Local and Global Variables A local variable is defined inside a function and can only be accessed inside that function. A global variable is defined outside all the functions and can be used anywhere in the program after the variable definition.

Using Global and Local Variables Use a global variable when the variable needs to be used in more than one function. Use a local variable when the variable only needs to be used in one function.

Prototype Notes Prototypes allow functions to be called without any concern about the order that the functions have been declared. Prototypes keep the main function at the beginning of the program, since the complete functions (implementations) can now be placed below the main function. Remember that a prototype is a function heading with a semi-colon at the end. By convention function prototypes are placed before the main function, and function implementations are placed below the main function body.

Program Development with Stubs We have now arrived at a very critical topic in computer science. This topic of stubs is embraced by many students and they soon see the benefits. On the other hand, I have seen students who simply refuse to use this wonderful tool. The tool you are about to learn makes program writing quicker because you will have so little trouble identifying error locations. There is a real irony in this topic. Actually it is not an irony, it is a paradox. Everything you see will appear as if you are taking longer to write a program. Many students reject anything that prolongs the program writing process. But this is only an illusion. It only appears like you take longer, and in reality you save a lot of time.

Stub Definition A stub is a function without any program statements // Stub Example void EnterData( ) { }

// PROG0809.CPP // Using Stubs for program development // Step 1, starts with the main function and include // statements. This step does not compile yet. #include void main() { EnterData(); ProcessData(); DisplayData(); } PROG0809.CPP OUTPUT There is no output This program does not compile.

// PROG0810.CPP // Using Stubs for Program Development // Step 2, writing prototypes // This program compiles, but it will not link. #include void EnterData(); void ProcessData(); void DisplayData(); void main() { EnterData(); ProcessData(); DisplayData(); } PROG0810.CPP OUTPUT There is no output yet. This program compiles, but it does not link.

// PROG0811.CPP // Using Stubs for Program Development // Step 3, writing stubs for each prototype // This step compiles and links. #include void EnterData(); void ProcessData(); void DisplayData(); void main() { EnterData(); ProcessData(); DisplayData(); } void EnterData() { } void ProcessData() { } void DisplayData() { } STUB FUNCTIONS PROG0811.CPP OUTPUT This program shows no output. It compiles, and links, but the stubs are all empty.

// PROG0812.CPP // Using Stubs for Program Development Step 4, creates "glorified stubs" // which helps to showthe global execution sequence of a program. void EnterData() { cout << endl << endl; cout << "ENTER DATA FUNCTION" << endl; } void ProcessData() { cout << endl << endl; cout << "PROCESS DATA FUNCTION" << endl; } void DisplayData() { cout << endl << endl; cout << "DISPLAY DATA FUNCTION" << endl; } PROG0812.CPP OUTPUT ENTER DATA FUNCTION PROCESS DATA FUNCTION DISPLAY DATA FUNCTION

// PROG0813.CPP // Step 5 completes the EnterData function // This step also defines global variables for EnterData. double PayRate; double HoursWorked; void EnterData() { cout << endl; cout << "ENTER DATA FUNCTION" << endl; cout << endl; cout > "; cin >> PayRate; cout > "; cin >> HoursWorked; } PROG0813.CPP OUTPUT ENTER DATA FUNCTION ENTER YOUR HOURLY PAYRATE ===>> 7.75 ENTER YOUR WEEKLY HOURS ===>> 25 PROCESS DATA FUNCTION DISPLAY DATA FUNCTION

// PROG0814.CPP // Step 6 completes the ProcessData function. // This step also defines additional global variables for ProcessData. double PayRate; double HoursWorked; double GrossPay; double Deductions; double NetPay; void ProcessData() { cout << endl; cout << "PROCESS DATA FUNCTION" << endl; GrossPay = PayRate * HoursWorked; Deductions = GrossPay * 0.28; NetPay = GrossPay - Deductions; } PROG0814.CPP OUTPUT ENTER DATA FUNCTION ENTER YOUR HOURLY PAYRATE ===>> 7.75 ENTER YOUR WEEKLY HOURS ===>> 25 PROCESS DATA FUNCTION DISPLAY DATA FUNCTION

// PROG0815.CPP // Step 7 completes the DisplayData function. void DisplayData() { cout << endl; cout << "DISPLAY DATA FUNCTION" << endl; cout << endl; cout << setiosflags(ios::fixed); cout << setiosflags(ios::showpoint); cout << setprecision(2); cout << "GROSSPAY: " << GrossPay << endl; cout << "DEDUCTIONS: " << Deductions << endl; cout << "NETPAY: " << NetPay << endl ; }

Final Output of Stub Example PROG0814.CPP OUTPUT ENTER DATA FUNCTION ENTER YOUR HOURLY PAYRATE ===>> 7.75 ENTER YOUR WEEKLY HOURS ===>> 25 PROCESS DATA FUNCTION DISPLAY DATA FUNCTION GROSSPAY: DEDUCTIONS: NETPAY:

Using the Main Function Properly // PROG0816.CPP // This program demonstrates modular programming with // only function calls in the program's main function body. void main() { Execution(); EnterData(); ComputeMean(); DisplayData(); Terminate(); } Always start with the Main Function

Modular Programming and the Main Function The main program function needs to consist of a sequence of function calls, which represent the major program modules. Function prototypes should be used and placed before the main function.

// PROG0817.CPP // This program demonstrate the use of a frequently used function. #include void Skip4(); void Continue(); void EnterData(); void ProcessData(); void DisplayData(); void main() { EnterData(); ProcessData(); DisplayData(); } void Skip4() { cout << endl << endl << endl << endl; } Note that function Skip4 is not used in the main function body. It is not one of the main program modules. However, it is conveniently used by various functions.

void Continue() { cout << endl << endl; cout to Continue"; getch(); } void EnterData() { Skip4(); cout << "ENTER DATA FUNCTION" << endl; Continue(); } void ProcessData() { Skip4(); cout << "PROCESS DATA FUNCTION" << endl; Continue(); } void DisplayData() { Skip4(); cout << "DISPLAY DATA FUNCTION" << endl; Continue(); } PROG0817.CPP OUTPUT ENTER DATA FUNCTION Press to Contunue PROCESS DATA FUNCTION Press <Enter to Contunue DISPLAY DATA FUNCTION Press to Continue

// PROG0820.CPP // This program demonstrates using an include file. #include #include "PROG0820.INC" void main() { One(); Two(); } // PROG0820.INC // This file is included by program PROG0820.CPP void One() { cout << endl << endl; cout << "THIS IS PROCEDURE ONE" << endl; } void Two() { cout << endl << endl; cout << "THIS IS PROCEDURE TWO" << endl; }

Include File Placement Rule Include files are compiled at the exact location of the include preprocessor. Normally, the include statement is at the top of the program. However, it can be placed anywhere that a file needs to be included. User-created include files require quotes like: #include "UTILITY.INC" File names are not case-sensitive. They can be lower case, upper case, or a combination. The convention at Berkner High School is to use UPPER CASE for user- created include files.

// PROG0821.CPP // This program demonstrates using a utility library of // of frequently used functions. #include "UTILITY.INC" int Nr1,Nr2,Nr3; // three numbers to be averaged float Mean; // average of the three numbers void EnterData(); void ComputeMean(); void DisplayData(); void main() { OutputSelection(); Execution(); EnterData(); ComputeMean(); DisplayData(); Terminate(); } << This statement include the function library

Header and Implementation Files Function libraries are divided into two separate files: Header files, which stores all the function heading proto- types and other include libraries. Implementation files, which stores the details of the complete function implementations The header file ends with.H and has a name like UTILITY.H and includes the implementation file. The implementation uses the same identifier as the header file and has a name like UTILITY.CPP.

// PROG0822.CPP // This program demonstrates how to use the utility library // by including the header file, which in turn includes the // implementation file. #include "UTILITY.H" int Nr1,Nr2,Nr3; // three numbers to be averaged float Mean; // average of the three numbers void EnterData(); void ComputeMean(); void DisplayData(); void main() { OutputSelection(); Execution(); EnterData(); ComputeMean(); DisplayData(); Terminate(); } The Complete Utility Library UTILITY.H file & UTILITY.CPP file are Shown on computer and in your book