C++ for Engineers and Scientists Second Edition Chapter 6 Modularity Using Functions.

Slides:



Advertisements
Similar presentations
Etter/Ingber Engineering Problem Solving with C Fundamental Concepts Chapter 4 Modular Programming with Functions.
Advertisements

C++ Programming: Program Design Including Data Structures, Third Edition Chapter 7: User-Defined Functions II.
C++ Programming: From Problem Analysis to Program Design, Third Edition Chapter 7: User-Defined Functions II.
Chapter 7: User-Defined Functions II
C++ Programming: Program Design Including Data Structures, Third Edition Chapter 7: User-Defined Functions II.
Chapter 7: User-Defined Functions II Instructor: Mohammad Mojaddam.
Chapter 5 C Functions The best way to develop and maintain a large program is to divide it into several smaller program modules, each of which is more.
Chapter 14: Overloading and Templates C++ Programming: Program Design Including Data Structures, Fifth Edition.
An Introduction to Programming with C++ Fifth Edition
Chapter 14: Overloading and Templates
Chapter 7 - Functions. Functions u Code group that performs single task u Specification refers to what goes into and out of function u Design refers to.
C++ Programming: From Problem Analysis to Program Design, Second Edition Chapter 7: User-Defined Functions II.
 2000 Prentice Hall, Inc. All rights reserved. Chapter 5 - Functions Outline 5.1Introduction 5.2Program Modules in C 5.3Math Library Functions 5.4Functions.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 5 - Functions Outline 5.1Introduction 5.2Program.
Chapter 6. 2 Objectives You should be able to describe: Function and Parameter Declarations Returning a Single Value Pass by Reference Variable Scope.
 2003 Prentice Hall, Inc. All rights reserved. 1 Functions Modules: functions and classes Programs use new and “prepackaged” modules –New: programmer-defined.
C Lecture Notes Functions (Cont...). C Lecture Notes 5.8Calling Functions: Call by Value and Call by Reference Used when invoking functions Call by value.
Lesson 6 Functions Also called Methods CS 1 Lesson 6 -- John Cole1.
Chapter 15: Operator Overloading
1 Functions Modules: functions and classes Programs use new and “prepackaged” modules –New: programmer-defined functions, classes –Prepackaged: from the.
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 14: Overloading and Templates.
Chapter 12: Adding Functionality to Your Classes.
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 15: Overloading and Templates.
C++ for Engineers and Scientists Third Edition
A First Book of ANSI C Fourth Edition Chapter 6 Modularity Using Functions: Part I.
C Functions Programmer-defined functions – Functions written by the programmer to define specific tasks. Functions are invoked by a function call. The.
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.
Chapter 6: Modularity Using Functions. In this chapter, you will learn about: – Function and parameter declarations – Returning a single value – Returning.
© 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 © 2012 Pearson Education, Inc. Chapter 6: Functions.
Chapter 6: User-Defined Functions
C++ Programming: From Problem Analysis to Program Design, Fifth Edition, Fifth Edition Chapter 7: User-Defined Functions II.
Copyright © 2012 Pearson Education, Inc. Chapter 6: Functions.
Chapter 10 Introduction to Classes
Chapter 11: Introduction to Classes. In this chapter you will learn about: – Classes – Basic class functions – Adding class functions – A case study involving.
Engineering Problem Solving with C Fundamental Concepts Chapter 4 Modular Programming with Functions.
#include using namespace std; // Declare a function. void check(int, double, double); int main() { check(1, 2.3, 4.56); check(7, 8.9, 10.11); } void check(int.
Chapter 10: Classes and Data Abstraction. Objectives In this chapter, you will: Learn about classes Learn about private, protected, and public members.
A FIRST BOOK OF C++ CHAPTER 6 MODULARITY USING FUNCTIONS.
C++ Programming: From Problem Analysis to Program Design, Third Edition Chapter 15: Overloading and Templates.
Copyright 2003 Scott/Jones Publishing Standard Version of Starting Out with C++, 4th Brief Edition Chapter 6 Functions.
Functions Math library functions Function definition Function invocation Argument passing Scope of an variable Programming 1 DCT 1033.
1 MORE ON MODULAR DESIGN: MODULE COMMUNICATIONS. 2 WHEN A FUNCTION IS INVOKED, MEMORY IS ALLOCATED LOCALLY FOR THE FORMAL PARAMETERS AND THE VALUE OF.
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.
+ Storage Classes and Linkage. + Introduction Scope describe the region or regions of a program that can access and identifier Variables can be shared.
Chapter 10: Classes and Data Abstraction. Classes Object-oriented design (OOD): a problem solving methodology Objects: components of a solution Class:
2Object-Oriented Program Development Using C++ 3 Function Declarations and Definitions Analogize functions to methods –Share same basic features –Unlike.
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.
Chapter 7 - Functions. Functions u Code group that performs single task u Specification refers to what goes into and out of function u Design refers to.
Building Programs from Existing Information Solutions for programs often can be developed from previously solved problems. Data requirements and solution.
A First Book of ANSI C Fourth Edition
1 Chapter 8 Scope, Lifetime, and More on Functions CS185/09 - Introduction to Programming Caldwell College.
Chapter 6 Modularity Using Functions
C++ for Engineers and Scientists Second Edition Chapter 12 Pointers.
BIL 104E Introduction to Scientific and Engineering Computing Lecture 4.
Chapter 9: Value-Returning Functions
Functions.
Chapter 7: User-Defined Functions II
Functions and an Introduction to Recursion
Chapter 6: Functions Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley.
User-Defined Functions
C++ for Engineers and Scientists Second Edition
Chapter 5 - Functions Outline 5.1 Introduction
CHAPTER 6 GENERAL-PURPOSE METHODS
A First Book of ANSI C Fourth Edition
Chapter 7: User-Defined Functions II
Chapter 9: Value-Returning Functions
6 Functions.
Presentation transcript:

