SlideShare a Scribd company logo
1 of 50
1
C++ Programming: From Problem Analysis to Program Design, Eighth Edition
Chapter 6
User-Defined Functions
2
Objectives (1 of 2)
• In this chapter, you will:
• Learn about standard (predefined) functions and discover how to use them in a
program
• Learn about user-defined functions
• Examine value-returning functions , including actual and formal parameters
• Explore how to construct and use a value-returning, user-defined function in a
program
• Learn about function prototypes
• Learn how to construct and use void functions
© 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service
or otherwise on a password-protected website for classroom
3
Objectives (2 of 2)
• Discover the difference between value and reference parameters
• Explore reference parameters and value-returning functions
• Learn about the scope of an identifier
• Examine the difference between local and global identifiers
• Discover static variables
• Learn how to debug programs using drivers and stubs
• Learn function overloading
• Explore functions with default parameters
© 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service
or otherwise on a password-protected website for classroom
4
Introduction
• Functions allow complicated programs to be divided into manageable pieces
• Functions are often called modules
• They are like miniature programs that can be combined to form larger programs
© 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service
or otherwise on a password-protected website for classroom
5
Predefined Functions (1 of 2)
• In C++, a function is similar to that of a function in algebra
• It has a name
• It does some computation
• Some of the predefined mathematical functions are:
pow(x, y)
sqrt(x)
floor(x)
© 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service
or otherwise on a password-protected website for classroom
6
Predefined Functions (2 of 2)
• Predefined functions are organized into separate libraries
• I/O functions are in iostream header
• Math functions are in cmath header
• To use predefined functions, you must include the header file using an
include statement
• See Table 6-1 in the text for some common predefined functions
© 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service
or otherwise on a password-protected website for classroom
7
User-Defined Functions
• Value-returning functions have a return type
• Return a value of a specific data type using the return statement
• Void functions do not have a return type
• Do not use a return statement to return a value
© 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service
or otherwise on a password-protected website for classroom
8
Value-Returning Functions (1 of 3)
• To use these functions, you must:
• Include the appropriate header file in your program using the include statement
• Know the following items:
- Name of the function
- Number of parameters, if any
- Data type of each parameter
- Data type of the value returned, called the type of the function
© 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service
or otherwise on a password-protected website for classroom
9
Value-Returning Functions (2 of 3)
• Can use the value returned by a value-returning function by:
• Saving it for further calculation
• Using it in some calculation
• Printing it
• A value-returning function is used:
• In an assignment
• As a parameter in a function call
• In an output statement
© 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service
or otherwise on a password-protected website for classroom
10
Value-Returning Functions (3 of 3)
• Heading (or function header): the first line of the function
• Example: int abs(int number)
• The body is the function’s code that accomplishes the task
• Formal parameter: a variable declared in the heading
• Example: number
• Actual parameter: a variable or expression listed in a call to a function
• Example: x = pow(u, v)
© 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service
or otherwise on a password-protected website for classroom
11
Syntax: Value-Returning Function
• Syntax
• functionType is also called the data type or return type
© 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service
or otherwise on a password-protected website for classroom
12
Syntax: Formal Parameter List
FIGURE 6-1 Various parts of the function abs
© 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service
or otherwise on a password-protected website for classroom
13
Function Call
• Syntax to call a value-returning function
© 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service
or otherwise on a password-protected website for classroom
14
Syntax: Actual Parameter List
• The syntax of the actual parameter list is:
• The formal parameter list can be empty
• A call to a value-returning function with an empty formal parameter list is:
© 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service
or otherwise on a password-protected website for classroom
15
return Statement
• A function returns its value via the return statement
• It passes this value outside the function
© 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service
or otherwise on a password-protected website for classroom
16
Syntax: return Statement (1 of 2)
• The return statement has this syntax:
• In C++, return is a reserved word
• When a return statement executes
• The function immediately terminates
• Control goes back to the caller
• When a return statement executes in the function main, the program
terminates
© 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service
or otherwise on a password-protected website for classroom
17
Syntax: return Statement (2 of 2)
FIGURE 6-2 Various parts of the function larger
© 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service
or otherwise on a password-protected website for classroom
18
Function Prototype
• A function prototype is the function heading without the body of the function
• The general syntax of the function prototype of a value-returning function is:
• It is not necessary to specify the variable name in the parameter list
• The data type of each parameter must be specified
© 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service
or otherwise on a password-protected website for classroom
19
Value-Returning Functions: Some Peculiarities (1 of 2)
int secret(int x)
{
if (x > 5) //Line 1
return 2 * x; //Line 2
}
A correct definition of the function secret is:
int secret(int x)
{
if (x > 5) //Line 1
return 2 * x; //Line 2
return x; //Line 3
}
© 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service
or otherwise on a password-protected website for classroom
20
Value-Returning Functions: Some Peculiarities (2 of 2)
• Examples pointing out that the return statement only returns one value
return x, y; //only the value of y will be returned
int funcRet1()
{
int x = 45;
return 23, x; //only the value of x is returned
}
int funcRet2(int z)
{
int a = 2;
int b = 3;
return 2 * a + b, z + b;
//only the value of z + b is returned
}
© 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service
or otherwise on a password-protected website for classroom
21
More Examples of Value-Returning Functions (1 of 2)
char courseGrade(int score)
{
switch (score / 10)
{
case 0:
case 1:
case 2:
case 3:
case 4:
case 5:
return 'F';
case 6:
return 'D';
case 7:
return 'C';
case 8:
return 'B';
case 9:
case 10:
return 'A';
}
}
© 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service
or otherwise on a password-protected website for classroom
22
More Examples of Value-Returning Functions (2 of 2)
• In addition to Example 6-3 courseGrade, other examples are given in the
text
• Example 6-4 (rolling a pair of dice)
• Example 6-5 (Fibonacci number)
• Example 6-6 (palindrome)
• Example 6-7 (cable company)
© 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service
or otherwise on a password-protected website for classroom
23
Flow of Compilation and Execution (1 of 2)
• Execution always begins at the first statement in the function main
• Other functions are executed only when called
• Function prototypes appear before any function definition
• The compiler translates these first
• The compiler can then correctly translate a function call
© 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service
or otherwise on a password-protected website for classroom
24
Flow of Compilation and Execution (2 of 2)
• A function call transfers control to the first statement in the body of the called
function
• When the end of a called function is executed, control is passed back to the
point immediately following the function call
• A function’s returned value replaces the function call statement
© 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service
or otherwise on a password-protected website for classroom
25
Void Functions (1 of 4)
• User-defined void functions can be placed either before or after the function
main
• If user-defined void functions are placed after the function main
• The function prototype must be placed before the function main
• A void function does not have a return type
• A return statement without any value is typically used to exit the function early
© 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service
or otherwise on a password-protected website for classroom
26
Void Functions (2 of 4)
• Formal parameters are optional
• A call to a void function is a stand-alone statement
• The function definition of void functions with parameters has the following
syntax:
© 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service
or otherwise on a password-protected website for classroom
27
Void Functions (3 of 4)
• Formal parameter list syntax
• Function call syntax
• Actual parameter list syntax
© 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service
or otherwise on a password-protected website for classroom
28
Void Functions (4 of 4)
• Two types of formal parameters
• Value parameter: a formal parameter that receives a copy of the content of
corresponding actual parameter
• Reference parameter: a formal parameter that receives the location (memory
address) of the corresponding actual parameter
© 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service
or otherwise on a password-protected website for classroom
29
Value Parameters
• If a formal parameter is a value parameter, the value of the corresponding
actual parameter is copied into it
• A formal parameter has its own copy of the data
• During program execution, a formal parameter manipulates the data stored in
its own memory space
© 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service
or otherwise on a password-protected website for classroom
30
Reference Variables as Parameters (1 of 2)
• If a formal parameter is a reference parameter:
• It receives the memory address of the corresponding actual parameter
• During program execution to manipulate data:
• Changes to a formal parameter will change the corresponding actual parameter
© 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service
or otherwise on a password-protected website for classroom
31
Reference Variables as Parameters (2 of 2)
• Reference parameters are useful in three situations:
• When changing the actual parameter
• When returning more than one value
• When passing the address would save memory space and time
© 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service
or otherwise on a password-protected website for classroom
32
Value and Reference Parameters and Memory Allocation (1 of 2)
• When a function is called:
• Memory for its formal parameters and its local variables is allocated in the function
data area
• For a value parameter, the actual parameter’s value is copied into the formal
parameter’s memory cell
• Changes to the formal parameter do not affect the actual parameter’s value
© 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service
or otherwise on a password-protected website for classroom
33
Value and Reference Parameters and Memory Allocation (2 of 2)
• For a reference parameter, the actual parameter’s address passes to the formal
parameter
• Both formal and actual parameters refer to the same memory location
• During execution, any change made to the formal parameter’s value immediately
changes the actual parameter’s value
© 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service
or otherwise on a password-protected website for classroom
34
Reference Parameters and Value-Returning Functions
• Can also use reference parameters in a value-returning function
• Not recommended
• By definition, a value-returning function returns a single value via return
statement
• If a function needs to return more than one value, change it to a void function
and use reference parameters to return the values
© 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service
or otherwise on a password-protected website for classroom
35
Scope of an Identifier
• Scope of an identifier: where in the program the identifier is accessible
• Local identifier: identifiers declared within a function (or block)
• Global identifier: identifiers declared outside of every function definition
• C++ does not allow nested functions
• Definition of one function cannot be included in the body of another function
© 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service
or otherwise on a password-protected website for classroom
36
Rules Applied When an Identifier is Accessed (1 of 2)
• Global identifiers are accessible by a function or block if:
• The identifier is declared before the function definition (block)
• The function name different from the identifier
• All parameters to the function have different names than the identifier name
• All local identifiers have different names than the identifier name
© 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service
or otherwise on a password-protected website for classroom
37
Rules Applied When an Identifier is Accessed (2 of 2)
• (Nested block) – an identifier declared within a block is accessible:
• From its point of declaration to the end of the block in which it is declared
• Within nested blocks if no identifier with the same name exists
• The scope of a function name is similar to the scope of an identifier declared
outside any block
• The function name scope is the same as the global variable scope
© 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service
or otherwise on a password-protected website for classroom
38
Other Notes about Global Variables
• Some compilers initialize global variables to default values
• The scope resolution operator in C++ is ::
• By using the scope resolution operator:
• A global variable declared before the definition of a function (or block) can be
accessed by the function (or block) even if the function (or block) has an identifier
with the same name as the global variable
• To access a global variable declared after the definition of a function, the
function must not contain any identifier with the same name
• Reserved word extern indicates that a global variable has been declared elsewhere
© 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service
or otherwise on a password-protected website for classroom
39
Global Variables, Named Constants, and Side Effects
• Using global variables causes side effects
• A function that uses global variables is not independent
• If more than one function uses the same global variable:
• It can be difficult to debug problems with the code
• Problems caused in one area of the program may appear to be from another area
• Global named constants have no side effects
© 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service
or otherwise on a password-protected website for classroom
40
Static and Automatic Variables (1 of 2)
• Automatic variable: memory is allocated at block entry and deallocated at block
exit
• By default, variables declared within a block are automatic variables
• Static variable: memory remains allocated as long as the program executes
• Global variables declared outside of any block are static variables
© 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service
or otherwise on a password-protected website for classroom
41
Static and Automatic Variables (2 of 2)
• Declare a static variable within a block by using the reserved word static
• The syntax for declaring a static variable is:
• Static variables declared within a block are local to the block
• Have same scope as any other local identifier in that block
© 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service
or otherwise on a password-protected website for classroom
42
Debugging: Using Drivers and Stubs
• A driver program is a separate program used to test a function
• When results calculated by one function are needed in another function, use a
function stub
• A function stub is a function that is not fully coded
© 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service
or otherwise on a password-protected website for classroom
43
Function Overloading: An Introduction
• In a C++ program, several functions can have the same name
• Function overloading (or overloading a function name) occurs when creating
several functions with the same name
• Two functions are said to have different formal parameter lists if both functions
have either:
• A different number of formal parameters
• If the number of formal parameters is the same, but the data type of the formal
parameters differs in at least one position
© 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service
or otherwise on a password-protected website for classroom
44
Function Overloading
• Overloaded functions must have different formal parameter lists
• The signature: the name and formal parameter list of the function
• Does not include the return type of the function
• The parameter list supplied in a call to an overloaded function determines
which function is executed
© 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service
or otherwise on a password-protected website for classroom
45
Functions with Default Parameters (1 of 2)
• In a function call, the number of actual and formal parameters must be the
same
• C++ relaxes this condition for functions with default parameters
• Can specify the value of a default parameter in the function prototype
• If you do not specify the value for a default parameter when calling the
function, the default value is used
© 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service
or otherwise on a password-protected website for classroom
46
Functions with Default Parameters (2 of 2)
• All default parameters must be the rightmost parameters of the function
• If a default parameter value is not specified:
• You must omit all of the arguments to its right
• Default values can be constants, global variables, or function calls
• Cannot assign a constant value as a default value to a reference parameter
© 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service
or otherwise on a password-protected website for classroom
47
Quick Review (1 of 4)
• Functions (modules) divide a program into manageable tasks
• C++ provides standard, predefined functions
• Two types of user-defined functions: value-returning functions and void
functions
• Variables defined in a function heading are called formal parameters
• Expressions, variables, or constant values in a function call are called actual
parameters
© 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service
or otherwise on a password-protected website for classroom
48
Quick Review (2 of 4)
• Function heading and the body of the function are called the definition of the
function
• A value-returning function returns its value via the return statement
• A prototype is the function heading without the body of the function
• User-defined functions execute only when they are called
• Void functions do not have a data type
© 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service
or otherwise on a password-protected website for classroom
49
Quick Review (3 of 4)
• There are two types of formal parameters
• A value parameter receives a copy of its corresponding actual parameter
• A reference parameter receives the memory address of its corresponding actual
parameter
• Variables declared within a function (or block) are called local variables
• Variables declared outside of every function definition (and block) are global
variables
© 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service
or otherwise on a password-protected website for classroom
50
Quick Review (4 of 4)
• An automatic variable is a variable for which memory is allocated on
function/block entry and deallocated on function/block exit
• A static variable is a variable for which memory remains allocated throughout
the execution of the program
• C++ functions can have default parameters
© 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service
or otherwise on a password-protected website for classroom

