CSCE 1811 CSCE 181 A Brief History of Computer Science Jinxiang Chai.
No Slide Titleelkadi/Slides/CSCE 106/CSCE 1001 Chapter 5… · Title: No Slide Title Author:...
Transcript of No Slide Titleelkadi/Slides/CSCE 106/CSCE 1001 Chapter 5… · Title: No Slide Title Author:...
Copyright © 2015 Pearson Education, Ltd.. All rights reserved.
Chapter 5
Functions for All Subtasks
Copyright © 2015 Pearson Education, Ltd.. All rights reserved.
Value and Reference Parameters
Functions can
return no value
type void
return exactly one value
return statement
function type
return more than one value
type void
reference parameters
Copyright © 2015 Pearson Education, Ltd.. All rights reserved.
Function to compute sum and average
Copyright © 2015 Pearson Education, Ltd.. All rights reserved.
Function to compute sum and average (continued)
Copyright © 2015 Pearson Education, Ltd.. All rights reserved.
Function computeSumAve
Two input parameters
num1, num2
Two output parameters
sum, average
& indicates output parameters
Function call
computeSumAve(x, y, sum, mean);
Copyright © 2015 Pearson Education, Ltd.. All rights reserved.
Argument Correspondence
Actual Argument
x
y
sum
mean
Corresponds to
Formal Argument
num1 (input)
num2 (input)
sum (output)
average (output)
Copyright © 2015 Pearson Education, Ltd.. All rights reserved.
Call-by-Value and Call-by-Reference
Parameters
& between type and identifier defines a
parameter as output mode (pass by reference)
no & in a parameter’s declaration identifies input
mode (pass by value)
Compiler uses info in parameter declaration list
to set up correct argument-passing
mechanism
Copyright © 2015 Pearson Education, Ltd.. All rights reserved.
Data areas after call to computeSumAve (before execution)
Copyright © 2015 Pearson Education, Ltd.. All rights reserved.
Data areas after execution of computeSumAve
Copyright © 2015 Pearson Education, Ltd.. All rights reserved.
Notes on Call-by-Reference
Place the & only in the formal parameter list -not in the actual parameter list
Place the & also in the prototype:
void computeSumAve(float, float, float&, float&);
Note that this is a void function
Copyright © 2015 Pearson Education, Ltd.. All rights reserved.
When to Use a Reference or a Value
Parameter
If information is to be passed into a function and
doesn’t have to be returned or passed out of the
function, then the formal parameter representing
that information should be a value parameter
(input parameter)
Copyright © 2015 Pearson Education, Ltd.. All rights reserved.
When to Use a Reference or a Value
Parameter
If information is to be returned to the calling
function through a parameter, then the formal
parameter representing that information must be
a reference parameter (output parameter).
Copyright © 2015 Pearson Education, Ltd.. All rights reserved.
When to Use a Reference or a Value
Parameter
If information is to be passed into a function,
perhaps modified, and a new value returned,
then the formal parameter representing that
information must be a reference parameter
(input/output parameter)
Note: C++ does not distinguish between output
parameters and input/output parameters
Copyright © 2015 Pearson Education, Ltd.. All rights reserved.
Program Style
Formal parameter list often written on multiple
lines to improve readability.
Value parameters first, followed by reference
parameters
Comment each parameter to identify its use
Copyright © 2015 Pearson Education, Ltd.. All rights reserved.
Function to order three numbers
Copyright © 2015 Pearson Education, Ltd.. All rights reserved.
Function to order three numbers (continued)