0% found this document useful (0 votes)
5 views17 pages

CSCE 1001 Chapter 5

cs

Uploaded by

abdelrahmansendo
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views17 pages

CSCE 1001 Chapter 5

cs

Uploaded by

abdelrahmansendo
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

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

Corresponds to
Actual Argument Formal Argument
x num1 (input)
y num2 (input)
sum sum (output)
mean 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)

Copyright © 2015 Pearson Education, Ltd.. All rights reserved.

You might also like