More Related Content

What's hot

9781337102087 ppt ch14
9781337102087 ppt ch149781337102087 ppt ch14
9781337102087 ppt ch14Terry Yoast
 
9781337102087 ppt ch11
9781337102087 ppt ch119781337102087 ppt ch11
9781337102087 ppt ch11Terry Yoast
 
9781337102087 ppt ch17
9781337102087 ppt ch179781337102087 ppt ch17
9781337102087 ppt ch17Terry Yoast
 
Spring AOP
Spring AOPSpring AOP
Spring AOPAnushaNaidu
 
.Net Classes and Objects | UiPath Community
.Net Classes and Objects | UiPath Community.Net Classes and Objects | UiPath Community
.Net Classes and Objects | UiPath CommunityRohit Radhakrishnan
 
Certification preparation - Net classses and functions.pptx
Certification preparation - Net classses and functions.pptxCertification preparation - Net classses and functions.pptx
Certification preparation - Net classses and functions.pptxRohit Radhakrishnan
 
Certification preparation - Error Handling and Troubleshooting recap.pptx
Certification preparation - Error Handling and Troubleshooting recap.pptxCertification preparation - Error Handling and Troubleshooting recap.pptx
Certification preparation - Error Handling and Troubleshooting recap.pptxRohit Radhakrishnan
 
