Download as ppt, pdf, or txt
Download as ppt, pdf, or txt
You are on page 1of 22

1

6
Functions

2008 Pearson Education, Inc. All rights reserved.

Function Components
A function consists of function
prototype, function header and
function body.
Functions often require more than one
piece of information to perform their
tasks, which is passed by parameters.
Parameters are specified in both the
function prototype and the function
header.

Function prototype
Also called a function declaration
Indicates to the compiler:
Name of the function
Type of data returned by the function
Parameters the function expects to receive
Types of those parameters
Order of those parameters

Example: int max(int, int, int)

Function Header
Same as function prototype but it also
includes the name of the parameters.
Example: int max(int x, int y, int z)
parameter names: x, y, z

Function Body
Function body begins with a left brace
following the function header and ends
with a right brace.
Ex:
int max(int x, int y, int z)
{ int maxValue = x;
if ( y > maxValue) maxValue = y;
if( z > maxValue) maxValue = z;
return maxValue }

Function Execution
A function is invoked by a function call
Following a call, statements within the
function body will execute
Ex: max (grade1, grade2, grade3)
argumets : grade1, grade2, grade3

Three ways to return control to the


calling statement:
If the function does not return a result
( return type is void):
Program flow reaches the function-ending right
brace or
Program executes the statement return;

If the function does return a result:


Program executes the statement,
return expression;
expression is evaluated and its value is returned to
the caller

Storage Classes and Scope


Identifiers are used as variable and
function names. Identifier properties,
Identifiers storage class
Determines the period during which that
identifier exists in memory

Identifiers scope
Determines where the identifier can be
referenced in a program

Variables
Local : These are variables defined
within a function. They will be visible
only within the function. They exist only
during the execution of a function
(exception static local variables).
Global: These are variables defined
outside of all the functions. They will be
visible in all the functions and they exist
during the entire execution.

6.5 Function Prototypes and Argument


Coercion (Cont.)

10

Function signature (or simply signature)


The portion of a function prototype that includes the
name of the function and the types of its parameters
Does not specify the functions return type
Functions in the same scope must have unique
signatures
The scope of a function is the region of a program
in which the function is known and accessible

2008 Pearson Education, Inc. All rights reserved.

6.5 Function Prototypes and Argument


Coercion (Cont.)

11

Argument Coercion
Forcing arguments to the appropriate types specified by
the corresponding parameters
For example, calling a function with an integer argument,
even though the function prototype specifies a double
argument
Ex: double sqrt( double x)
int x = 10;
sqrt( x);
The function will still work correctly

2008 Pearson Education, Inc. All rights reserved.

Explicit and Implicit Conversion between


data types

12

Unary cast operator (explicit conversion)


Creates a temporary copy of its operand with a different data
type . Example: int total = 5;
static_cast< double > ( total )
Creates temporary floating-point copy of total

Assignment operator( explicit conversion)


- Lower data type may be converted to
higher data type
Ex:

int x =5; double y;

y = x;

- Higher data type may be converted to


lower data type
Ex:

int x; double y = 5.6;

x = y;

2008 Pearson Education, Inc. All rights reserved.

13

Argument coercion occurs as if the argument value were


being assigned directly to the parameter variable.

Converting a value to a lower fundamental type


Will likely result in the loss of data or incorrect values
Can only be performed explicitly
By assigning the value to a variable of lower type (some
compilers will issue a warning in this case) or
By using a cast operator

2008 Pearson Education, Inc. All rights reserved.

14

Promotion (implicit conversion)


Converting a value (e.g. int) to another data type (e.g.
double) to perform a calculation

C++ Promotion Rules


Apply to expressions containing values of two or more data
types
Such expressions are also referred to as mixed-type
expressions
Each value in the expression is promoted to the highest
type in the expression
Temporary version of each value is created and used for
the expression
Original values remain unchanged

2008 Pearson Education, Inc. All rights reserved.

15

Fig. 6.6 | Promotion hierarchy for fundamental data types.

2008 Pearson Education, Inc. All rights reserved.

16

6.6 C++ Standard Library Header Files


C++ Standard Library header files
Each contains a portion of the Standard Library
Function prototypes for the related functions
Definitions of various class types and functions
Constants needed by those functions

Instruct the compiler on how to interface with library


and user-written components
Header file names ending in .h
Are old-style header files
Superseded by the C++ Standard Library header files

2008 Pearson Education, Inc. All rights reserved.

17

Fig. 6.7 | C++ Standard Library header files. (Part 1 of 4)

2008 Pearson Education, Inc. All rights reserved.

18

Fig. 6.7 | C++ Standard Library header files. (Part 2 of 4)

2008 Pearson Education, Inc. All rights reserved.

19

Fig. 6.7 | C++ Standard Library header files. (Part 3 of 4)

2008 Pearson Education, Inc. All rights reserved.

20

Fig. 6.7 | C++ Standard Library header files. (Part 4 of 4)

2008 Pearson Education, Inc. All rights reserved.

Math Library Functions


Global functions
Do not belong to a particular class
Have function prototypes placed in header
files
Can be reused in any program that includes the
header file and that can link to the functions
object code

Example: sqrt in <cmath> header file


sqrt( 900.0 )
All functions in <cmath> are global functions

Fig. 6.2 | Math library functions.

You might also like