17

No Slide Titleelkadi/Slides/CSCE 106/CSCE 1001 Chapter 5… · Title: No Slide Title Author: Addison Wesley Created Date: 6/15/2016 12:25:00 PM

  • Upload
    others

  • View
    1

  • Download
    0

Embed Size (px)

Citation preview

Page 1: No Slide Titleelkadi/Slides/CSCE 106/CSCE 1001 Chapter 5… · Title: No Slide Title Author: Addison Wesley Created Date: 6/15/2016 12:25:00 PM
Page 2: No Slide Titleelkadi/Slides/CSCE 106/CSCE 1001 Chapter 5… · Title: No Slide Title Author: Addison Wesley Created Date: 6/15/2016 12:25:00 PM

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

Chapter 5

Functions for All Subtasks

Page 3: No Slide Titleelkadi/Slides/CSCE 106/CSCE 1001 Chapter 5… · Title: No Slide Title Author: Addison Wesley Created Date: 6/15/2016 12:25:00 PM

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

Page 4: No Slide Titleelkadi/Slides/CSCE 106/CSCE 1001 Chapter 5… · Title: No Slide Title Author: Addison Wesley Created Date: 6/15/2016 12:25:00 PM

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

Function to compute sum and average

Page 5: No Slide Titleelkadi/Slides/CSCE 106/CSCE 1001 Chapter 5… · Title: No Slide Title Author: Addison Wesley Created Date: 6/15/2016 12:25:00 PM

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

Function to compute sum and average (continued)

Page 6: No Slide Titleelkadi/Slides/CSCE 106/CSCE 1001 Chapter 5… · Title: No Slide Title Author: Addison Wesley Created Date: 6/15/2016 12:25:00 PM

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);

Page 7: No Slide Titleelkadi/Slides/CSCE 106/CSCE 1001 Chapter 5… · Title: No Slide Title Author: Addison Wesley Created Date: 6/15/2016 12:25:00 PM

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)

Page 8: No Slide Titleelkadi/Slides/CSCE 106/CSCE 1001 Chapter 5… · Title: No Slide Title Author: Addison Wesley Created Date: 6/15/2016 12:25:00 PM

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

Page 9: No Slide Titleelkadi/Slides/CSCE 106/CSCE 1001 Chapter 5… · Title: No Slide Title Author: Addison Wesley Created Date: 6/15/2016 12:25:00 PM

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

Data areas after call to computeSumAve (before execution)

Page 10: No Slide Titleelkadi/Slides/CSCE 106/CSCE 1001 Chapter 5… · Title: No Slide Title Author: Addison Wesley Created Date: 6/15/2016 12:25:00 PM

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

Data areas after execution of computeSumAve

Page 11: No Slide Titleelkadi/Slides/CSCE 106/CSCE 1001 Chapter 5… · Title: No Slide Title Author: Addison Wesley Created Date: 6/15/2016 12:25:00 PM

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

Page 12: No Slide Titleelkadi/Slides/CSCE 106/CSCE 1001 Chapter 5… · Title: No Slide Title Author: Addison Wesley Created Date: 6/15/2016 12:25:00 PM

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)

Page 13: No Slide Titleelkadi/Slides/CSCE 106/CSCE 1001 Chapter 5… · Title: No Slide Title Author: Addison Wesley Created Date: 6/15/2016 12:25:00 PM

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).

Page 14: No Slide Titleelkadi/Slides/CSCE 106/CSCE 1001 Chapter 5… · Title: No Slide Title Author: Addison Wesley Created Date: 6/15/2016 12:25:00 PM

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

Page 15: No Slide Titleelkadi/Slides/CSCE 106/CSCE 1001 Chapter 5… · Title: No Slide Title Author: Addison Wesley Created Date: 6/15/2016 12:25:00 PM

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

Page 16: No Slide Titleelkadi/Slides/CSCE 106/CSCE 1001 Chapter 5… · Title: No Slide Title Author: Addison Wesley Created Date: 6/15/2016 12:25:00 PM

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

Function to order three numbers

Page 17: No Slide Titleelkadi/Slides/CSCE 106/CSCE 1001 Chapter 5… · Title: No Slide Title Author: Addison Wesley Created Date: 6/15/2016 12:25:00 PM

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

Function to order three numbers (continued)