9781337102087 ppt ch16
9781337102087 ppt ch169781337102087 ppt ch16
9781337102087 ppt ch16Terry Yoast
 
Functional Programming With Lambdas and Streams in JDK8
 Functional Programming With Lambdas and Streams in JDK8 Functional Programming With Lambdas and Streams in JDK8
Functional Programming With Lambdas and Streams in JDK8IndicThreads
 
Lambdas and Streams in Java SE 8: Making Bulk Operations simple - Simon Ritter
Lambdas and Streams in Java SE 8: Making Bulk Operations simple - Simon RitterLambdas and Streams in Java SE 8: Making Bulk Operations simple - Simon Ritter
Lambdas and Streams in Java SE 8: Making Bulk Operations simple - Simon RitterJAXLondon2014
 
Improved Developer Productivity In JDK8
Improved Developer Productivity In JDK8Improved Developer Productivity In JDK8
Improved Developer Productivity In JDK8Simon Ritter
 
Aspect Oriented Programming Through C#.NET
Aspect Oriented Programming Through C#.NETAspect Oriented Programming Through C#.NET
Aspect Oriented Programming Through C#.NETWaqas Tariq
 
Defaultification Refactoring: A Tool for Automatically Converting Java Method...
Defaultification Refactoring: A Tool for Automatically Converting Java Method...Defaultification Refactoring: A Tool for Automatically Converting Java Method...
Defaultification Refactoring: A Tool for Automatically Converting Java Method...Raffi Khatchadourian
 
Java8 training - class 2
Java8 training - class 2Java8 training - class 2
Java8 training - class 2Marut Singh
 
Defaultification Refactoring: A Tool for Automatically Converting Java Method...
Defaultification Refactoring: A Tool for Automatically Converting Java Method...Defaultification Refactoring: A Tool for Automatically Converting Java Method...
Defaultification Refactoring: A Tool for Automatically Converting Java Method...Raffi Khatchadourian
 
Testware Hierarchy for Test Automation
Testware Hierarchy for Test AutomationTestware Hierarchy for Test Automation
Testware Hierarchy for Test AutomationGregory Solovey
 
Cso gaddis java_chapter5
Cso gaddis java_chapter5Cso gaddis java_chapter5
Cso gaddis java_chapter5mlrbrown
 
Software Design Patterns. Part I :: Structural Patterns
Software Design Patterns. Part I :: Structural PatternsSoftware Design Patterns. Part I :: Structural Patterns
Software Design Patterns. Part I :: Structural PatternsSergey Aganezov
 

What's hot (20)

9781337102087 ppt ch14
9781337102087 ppt ch149781337102087 ppt ch14
9781337102087 ppt ch14
 
9781337102087 ppt ch11
9781337102087 ppt ch119781337102087 ppt ch11
9781337102087 ppt ch11
 
9781337102087 ppt ch17
9781337102087 ppt ch179781337102087 ppt ch17
9781337102087 ppt ch17
 
Spring AOP
Spring AOPSpring AOP
Spring AOP
 
.Net Classes and Objects | UiPath Community
.Net Classes and Objects | UiPath Community.Net Classes and Objects | UiPath Community
.Net Classes and Objects | UiPath Community
 
Certification preparation - Net classses and functions.pptx
Certification preparation - Net classses and functions.pptxCertification preparation - Net classses and functions.pptx
Certification preparation - Net classses and functions.pptx
 
Certification preparation - Error Handling and Troubleshooting recap.pptx
Certification preparation - Error Handling and Troubleshooting recap.pptxCertification preparation - Error Handling and Troubleshooting recap.pptx
Certification preparation - Error Handling and Troubleshooting recap.pptx
 