C++ for Engineers and Scientists Second Edition Chapter 6 Modularity Using Functions

C++ for Engineers and Scientists, Second Edition2 Objectives Function and Parameter Declarations Returning a Single Value Returning Multiple Values Applications Variable Scope

C++ for Engineers and Scientists, Second Edition3 Objectives (continued) Variable Storage Category Common Programming Errors

C++ for Engineers and Scientists, Second Edition4 Function and Parameter Declarations A function is called by using function’s name and passing data as arguments Figure 6.1 Calling and passing data to a function.

C++ for Engineers and Scientists, Second Edition5 Function and Parameter Declarations (continued) Function prototype: declaration statement for a function; it specifies –Type of value that will be returned (if any) –Data type and order of arguments that must be supplied when calling it Syntax: returnDataType functionName(list of argument data types);

C++ for Engineers and Scientists, Second Edition6 Function and Parameter Declarations (continued) Function prototype allows –Error checking of data types by compiler –Conversion of all arguments passed in to the required data type When a function is called, the arguments must be passed in the correct order, type, and number

C++ for Engineers and Scientists, Second Edition7 Function and Parameter Declarations (continued) Figure 6.3 findMax() receives actual values.

C++ for Engineers and Scientists, Second Edition8 Function and Parameter Declarations (continued) Function declaration consists of two parts: –Function header: identifies the data type of the return value, and specifies number, type, and order of arguments –Function body: set of statements that operates on the data passed in as arguments Arguments: also called formal parameters

C++ for Engineers and Scientists, Second Edition9 Function and Parameter Declarations (continued) Figure 6.5 Storing values into parameters.

C++ for Engineers and Scientists, Second Edition10 Function and Parameter Declarations (continued) Function body: constant and variable declarations, and statements, contained in braces Syntax: { symbolic constant declarations, variable declarations, and other C++ statements }

C++ for Engineers and Scientists, Second Edition11 Function and Parameter Declarations (continued) Stub: placeholder for a function not yet written Stubbed functions allow testing of main() function logic without all of the details A function may be declared with no arguments; it is invoked simply by its name Default argument values may be specified for use if a value is not passed in when function is invoked

