Sie sind auf Seite 1von 13

//

Objectives
In this chapter, you will:
Learn about standard (predefined) functions
Chapter 6: Learn about user-defined functions
Examine value-returning functions
User-Defined Functions Construct and use a value-returning, user-defined
function
Construct and use void functions
Understand value and reference parameters

C++ Programming: From Problem Analysis to Program Design, Sixth Edition 2

Objectives (contd.) Introduction


In this chapter, you will (contd.): Functions are often called modules
Learn about the scope of identifiers They are like miniature programs that can be
combined to form larger programs
Understand local and global identifiers
They allow complicated programs to be divided
Learn about static variables into manageable pieces
Debug programs using drivers and stubs
Learn about function overloading
Explore functions with default parameters

C++ Programming: From Problem Analysis to Program Design, Sixth Edition 3 C++ Programming: From Problem Analysis to Program Design, Sixth Edition 4

//

Predefined Functions Predefined Functions (cont'd.)


In C++, a function is similar to that of a Predefined functions are organized into
function in algebra separate libraries
It has a name I/O functions are in iostream header
It does some computation Math functions are in cmath header
Some of the predefined mathematical To use predefined functions, you must include
functions are: the header file using an include statement
sqrt(x) See Table 6-1 in the text for some common
pow(x, y) predefined functions
floor(x)
C++ Programming: From Problem Analysis to Program Design, Sixth Edition 5 C++ Programming: From Problem Analysis to Program Design, Sixth Edition 6

User-Defined Functions Value-Returning Functions


Value-returning functions: have a return type To use these functions, you must:
Return a value of a specific data type using the Include the appropriate header file in your
return statement program using the include statement
Void functions: do not have a return type Know the following items:
Do not use a return statement to return a value 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

C++ Programming: From Problem Analysis to Program Design, Sixth Edition 7 C++ Programming: From Problem Analysis to Program Design, Sixth Edition 8

//

Value-Returning Functions Value-Returning Functions


(contd.) (contd.)
Can use the value returned by a value- Heading (or function header): first line of the
returning function by: function
Saving it for further calculation Example: int abs(int number)
Using it in some calculation Formal parameter: variable declared in the
Printing it heading
A value-returning function is used in an Example: number
assignment or in an output statement
Actual parameter: variable or expression listed
in a call to a function
Example: x = pow(u, v)
C++ Programming: From Problem Analysis to Program Design, Sixth Edition 9 C++ Programming: From Problem Analysis to Program Design, Sixth Edition 10

Syntax: Value-Returning Function Syntax: Formal Parameter List


Syntax:

functionType is also called the data type


or return type

C++ Programming: From Problem Analysis to Program Design, Sixth Edition 11 C++ Programming: From Problem Analysis to Program Design, Sixth Edition 12

//

Function Call Syntax: Actual Parameter List


Syntax to call a value-returning function: Syntax of the actual parameter list:

Formal parameter list can be empty:

A call to a value-returning function with an


empty formal parameter list is:

C++ Programming: From Problem Analysis to Program Design, Sixth Edition 13 C++ Programming: From Problem Analysis to Program Design, Sixth Edition 14

return Statement Syntax: return Statement


Function returns its value via the return Syntax:
statement
It passes this value outside the function In C++, return is a reserved word
When a return statement executes
Function immediately terminates
Control goes back to the caller
When a return statement executes in the
function main, the program terminates
C++ Programming: From Problem Analysis to Program Design, Sixth Edition 15 C++ Programming: From Problem Analysis to Program Design, Sixth Edition 16

//

Syntax: return Statement


Function Prototype
(contd.)
Function prototype: function heading without
the body of the function
Syntax:

Not necessary to specify the variable name in


the parameter list
Data type of each parameter must be
specified
C++ Programming: From Problem Analysis to Program Design, Sixth Edition 17 C++ Programming: From Problem Analysis to Program Design, Sixth Edition 18

Value-Returning Functions: Some Value-Returning Functions: Some


Peculiarities Peculiarities (contd.)

C++ Programming: From Problem Analysis to Program Design, Sixth Edition 19 C++ Programming: From Problem Analysis to Program Design, Sixth Edition 20

//

Flow of Execution Flow of Execution (contd.)


Execution always begins at the first statement Function call transfers control to the first
in the function main statement in the body of the called function
Other functions are executed only when called When the end of a called function is executed,
Function prototypes appear before any control is passed back to the point immediately
function definition following the function call
Compiler translates these first Functions returned value replaces the function
call statement
Compiler can then correctly translate a
function call

C++ Programming: From Problem Analysis to Program Design, Sixth Edition 21 C++ Programming: From Problem Analysis to Program Design, Sixth Edition 22

Void Functions Void Functions (contd.)


User-defined void functions can be placed Formal parameters are optional
either before or after the function main A call to a void function is a stand-alone
If user-defined void functions are placed after statement
the function main Void function definition syntax:
The function prototype must be placed before the
function main
Void function does not have a return type
return statement without any value is typically
used to exit the function early
C++ Programming: From Problem Analysis to Program Design, Sixth Edition 23 C++ Programming: From Problem Analysis to Program Design, Sixth Edition 24

//

Void Functions (contd.) Void Functions (contd.)


Formal parameter list syntax: Value parameter: a formal parameter that
receives a copy of the content of
Function call syntax: corresponding actual parameter
Reference parameter: a formal parameter that
receives the location (memory address) of the
Actual parameter list syntax: corresponding actual parameter

C++ Programming: From Problem Analysis to Program Design, Sixth Edition 25 C++ Programming: From Problem Analysis to Program Design, Sixth Edition 26

Value Parameters Reference Variables as Parameters


If a formal parameter is a value parameter: If a formal parameter is a reference parameter
The value of the corresponding actual parameter It receives the memory address of the
is copied into it corresponding actual parameter
Formal parameter has its own copy of the data During program execution to manipulate data
During program execution Changes to formal parameter will change the
Formal parameter manipulates the data stored in corresponding actual parameter
its own memory space

C++ Programming: From Problem Analysis to Program Design, Sixth Edition 27 C++ Programming: From Problem Analysis to Program Design, Sixth Edition 28

//

Reference Variables as Parameters Value and Reference Parameters and


(cont'd.) Memory Allocation
Reference parameters are useful in three When a function is called
situations: Memory for its formal parameters and its local
Returning more than one value variables is allocated in the function data area
Changing the actual parameter For a value parameter, the actual parameters
When passing the address would save memory value is copied into the formal parameters
space and time memory cell
Changes to the formal parameter do not affect the
actual parameters value

C++ Programming: From Problem Analysis to Program Design, Sixth Edition 29 C++ Programming: From Problem Analysis to Program Design, Sixth Edition 30

Value and Reference Parameters and Reference Parameters and Value-


Memory Allocation (contd.) Returning Functions
For a reference parameter, the actual Can also use reference parameters in a value-
parameters address passes to the formal returning function
parameter Not recommended
Both formal and actual parameters refer to the By definition, a value-returning function
same memory location returns a single value via return statement
During execution, changes made to the formal
If a function needs to return more than one
parameters value permanently change the actual
parameters value value, change it to a void function and use
reference parameters to return the values

C++ Programming: From Problem Analysis to Program Design, Sixth Edition 31 C++ Programming: From Problem Analysis to Program Design, Sixth Edition 32

//

Scope of an Identifier Scope of an Identifier (contd.)


Scope of an identifier: where in the program Rules when an identifier is accessed:
the identifier is accessible Global identifiers are accessible by a function or
Local identifier: identifiers declared within a block if:
function (or block) Declared before function definition
Global identifier: identifiers declared outside Function name different from identifier
of every function definition Parameters to the function have different names
All local identifiers have different names
C++ does not allow nested functions
Definition of one function cannot be included in
the body of another function

C++ Programming: From Problem Analysis to Program Design, Sixth Edition 33 C++ Programming: From Problem Analysis to Program Design, Sixth Edition 34

Scope of an Identifier (contd.) Scope of an Identifier (contd.)


Rules when an identifier is accessed (contd.): Some compilers initialize global variables to
Nested block default values
Identifier accessible from declaration to end of block in Scope resolution operator in C++ is ::
which it is declared By using the scope resolution operator
Within nested blocks if no identifier with same name
A global variable declared before the definition of
exists
a function (or block) can be accessed by the
Scope of function name similar to scope of function (or block)
identifier declared outside any block Even if the function (or block) has an identifier
i.e., function name scope = global variable scope with the same name as the global variable

C++ Programming: From Problem Analysis to Program Design, Sixth Edition 35 C++ Programming: From Problem Analysis to Program Design, Sixth Edition 36

//

Global Variables, Named


Scope of an Identifier (contd.)
Constants, and Side Effects
To access a global variable declared after the Using global variables causes side effects
definition of a function, the function must not A function that uses global variables is not
contain any identifier with the same name independent
Reserved word extern indicates that a global If more than one function uses the same
variable has been declared elsewhere global variable:
Can be difficult to debug problems with it
Problems caused in one area of the program may
appear to be from another area
Global named constants have no side effects
C++ Programming: From Problem Analysis to Program Design, Sixth Edition 37 C++ Programming: From Problem Analysis to Program Design, Sixth Edition 38

Static and Automatic Variables


Static and Automatic Variables
(contd.)
Automatic variable: memory is allocated at Can declare a static variable within a block by
block entry and deallocated at block exit using the reserved word static
By default, variables declared within a block are Syntax:
automatic variables
Static variable: memory remains allocated as
Static variables declared within a block are
long as the program executes
local to the block
Global variables declared outside of any block are
Have same scope as any other local identifier in
static variables
that block

C++ Programming: From Problem Analysis to Program Design, Sixth Edition 39 C++ Programming: From Problem Analysis to Program Design, Sixth Edition 40

//

Debugging: Function Overloading:


Using Drivers and Stubs An Introduction
Driver program: separate program used to test In a C++ program, several functions can have
a function the same name
When results calculated by one function are Function overloading: creating several
needed in another function, use a function functions with the same name
stub
Function signature: the name and formal
Function stub: a function that is not fully parameter list of the function
coded
Does not include the return type of the function

C++ Programming: From Problem Analysis to Program Design, Sixth Edition 41 C++ Programming: From Problem Analysis to Program Design, Sixth Edition 42

Function Overloading (contd.) Function Overloading (contd.)


Two functions are said to have different The parameter list supplied in a call to an
formal parameter lists if both functions have overloaded function determines which
either: function is executed
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
Overloaded functions must have different
function signatures
C++ Programming: From Problem Analysis to Program Design, Sixth Edition 43 C++ Programming: From Problem Analysis to Program Design, Sixth Edition 44

//

Functions with Default Parameters


Functions with Default Parameters
(contd.)
In a function call, the number of actual and All default parameters must be the rightmost
formal parameters must be the same parameters of the function
C++ relaxes this condition for functions with
default parameters
If a default parameter value is not specified:
You must omit all of the arguments to its right
Can specify the value of a default parameter in
the function prototype Default values can be constants, global
If you do not specify the value for a default variables, or function calls
parameter when calling the function, the Cannot assign a constant value as a default
default value is used value to a reference parameter
C++ Programming: From Problem Analysis to Program Design, Sixth Edition 45 C++ Programming: From Problem Analysis to Program Design, Sixth Edition 46

Summary Summary (contd.)


Functions (modules) divide a program into Function heading and the body of the function
manageable tasks are called the definition of the function
C++ provides standard, predefined functions A value-returning function returns its value via
Two types of user-defined functions: value- the return statement
returning functions and void functions A prototype is the function heading without the
Variables defined in a function heading are called body of the function
formal parameters User-defined functions execute only when they
Expressions, variables, or constant values in a are called
function call are called actual parameters Void functions do not have a data type
C++ Programming: From Problem Analysis to Program Design, Sixth Edition 47 C++ Programming: From Problem Analysis to Program Design, Sixth Edition 48

//

Summary (contd.) Summary (contd.)


Two types of formal parameters: Automatic variable: variable for which
A value parameter receives a copy of its memory is allocated on function/block entry
corresponding actual parameter and deallocated on function/block exit
A reference parameter receives the memory Static variable: memory remains allocated
address of its corresponding actual parameter throughout the execution of the program
Variables declared within a function (or block) C++ functions can have default parameters
are called local variables
Variables declared outside of every function
definition (and block) are global variables
C++ Programming: From Problem Analysis to Program Design, Sixth Edition 49 C++ Programming: From Problem Analysis to Program Design, Sixth Edition 50

Das könnte Ihnen auch gefallen