9781337102087 ppt ch16
9781337102087 ppt ch169781337102087 ppt ch16
9781337102087 ppt ch16
 
Functional Programming With Lambdas and Streams in JDK8
 Functional Programming With Lambdas and Streams in JDK8 Functional Programming With Lambdas and Streams in JDK8
Functional Programming With Lambdas and Streams in JDK8
 
Lambdas and Streams in Java SE 8: Making Bulk Operations simple - Simon Ritter
Lambdas and Streams in Java SE 8: Making Bulk Operations simple - Simon RitterLambdas and Streams in Java SE 8: Making Bulk Operations simple - Simon Ritter
Lambdas and Streams in Java SE 8: Making Bulk Operations simple - Simon Ritter
 
Improved Developer Productivity In JDK8
Improved Developer Productivity In JDK8Improved Developer Productivity In JDK8
Improved Developer Productivity In JDK8
 
Chap4java5th
Chap4java5thChap4java5th
Chap4java5th
 
Aspect Oriented Programming Through C#.NET
Aspect Oriented Programming Through C#.NETAspect Oriented Programming Through C#.NET
Aspect Oriented Programming Through C#.NET
 
Defaultification Refactoring: A Tool for Automatically Converting Java Method...
Defaultification Refactoring: A Tool for Automatically Converting Java Method...Defaultification Refactoring: A Tool for Automatically Converting Java Method...
Defaultification Refactoring: A Tool for Automatically Converting Java Method...
 
Java8 training - class 2
Java8 training - class 2Java8 training - class 2
Java8 training - class 2
 
Chap2java5th
Chap2java5thChap2java5th
Chap2java5th
 
Defaultification Refactoring: A Tool for Automatically Converting Java Method...
Defaultification Refactoring: A Tool for Automatically Converting Java Method...Defaultification Refactoring: A Tool for Automatically Converting Java Method...
Defaultification Refactoring: A Tool for Automatically Converting Java Method...
 
Testware Hierarchy for Test Automation
Testware Hierarchy for Test AutomationTestware Hierarchy for Test Automation
Testware Hierarchy for Test Automation
 
Cso gaddis java_chapter5
Cso gaddis java_chapter5Cso gaddis java_chapter5
Cso gaddis java_chapter5
 
Software Design Patterns. Part I :: Structural Patterns
Software Design Patterns. Part I :: Structural PatternsSoftware Design Patterns. Part I :: Structural Patterns
Software Design Patterns. Part I :: Structural Patterns
 

Similar to 9781337102087 ppt ch06

Software Development, Data Types, and Expressions
Software Development, Data Types, and ExpressionsSoftware Development, Data Types, and Expressions
Software Development, Data Types, and Expressionspullaravikumar
 
9781337102087 ppt ch15
9781337102087 ppt ch159781337102087 ppt ch15
9781337102087 ppt ch15Terry Yoast
 
Python Fundamentals
Python FundamentalsPython Fundamentals
Python Fundamentalspullaravikumar
 
Guidelines and Best Practices for Sencha Projects
Guidelines and Best Practices for Sencha ProjectsGuidelines and Best Practices for Sencha Projects
Guidelines and Best Practices for Sencha ProjectsAmitaSuri
 
Oracle APEX Application Features
Oracle APEX Application FeaturesOracle APEX Application Features
Oracle APEX Application FeaturesChristina Cho
 
APEX Office Hours Interactive Grid Deep Dive
APEX Office Hours Interactive Grid Deep DiveAPEX Office Hours Interactive Grid Deep Dive
APEX Office Hours Interactive Grid Deep DiveJohnSnyders
 
Excel module 2 ppt presentation
Excel module 2 ppt presentationExcel module 2 ppt presentation
Excel module 2 ppt presentationdgdotson
 
Access 2016 module 4 ppt presentation
Access 2016 module 4 ppt presentationAccess 2016 module 4 ppt presentation
Access 2016 module 4 ppt presentationdgdotson
 
GraphQL Advanced
GraphQL AdvancedGraphQL Advanced
GraphQL AdvancedLeanIX GmbH
 
Chapter 14 Business Continuity
Chapter 14 Business ContinuityChapter 14 Business Continuity
Chapter 14 Business ContinuityDr. Ahmed Al Zaidy
 
Excel module 1 ppt presentation
Excel module 1 ppt presentationExcel module 1 ppt presentation
Excel module 1 ppt presentationdgdotson
 
Automation in Jira for beginners
Automation in Jira for beginnersAutomation in Jira for beginners
Automation in Jira for beginnersElad Ben-Noam
 
Chapter 4 - Completing the Problem-Solving Process
Chapter 4 - Completing the Problem-Solving ProcessChapter 4 - Completing the Problem-Solving Process
Chapter 4 - Completing the Problem-Solving Processmshellman
 
Talentica - JS Meetup - Angular Schematics
Talentica - JS Meetup - Angular SchematicsTalentica - JS Meetup - Angular Schematics
Talentica - JS Meetup - Angular SchematicsKrishnan Mudaliar
 
Integrating Existing C++ Libraries into PySpark with Esther Kundin
Integrating Existing C++ Libraries into PySpark with Esther KundinIntegrating Existing C++ Libraries into PySpark with Esther Kundin
Integrating Existing C++ Libraries into PySpark with Esther KundinDatabricks
 

Similar to 9781337102087 ppt ch06 (20)

Software Development, Data Types, and Expressions
Software Development, Data Types, and ExpressionsSoftware Development, Data Types, and Expressions
Software Development, Data Types, and Expressions
 
9781337102087 ppt ch15
9781337102087 ppt ch159781337102087 ppt ch15
9781337102087 ppt ch15
 
Sql9e ppt ch08
Sql9e ppt ch08Sql9e ppt ch08
Sql9e ppt ch08
 
Python Fundamentals
Python FundamentalsPython Fundamentals
Python Fundamentals
 
Guidelines and Best Practices for Sencha Projects
Guidelines and Best Practices for Sencha ProjectsGuidelines and Best Practices for Sencha Projects
Guidelines and Best Practices for Sencha Projects
 
Oracle APEX Application Features
Oracle APEX Application FeaturesOracle APEX Application Features
Oracle APEX Application Features
 
APEX Office Hours Interactive Grid Deep Dive
APEX Office Hours Interactive Grid Deep DiveAPEX Office Hours Interactive Grid Deep Dive
APEX Office Hours Interactive Grid Deep Dive
 
Advanced angular
Advanced angularAdvanced angular
Advanced angular
 