C++ for Engineers and Scientists, Second Edition12 Function and Parameter Declarations (continued) Rules for using default parameters: –Default values should be assigned in the function prototype –All parameters after one with a default value must also be assigned default values –When calling the function, if one argument is omitted, all arguments to its right must also be omitted –Default value may use constants and previously declared variables that will yield a value

C++ for Engineers and Scientists, Second Edition13 Function and Parameter Declarations (continued) Function overloading: using the same function name for more than one function Data types for overloaded versions of a function must be different to allow compiler to determine which function to use Function template: a single, complete function that serves as a model for a family of functions Arguments to a function template do not specify data type

C++ for Engineers and Scientists, Second Edition14 Function and Parameter Declarations (continued) Template prefix: informs the compiler that the function following the prefix is a template When the function is called, the compiler constructs a function from the template, using the data type of the passed-in argument Function templates can have multiple arguments

C++ for Engineers and Scientists, Second Edition15 Function and Parameter Declarations (continued)

C++ for Engineers and Scientists, Second Edition16 Returning a Single Value Passed by value: when a function receives only a copy of the data passed in as the argument When passed by value, the function cannot alter the actual variable containing the data A function can directly return only one value, although it can receive many values

C++ for Engineers and Scientists, Second Edition17 Returning a Single Value (continued) return statement: causes the value to be returned to the calling function Syntax:return expression; return statement should use data type specified in function header Calling function must assign the called function to a variable of the same data type as the called function’s return type

C++ for Engineers and Scientists, Second Edition18 Returning a Single Value (continued)

C++ for Engineers and Scientists, Second Edition19 Returning a Single Value (continued) When a function is called, a stack region for the function must be built by the operating system If function is small and not called often, the overhead of calling this function may not be efficient inline function: a function whose code is placed by the compiler directly inline at the point at which it is called inline function must be placed ahead of any calls to it

C++ for Engineers and Scientists, Second Edition20 Returning a Single Value (continued)

C++ for Engineers and Scientists, Second Edition21 Returning Multiple Values Pass by reference: a called function is given the address of a variable as an argument; allows the called function to directly alter the variable’s value C++ has two types of address parameters: –References –Pointers

C++ for Engineers and Scientists, Second Edition22 Returning Multiple Values (continued) Reference parameter declaration in function header uses the addressof operator Syntax:dataType& referenceName Figure 6.8 The equivalence of arguments and parameters in Program 6.8.

C++ for Engineers and Scientists, Second Edition23 Returning Multiple Values (continued)

C++ for Engineers and Scientists, Second Edition24 Returning Multiple Values (continued) Ability to change actual variables provides a means to “return” multiple values Function may contain a mix of by value and by reference arguments Reference arguments must be variables, not constants Function call does not indicate that reference arguments are used in the called function Reference parameters should be used only in restricted situations that require multiple return values

C++ for Engineers and Scientists, Second Edition25 Applications Problem-Solver Algorithm consists of three tasks, each of which can be implemented as a function: –Get inputs –Calculate result –Display result Rectangular to Polar Coordinate Conversion: –Convert rectangular (x,y) coordinates to polar form (r, theta)

C++ for Engineers and Scientists, Second Edition26 Applications (continued) Simulation: –Simulate the tossing of a coin to determine the number of resulting heads and tails –Pseudorandom numbers: numbers that are sufficiently random (the order cannot be predicted) –rand() function generates pseudorandom numbers; srand() sets the initial random “seed” value –Scaling: adjusting the range of numbers

C++ for Engineers and Scientists, Second Edition27 Variable Scope Local variables: variables declared within a function Such variables have local scope; they can be used only within the function in which they are declared Global variables: variables declared outside any function Such variables have global scope; they can be used by all functions that occur after their declaration

C++ for Engineers and Scientists, Second Edition28 Variable Scope (continued)

