Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "C++ for Engineers and Scientists Second Edition Chapter 6 Modularity Using Functions."— Presentation transcript:

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

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

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

4 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.

5 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);

6 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

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

8 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

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

10 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 }

11 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

12 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

13 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

14 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

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

16 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

17 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

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

19 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

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

21 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

22 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.

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

24 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

25 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)

26 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

27 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

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

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

30 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 ( :: )

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

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

33 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

34 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

35 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

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

37 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

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

39 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

40 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

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

42 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

43 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

44 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

45 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

46 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

47 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


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

Similar presentations


Ads by Google