Excel module 2 ppt presentation
Excel module 2 ppt presentationExcel module 2 ppt presentation
Excel module 2 ppt presentation
 
Access 2016 module 4 ppt presentation
Access 2016 module 4 ppt presentationAccess 2016 module 4 ppt presentation
Access 2016 module 4 ppt presentation
 
GraphQL Advanced
GraphQL AdvancedGraphQL Advanced
GraphQL Advanced
 
Chapter 14 Business Continuity
Chapter 14 Business ContinuityChapter 14 Business Continuity
Chapter 14 Business Continuity
 
Excel module 1 ppt presentation
Excel module 1 ppt presentationExcel module 1 ppt presentation
Excel module 1 ppt presentation
 
Automation in Jira for beginners
Automation in Jira for beginnersAutomation in Jira for beginners
Automation in Jira for beginners
 
Open mp
Open mpOpen mp
Open mp
 
Chapter 4 - Completing the Problem-Solving Process
Chapter 4 - Completing the Problem-Solving ProcessChapter 4 - Completing the Problem-Solving Process
Chapter 4 - Completing the Problem-Solving Process
 
Talentica - JS Meetup - Angular Schematics
Talentica - JS Meetup - Angular SchematicsTalentica - JS Meetup - Angular Schematics
Talentica - JS Meetup - Angular Schematics
 
Integrating Existing C++ Libraries into PySpark with Esther Kundin
Integrating Existing C++ Libraries into PySpark with Esther KundinIntegrating Existing C++ Libraries into PySpark with Esther Kundin
Integrating Existing C++ Libraries into PySpark with Esther Kundin
 
Intro To C++ - Class #19: Functions
Intro To C++ - Class #19: FunctionsIntro To C++ - Class #19: Functions
Intro To C++ - Class #19: Functions
 
Sql9e ppt ch02
Sql9e ppt ch02 Sql9e ppt ch02
Sql9e ppt ch02
 

More from Terry Yoast

9781305078444 ppt ch12
9781305078444 ppt ch129781305078444 ppt ch12
9781305078444 ppt ch12Terry Yoast
 
9781305078444 ppt ch11
9781305078444 ppt ch119781305078444 ppt ch11
9781305078444 ppt ch11Terry Yoast
 
9781305078444 ppt ch10
9781305078444 ppt ch109781305078444 ppt ch10
9781305078444 ppt ch10Terry Yoast
 
9781305078444 ppt ch09
9781305078444 ppt ch099781305078444 ppt ch09
9781305078444 ppt ch09Terry Yoast
 
9781305078444 ppt ch08
9781305078444 ppt ch089781305078444 ppt ch08
9781305078444 ppt ch08Terry Yoast
 
9781305078444 ppt ch07
9781305078444 ppt ch079781305078444 ppt ch07
9781305078444 ppt ch07Terry Yoast
 
9781305078444 ppt ch06
9781305078444 ppt ch069781305078444 ppt ch06
9781305078444 ppt ch06Terry Yoast
 
9781305078444 ppt ch05
9781305078444 ppt ch059781305078444 ppt ch05
9781305078444 ppt ch05Terry Yoast
 
9781305078444 ppt ch04
9781305078444 ppt ch049781305078444 ppt ch04
9781305078444 ppt ch04Terry Yoast
 
9781305078444 ppt ch03
9781305078444 ppt ch039781305078444 ppt ch03
9781305078444 ppt ch03Terry Yoast
 
9781305078444 ppt ch02
9781305078444 ppt ch029781305078444 ppt ch02
9781305078444 ppt ch02Terry Yoast
 
9781305078444 ppt ch01
9781305078444 ppt ch019781305078444 ppt ch01
9781305078444 ppt ch01Terry Yoast
 
9780538745840 ppt ch10
9780538745840 ppt ch109780538745840 ppt ch10
9780538745840 ppt ch10Terry Yoast
 
9780538745840 ppt ch09
9780538745840 ppt ch099780538745840 ppt ch09
9780538745840 ppt ch09Terry Yoast
 
9780538745840 ppt ch08
9780538745840 ppt ch089780538745840 ppt ch08
9780538745840 ppt ch08Terry Yoast
 
9780538745840 ppt ch07
9780538745840 ppt ch079780538745840 ppt ch07
9780538745840 ppt ch07Terry Yoast
 
9780538745840 ppt ch06
9780538745840 ppt ch069780538745840 ppt ch06
9780538745840 ppt ch06Terry Yoast
 

More from Terry Yoast (17)

9781305078444 ppt ch12
9781305078444 ppt ch129781305078444 ppt ch12
9781305078444 ppt ch12
 
9781305078444 ppt ch11
9781305078444 ppt ch119781305078444 ppt ch11
9781305078444 ppt ch11
 
9781305078444 ppt ch10
9781305078444 ppt ch109781305078444 ppt ch10
9781305078444 ppt ch10
 
9781305078444 ppt ch09
9781305078444 ppt ch099781305078444 ppt ch09
9781305078444 ppt ch09
 
9781305078444 ppt ch08
9781305078444 ppt ch089781305078444 ppt ch08
9781305078444 ppt ch08
 
9781305078444 ppt ch07
9781305078444 ppt ch079781305078444 ppt ch07
9781305078444 ppt ch07
 
9781305078444 ppt ch06
9781305078444 ppt ch069781305078444 ppt ch06
9781305078444 ppt ch06
 
9781305078444 ppt ch05
9781305078444 ppt ch059781305078444 ppt ch05
9781305078444 ppt ch05
 
9781305078444 ppt ch04
9781305078444 ppt ch049781305078444 ppt ch04
9781305078444 ppt ch04
 
9781305078444 ppt ch03
9781305078444 ppt ch039781305078444 ppt ch03
9781305078444 ppt ch03
 
9781305078444 ppt ch02
9781305078444 ppt ch029781305078444 ppt ch02
9781305078444 ppt ch02
 
9781305078444 ppt ch01
9781305078444 ppt ch019781305078444 ppt ch01
9781305078444 ppt ch01
 
9780538745840 ppt ch10
9780538745840 ppt ch109780538745840 ppt ch10
9780538745840 ppt ch10
 
9780538745840 ppt ch09
9780538745840 ppt ch099780538745840 ppt ch09
9780538745840 ppt ch09
 
9780538745840 ppt ch08
9780538745840 ppt ch089780538745840 ppt ch08
9780538745840 ppt ch08
 
9780538745840 ppt ch07
9780538745840 ppt ch079780538745840 ppt ch07
9780538745840 ppt ch07
 