C++ for Engineers and Scientists, Second Edition29 Variable Scope (continued) Figure 6.16 The three storage areas created by Program 6.17.

C++ for Engineers and Scientists, Second Edition30 Variable Scope (continued) Scope resolution: –If a local and global variable both have the same name, all references to the variable name within the scope of the local variable refer to the local variable –To force the use of the global variable, use the resolution operator ( :: )

C++ for Engineers and Scientists, Second Edition31 Variable Scope (continued)

C++ for Engineers and Scientists, Second Edition32 Variable Scope (continued)

C++ for Engineers and Scientists, Second Edition33 Variable Scope (continued) Use of global variables and constants should be restricted to only those that must be shared among several functions Function prototypes, however, typically are global

C++ for Engineers and Scientists, Second Edition34 Variable Storage Category Lifetime of a variable: the timeframe within which a variable exists (has memory storage) Storage category of a variable: determines where and how long the storage location is kept Four storage categories: –auto –static –extern –register

C++ for Engineers and Scientists, Second Edition35 Variable Storage Category (continued) Storage category placed before the data type in the declaration Syntax: storageCategory dataType variableName; Local variables can be members of only the auto, static, or register storage classes Default storage class is auto auto: local variable storage is created on entrance to the function, and released on exit

C++ for Engineers and Scientists, Second Edition36 Variable Storage Category (continued)

C++ for Engineers and Scientists, Second Edition37 Variable Storage Category (continued) static : once created, local static variables remain and retain their values for the life of the program Static variables are created and initialized during compilation Static variables can be initialized by constants or constant expressions only; they are set to zero if no explicit initialization value is given

C++ for Engineers and Scientists, Second Edition38 Variable Storage Category (continued)

C++ for Engineers and Scientists, Second Edition39 Variable Storage Category (continued) register : variables have the same time duration as auto, but are stored in registers, not normal memory storage register storage can increase performance, but is platform dependent; compilers may switch register storage variables to auto storage Cannot use the address operator & with register storage; registers do not have standard memory addresses

C++ for Engineers and Scientists, Second Edition40 Variable Storage Category (continued) Global variables cannot be declared as auto or register, but may be static or extern static and extern affect only the scope, not the time duration, of the variable extern : extends the scope of a global variable; a global variable declared in one file may be used by another file extern declaration does not cause the creation of a variable by reserving new storage

C++ for Engineers and Scientists, Second Edition41 Variable Storage Category (continued) Figure 6.18 A program may extend beyond one file.

C++ for Engineers and Scientists, Second Edition42 Variable Storage Category (continued) Global static variables cannot be extended to a second file using the extern declaration Global static variables are declared in the same way as local static variables, but outside of any functions

C++ for Engineers and Scientists, Second Edition43 Common Programming Errors Passing incorrect data types to a function Assuming a change to a local variable in the called function also changes another local variable of the same name in the calling function Assuming a change to a local variable also changes a global variable of the same name Omitting the called function’s prototype before or within the calling function

C++ for Engineers and Scientists, Second Edition44 Common Programming Errors (continued) Terminating function’s header line with a semicolon (creating a null statement) Omitting data type for function parameters in header line

C++ for Engineers and Scientists, Second Edition45 Summary A function is called by its name, passing arguments in parentheses following the name Return type: the data type that the function will return when called A function can directly return only one value By default, all arguments are passed by value: a copy of the data is given to the function Reference arguments pass the address of a variable to the function for direct use

C++ for Engineers and Scientists, Second Edition46 Summary (continued) Function prototype provides a declaration for a function prior to its actual implementation Scope of a variable determines where in the program the variable can be used: –Local scope: usable only in the function in which it is declared –Global scope: usable throughout the program

C++ for Engineers and Scientists, Second Edition47 Summary (continued) Storage category of a variable determines how long the value will be retained: –auto : variable is created when the function is entered and destroyed when the function is exited –register : stored in a computer’s internal registers instead of memory –static : variable retains its values for the duration of the program