9780538745840 ppt ch06
9780538745840 ppt ch069780538745840 ppt ch06
9780538745840 ppt ch06
 

Recently uploaded

The basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxThe basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxheathfieldcps1
 
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdfssuser54595a
 
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17Incoming and Outgoing Shipments in 1 STEP Using Odoo 17
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17Celine George
 
Q4-W6-Restating Informational Text Grade 3
Q4-W6-Restating Informational Text Grade 3Q4-W6-Restating Informational Text Grade 3
Q4-W6-Restating Informational Text Grade 3JemimahLaneBuaron
 
Introduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptxIntroduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptxpboyjonauth
 
Mastering the Unannounced Regulatory Inspection
Mastering the Unannounced Regulatory InspectionMastering the Unannounced Regulatory Inspection
Mastering the Unannounced Regulatory InspectionSafetyChain Software
 
Industrial Policy - 1948, 1956, 1973, 1977, 1980, 1991
Industrial Policy - 1948, 1956, 1973, 1977, 1980, 1991Industrial Policy - 1948, 1956, 1973, 1977, 1980, 1991
Industrial Policy - 1948, 1956, 1973, 1977, 1980, 1991RKavithamani
 
How to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptxHow to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptxmanuelaromero2013
 
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptxPOINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptxSayali Powar
 
Accessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactAccessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactdawncurless
 
Sanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdfSanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdfsanyamsingh5019
 
MENTAL STATUS EXAMINATION format.docx
MENTAL     STATUS EXAMINATION format.docxMENTAL     STATUS EXAMINATION format.docx
MENTAL STATUS EXAMINATION format.docxPoojaSen20
 
Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111Sapana Sha
 
_Math 4-Q4 Week 5.pptx Steps in Collecting Data
_Math 4-Q4 Week 5.pptx Steps in Collecting Data_Math 4-Q4 Week 5.pptx Steps in Collecting Data
_Math 4-Q4 Week 5.pptx Steps in Collecting DataJhengPantaleon
 
Employee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptxEmployee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptxNirmalaLoungPoorunde1
 
Alper Gobel In Media Res Media Component
Alper Gobel In Media Res Media ComponentAlper Gobel In Media Res Media Component
Alper Gobel In Media Res Media ComponentInMediaRes1
 
Paris 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityParis 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityGeoBlogs
 
Concept of Vouching. B.Com(Hons) /B.Compdf
Concept of Vouching. B.Com(Hons) /B.CompdfConcept of Vouching. B.Com(Hons) /B.Compdf
Concept of Vouching. B.Com(Hons) /B.CompdfUmakantAnnand
 

Recently uploaded (20)

The basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxThe basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptx
 
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
 
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17Incoming and Outgoing Shipments in 1 STEP Using Odoo 17
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17
 
Q4-W6-Restating Informational Text Grade 3
Q4-W6-Restating Informational Text Grade 3Q4-W6-Restating Informational Text Grade 3
Q4-W6-Restating Informational Text Grade 3
 
Introduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptxIntroduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptx
 
Mastering the Unannounced Regulatory Inspection
Mastering the Unannounced Regulatory InspectionMastering the Unannounced Regulatory Inspection
Mastering the Unannounced Regulatory Inspection
 
Industrial Policy - 1948, 1956, 1973, 1977, 1980, 1991
Industrial Policy - 1948, 1956, 1973, 1977, 1980, 1991Industrial Policy - 1948, 1956, 1973, 1977, 1980, 1991
Industrial Policy - 1948, 1956, 1973, 1977, 1980, 1991
 
How to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptxHow to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptx
 
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptxPOINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
 
Accessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactAccessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impact
 
Sanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdfSanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdf
 
MENTAL STATUS EXAMINATION format.docx
MENTAL     STATUS EXAMINATION format.docxMENTAL     STATUS EXAMINATION format.docx
MENTAL STATUS EXAMINATION format.docx
 
Staff of Color (SOC) Retention Efforts DDSD
Staff of Color (SOC) Retention Efforts DDSDStaff of Color (SOC) Retention Efforts DDSD
Staff of Color (SOC) Retention Efforts DDSD
 
Model Call Girl in Bikash Puri Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Bikash Puri  Delhi reach out to us at 🔝9953056974🔝Model Call Girl in Bikash Puri  Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Bikash Puri Delhi reach out to us at 🔝9953056974🔝
 
Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111
 
_Math 4-Q4 Week 5.pptx Steps in Collecting Data
_Math 4-Q4 Week 5.pptx Steps in Collecting Data_Math 4-Q4 Week 5.pptx Steps in Collecting Data
_Math 4-Q4 Week 5.pptx Steps in Collecting Data
 
Employee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptxEmployee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptx
 
Alper Gobel In Media Res Media Component
Alper Gobel In Media Res Media ComponentAlper Gobel In Media Res Media Component
Alper Gobel In Media Res Media Component
 
Paris 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityParis 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activity
 
Concept of Vouching. B.Com(Hons) /B.Compdf
Concept of Vouching. B.Com(Hons) /B.CompdfConcept of Vouching. B.Com(Hons) /B.Compdf
Concept of Vouching. B.Com(Hons) /B.Compdf
 

9781337102087 ppt ch06

  • 1. 1 C++ Programming: From Problem Analysis to Program Design, Eighth Edition Chapter 6 User-Defined Functions
  • 2. 2 Objectives (1 of 2) • In this chapter, you will: • Learn about standard (predefined) functions and discover how to use them in a program • Learn about user-defined functions • Examine value-returning functions , including actual and formal parameters • Explore how to construct and use a value-returning, user-defined function in a program • Learn about function prototypes • Learn how to construct and use void functions © 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service or otherwise on a password-protected website for classroom
  • 3. 3 Objectives (2 of 2) • Discover the difference between value and reference parameters • Explore reference parameters and value-returning functions • Learn about the scope of an identifier • Examine the difference between local and global identifiers • Discover static variables • Learn how to debug programs using drivers and stubs • Learn function overloading • Explore functions with default parameters © 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service or otherwise on a password-protected website for classroom
  • 4. 4 Introduction • Functions allow complicated programs to be divided into manageable pieces • Functions are often called modules • They are like miniature programs that can be combined to form larger programs © 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service or otherwise on a password-protected website for classroom
  • 5. 5 Predefined Functions (1 of 2) • In C++, a function is similar to that of a function in algebra • It has a name • It does some computation • Some of the predefined mathematical functions are: pow(x, y) sqrt(x) floor(x) © 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service or otherwise on a password-protected website for classroom
  • 6. 6 Predefined Functions (2 of 2) • Predefined functions are organized into separate libraries • I/O functions are in iostream header • Math functions are in cmath header • To use predefined functions, you must include the header file using an include statement • See Table 6-1 in the text for some common predefined functions © 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service or otherwise on a password-protected website for classroom
  • 7. 7 User-Defined Functions • Value-returning functions have a return type • Return a value of a specific data type using the return statement • Void functions do not have a return type • Do not use a return statement to return a value © 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service or otherwise on a password-protected website for classroom
  • 8. 8 Value-Returning Functions (1 of 3) • To use these functions, you must: • Include the appropriate header file in your program using the include statement • Know the following items: - Name of the function - Number of parameters, if any - Data type of each parameter - Data type of the value returned, called the type of the function © 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service or otherwise on a password-protected website for classroom
  • 9. 9 Value-Returning Functions (2 of 3) • Can use the value returned by a value-returning function by: • Saving it for further calculation • Using it in some calculation • Printing it • A value-returning function is used: • In an assignment • As a parameter in a function call • In an output statement © 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service or otherwise on a password-protected website for classroom
  • 10. 10 Value-Returning Functions (3 of 3) • Heading (or function header): the first line of the function • Example: int abs(int number) • The body is the function’s code that accomplishes the task • Formal parameter: a variable declared in the heading • Example: number • Actual parameter: a variable or expression listed in a call to a function • Example: x = pow(u, v) © 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service or otherwise on a password-protected website for classroom
  • 11. 11 Syntax: Value-Returning Function • Syntax • functionType is also called the data type or return type © 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service or otherwise on a password-protected website for classroom
  • 12. 12 Syntax: Formal Parameter List FIGURE 6-1 Various parts of the function abs © 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service or otherwise on a password-protected website for classroom
  • 13. 13 Function Call • Syntax to call a value-returning function © 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service or otherwise on a password-protected website for classroom
  • 14. 14 Syntax: Actual Parameter List • The syntax of the actual parameter list is: • The formal parameter list can be empty • A call to a value-returning function with an empty formal parameter list is: © 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service or otherwise on a password-protected website for classroom
  • 15. 15 return Statement • A function returns its value via the return statement • It passes this value outside the function © 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service or otherwise on a password-protected website for classroom
  • 16. 16 Syntax: return Statement (1 of 2) • The return statement has this syntax: • In C++, return is a reserved word • When a return statement executes • The function immediately terminates • Control goes back to the caller • When a return statement executes in the function main, the program terminates © 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service or otherwise on a password-protected website for classroom
  • 17. 17 Syntax: return Statement (2 of 2) FIGURE 6-2 Various parts of the function larger © 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service or otherwise on a password-protected website for classroom
  • 18. 18 Function Prototype • A function prototype is the function heading without the body of the function • The general syntax of the function prototype of a value-returning function is: • It is not necessary to specify the variable name in the parameter list • The data type of each parameter must be specified © 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service or otherwise on a password-protected website for classroom
  • 19. 19 Value-Returning Functions: Some Peculiarities (1 of 2) int secret(int x) { if (x > 5) //Line 1 return 2 * x; //Line 2 } A correct definition of the function secret is: int secret(int x) { if (x > 5) //Line 1 return 2 * x; //Line 2 return x; //Line 3 } © 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service or otherwise on a password-protected website for classroom
  • 20. 20 Value-Returning Functions: Some Peculiarities (2 of 2) • Examples pointing out that the return statement only returns one value return x, y; //only the value of y will be returned int funcRet1() { int x = 45; return 23, x; //only the value of x is returned } int funcRet2(int z) { int a = 2; int b = 3; return 2 * a + b, z + b; //only the value of z + b is returned } © 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service or otherwise on a password-protected website for classroom
  • 21. 21 More Examples of Value-Returning Functions (1 of 2) char courseGrade(int score) { switch (score / 10) { case 0: case 1: case 2: case 3: case 4: case 5: return 'F'; case 6: return 'D'; case 7: return 'C'; case 8: return 'B'; case 9: case 10: return 'A'; } } © 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service or otherwise on a password-protected website for classroom
  • 22. 22 More Examples of Value-Returning Functions (2 of 2) • In addition to Example 6-3 courseGrade, other examples are given in the text • Example 6-4 (rolling a pair of dice) • Example 6-5 (Fibonacci number) • Example 6-6 (palindrome) • Example 6-7 (cable company) © 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service or otherwise on a password-protected website for classroom
  • 23. 23 Flow of Compilation and Execution (1 of 2) • Execution always begins at the first statement in the function main • Other functions are executed only when called • Function prototypes appear before any function definition • The compiler translates these first • The compiler can then correctly translate a function call © 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service or otherwise on a password-protected website for classroom
  • 24. 24 Flow of Compilation and Execution (2 of 2) • A function call transfers control to the first statement in the body of the called function • When the end of a called function is executed, control is passed back to the point immediately following the function call • A function’s returned value replaces the function call statement © 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service or otherwise on a password-protected website for classroom
  • 25. 25 Void Functions (1 of 4) • User-defined void functions can be placed either before or after the function main • If user-defined void functions are placed after the function main • The function prototype must be placed before the function main • A void function does not have a return type • A return statement without any value is typically used to exit the function early © 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service or otherwise on a password-protected website for classroom
  • 26. 26 Void Functions (2 of 4) • Formal parameters are optional • A call to a void function is a stand-alone statement • The function definition of void functions with parameters has the following syntax: © 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service or otherwise on a password-protected website for classroom
  • 27. 27 Void Functions (3 of 4) • Formal parameter list syntax • Function call syntax • Actual parameter list syntax © 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service or otherwise on a password-protected website for classroom
  • 28. 28 Void Functions (4 of 4) • Two types of formal parameters • Value parameter: a formal parameter that receives a copy of the content of corresponding actual parameter • Reference parameter: a formal parameter that receives the location (memory address) of the corresponding actual parameter © 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service or otherwise on a password-protected website for classroom
  • 29. 29 Value Parameters • If a formal parameter is a value parameter, the value of the corresponding actual parameter is copied into it • A formal parameter has its own copy of the data • During program execution, a formal parameter manipulates the data stored in its own memory space © 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service or otherwise on a password-protected website for classroom
  • 30. 30 Reference Variables as Parameters (1 of 2) • If a formal parameter is a reference parameter: • It receives the memory address of the corresponding actual parameter • During program execution to manipulate data: • Changes to a formal parameter will change the corresponding actual parameter © 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service or otherwise on a password-protected website for classroom
  • 31. 31 Reference Variables as Parameters (2 of 2) • Reference parameters are useful in three situations: • When changing the actual parameter • When returning more than one value • When passing the address would save memory space and time © 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service or otherwise on a password-protected website for classroom
  • 32. 32 Value and Reference Parameters and Memory Allocation (1 of 2) • When a function is called: • Memory for its formal parameters and its local variables is allocated in the function data area • For a value parameter, the actual parameter’s value is copied into the formal parameter’s memory cell • Changes to the formal parameter do not affect the actual parameter’s value © 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service or otherwise on a password-protected website for classroom
  • 33. 33 Value and Reference Parameters and Memory Allocation (2 of 2) • For a reference parameter, the actual parameter’s address passes to the formal parameter • Both formal and actual parameters refer to the same memory location • During execution, any change made to the formal parameter’s value immediately changes the actual parameter’s value © 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service or otherwise on a password-protected website for classroom
  • 34. 34 Reference Parameters and Value-Returning Functions • Can also use reference parameters in a value-returning function • Not recommended • By definition, a value-returning function returns a single value via return statement • If a function needs to return more than one value, change it to a void function and use reference parameters to return the values © 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service or otherwise on a password-protected website for classroom
  • 35. 35 Scope of an Identifier • Scope of an identifier: where in the program the identifier is accessible • Local identifier: identifiers declared within a function (or block) • Global identifier: identifiers declared outside of every function definition • C++ does not allow nested functions • Definition of one function cannot be included in the body of another function © 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service or otherwise on a password-protected website for classroom
  • 36. 36 Rules Applied When an Identifier is Accessed (1 of 2) • Global identifiers are accessible by a function or block if: • The identifier is declared before the function definition (block) • The function name different from the identifier • All parameters to the function have different names than the identifier name • All local identifiers have different names than the identifier name © 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service or otherwise on a password-protected website for classroom
  • 37. 37 Rules Applied When an Identifier is Accessed (2 of 2) • (Nested block) – an identifier declared within a block is accessible: • From its point of declaration to the end of the block in which it is declared • Within nested blocks if no identifier with the same name exists • The scope of a function name is similar to the scope of an identifier declared outside any block • The function name scope is the same as the global variable scope © 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service or otherwise on a password-protected website for classroom
  • 38. 38 Other Notes about Global Variables • Some compilers initialize global variables to default values • The scope resolution operator in C++ is :: • By using the scope resolution operator: • A global variable declared before the definition of a function (or block) can be accessed by the function (or block) even if the function (or block) has an identifier with the same name as the global variable • To access a global variable declared after the definition of a function, the function must not contain any identifier with the same name • Reserved word extern indicates that a global variable has been declared elsewhere © 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service or otherwise on a password-protected website for classroom
  • 39. 39 Global Variables, Named Constants, and Side Effects • Using global variables causes side effects • A function that uses global variables is not independent • If more than one function uses the same global variable: • It can be difficult to debug problems with the code • Problems caused in one area of the program may appear to be from another area • Global named constants have no side effects © 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service or otherwise on a password-protected website for classroom
  • 40. 40 Static and Automatic Variables (1 of 2) • Automatic variable: memory is allocated at block entry and deallocated at block exit • By default, variables declared within a block are automatic variables • Static variable: memory remains allocated as long as the program executes • Global variables declared outside of any block are static variables © 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service or otherwise on a password-protected website for classroom
  • 41. 41 Static and Automatic Variables (2 of 2) • Declare a static variable within a block by using the reserved word static • The syntax for declaring a static variable is: • Static variables declared within a block are local to the block • Have same scope as any other local identifier in that block © 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service or otherwise on a password-protected website for classroom
  • 42. 42 Debugging: Using Drivers and Stubs • A driver program is a separate program used to test a function • When results calculated by one function are needed in another function, use a function stub • A function stub is a function that is not fully coded © 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service or otherwise on a password-protected website for classroom
  • 43. 43 Function Overloading: An Introduction • In a C++ program, several functions can have the same name • Function overloading (or overloading a function name) occurs when creating several functions with the same name • Two functions are said to have different formal parameter lists if both functions have either: • A different number of formal parameters • If the number of formal parameters is the same, but the data type of the formal parameters differs in at least one position © 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service or otherwise on a password-protected website for classroom
  • 44. 44 Function Overloading • Overloaded functions must have different formal parameter lists • The signature: the name and formal parameter list of the function • Does not include the return type of the function • The parameter list supplied in a call to an overloaded function determines which function is executed © 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service or otherwise on a password-protected website for classroom
  • 45. 45 Functions with Default Parameters (1 of 2) • In a function call, the number of actual and formal parameters must be the same • C++ relaxes this condition for functions with default parameters • Can specify the value of a default parameter in the function prototype • If you do not specify the value for a default parameter when calling the function, the default value is used © 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service or otherwise on a password-protected website for classroom
  • 46. 46 Functions with Default Parameters (2 of 2) • All default parameters must be the rightmost parameters of the function • If a default parameter value is not specified: • You must omit all of the arguments to its right • Default values can be constants, global variables, or function calls • Cannot assign a constant value as a default value to a reference parameter © 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service or otherwise on a password-protected website for classroom
  • 47. 47 Quick Review (1 of 4) • Functions (modules) divide a program into manageable tasks • C++ provides standard, predefined functions • Two types of user-defined functions: value-returning functions and void functions • Variables defined in a function heading are called formal parameters • Expressions, variables, or constant values in a function call are called actual parameters © 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service or otherwise on a password-protected website for classroom
  • 48. 48 Quick Review (2 of 4) • Function heading and the body of the function are called the definition of the function • A value-returning function returns its value via the return statement • A prototype is the function heading without the body of the function • User-defined functions execute only when they are called • Void functions do not have a data type © 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service or otherwise on a password-protected website for classroom
  • 49. 49 Quick Review (3 of 4) • There are two types of formal parameters • A value parameter receives a copy of its corresponding actual parameter • A reference parameter receives the memory address of its corresponding actual parameter • Variables declared within a function (or block) are called local variables • Variables declared outside of every function definition (and block) are global variables © 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service or otherwise on a password-protected website for classroom
  • 50. 50 Quick Review (4 of 4) • An automatic variable is a variable for which memory is allocated on function/block entry and deallocated on function/block exit • A static variable is a variable for which memory remains allocated throughout the execution of the program • C++ functions can have default parameters © 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service or otherwise on a password-protected website for classroom