28
Parameter Passing Parameter Passing Mechanisms Mechanisms Reference Parameters Reference Parameters §10.1 - §10.3 §10.1 - §10.3 1

Parameter Passing Mechanisms

Embed Size (px)

DESCRIPTION

Parameter Passing Mechanisms. Reference Parameters §10.1 - §10.3. 1. Find/locate IP address. ipv6 16 bytes. Problem. - PowerPoint PPT Presentation

Citation preview

Page 1: Parameter Passing Mechanisms

Parameter Passing Parameter Passing MechanismsMechanisms

Reference ParametersReference Parameters

§10.1 - §10.3§10.1 - §10.3

1

Page 2: Parameter Passing Mechanisms

ProblemProblem

Using OCD, design and implement a function that, given a string containing an IP address, decomposes it into its four network/host information blocks (aaa, bbb, ccc, ddd) and returns these values.

2

IPv4 (Internet Protocol) addresses are expressed using "dotted decimal" notation:

aaa.bbb.ccc.dddwhere aaa, bbb, ccc, and ddd are 1-byte (8-bit) integers that provide network or host information.

ipv616 bytes

Find/locate IP address

Page 3: Parameter Passing Mechanisms

Preliminary AnalysisPreliminary Analysis

Our function can receive the IP address through a string parameter.

This problem requires that our function somehow communicate four values (the network/host information blocks ) back to its caller.

A function cannot return multiple values — the return statement only returns one value:

return expression;

3

Page 4: Parameter Passing Mechanisms

BehaviorBehavior

Our subprogram should receive from its caller an IP address (a string). It should compute and pass back its four network/host information blocks or halt execution if it did not receive a valid IP address.

4

Page 5: Parameter Passing Mechanisms

ObjectsObjects

Description Type Name

IP address string ipAddr

first block string info1

second block string info2

third block string info3

fourth block string info4

5

Movement

to

back

back

back

back

Where?

main() or in

function

Page 6: Parameter Passing Mechanisms

OperationsOperations

Description Predefined? Library? Name

6

receive a stringyes built-in none

select part of a yes string substr()

string

search a string yes string find()

halt if error yes cassert assert()

pass back 4strings

yes built-in ??

Page 7: Parameter Passing Mechanisms

AlgorithmAlgorithm

0. Receive ipAddress from caller and declare four variables info1, info2, info3, and info4.

1. Fill info1 with appropriate substring of ipAddr or halt if it can't be found.

2. Fill info2 with appropriate substring of ipAddr or halt if it can't be found.

3. Fill info3 with appropriate substring of ipAddr or halt if it can't be found.

4. Fill info4 with appropriate substring of ipAddr or halt if it can't be found.

7

Page 8: Parameter Passing Mechanisms

DiscussionDiscussion

Since a function cannot return 4 strings, we will instead require the caller to pass it four string variables as arguments, which our function will then assign values.Parameters used up to now are called value parameters; they are built as copies of their arguments.Changing a value parameter changes the copy,not its corresponding argument.So, we need a different kind of parameter.

8

Page 9: Parameter Passing Mechanisms

Solution: Reference Parameters

A reference parameter is an alias (i.e., another name) for its corresponding argument.They share memory locations.

Changing the value of a reference parameter changes the value of its corresponding argument.

In C++, reference parameters are declared with an ampersand (&) following the parameter’s type (and before its name).

9

& is also the"address of"

operator

Page 10: Parameter Passing Mechanisms

CodingCoding

void chopIPAddress(string ipAddr, // value: TO string & info1, // reference: BACK string & info2, // reference: BACK string & info3, // reference: BACK string & info4) // reference: BACK{ int dot1 = ipAddr.find(".", 0); assert(dot1 != string::npos); info1 = ipAddr.substr(0, dot1);

int dot2 = ipAddr.find(".", dot1 + 1); assert(dot2 != string::npos); info2 = ipAddr.substr(dot1 + 1, dot2 - dot1 - 1);

int dot3 = ipAddr.find(".", dot2 + 1); assert(dot3 != string::npos); info3 = ipAddr.substr(dot2 + 1, dot3 - dot2 - 1);

assert(ipAddr.find(".", dot3 + 1) == string::npos); info4 = ipAddr.substr(dot3 + 1, ipAddr.size() - dot3 - 1);}

10

Page 11: Parameter Passing Mechanisms

TestingTesting

The caller must now supply a variable for each reference parameter, to be "filled in" by the function.

11

cout << "Network/Host blocks are:\n" << part1 << endl << part2 << endl << part3 << endl << part4 << endl;

cout << "Enter an IP address: ";string ipAddress, part1, part2, part3, part4;cin >> ipAddress;

chopIPAddress(ipAddress, part1, part2, part3, part4);

Page 12: Parameter Passing Mechanisms

NotesNotesWhen function chopIDAddress() is called:

– ipAddr is allocated a memory location and a copy of the argument ipAddress is stored there

– Each of the parameters info1, info2, info3, info4 is an alias of the corresponding argument — part1, part2, part3, part4; they share the same memory location; that is,info1 and part1 are names of the same memory location, as areinfo2 and part2, info3 and part3,info4 and part4.

12

Page 13: Parameter Passing Mechanisms

0. Before the function 0. Before the function callcall

Memory

ipAddress

part1

part2

part3

153.106.4.23

part4

cout << "Enter an IP address: ";string ipAddress, part1, part2, part3, part4;cin >> ipAddress;

13

????

Page 14: Parameter Passing Mechanisms

1. ipAddr is created as a

copy of ipAddress

Memory

ipAddress

part1

part2

part3

ipAddr

14

153.106.4.23

153.106.4.23

part4

chopIPAddress(ipAddress, part1, part2 part3, part4);

????

Page 15: Parameter Passing Mechanisms

2. info1, ..., info4 are created as aliases for

part1, ..., part4

Memory

part1

part2

part3

15

ipAddress 153.106.4.23

153.106.4.23

part4

info1

info2

info3

info4

ipAddr

chopIPAddress(ipAddress, part1, part2 part3, part4);

????

Page 16: Parameter Passing Mechanisms

3. The function computes

info1, changing part1

16

Memory

part1

part2

part3

ipAddress 153.106.4.23

153.106.4.23

part4

info1

info3

info4

ipAddr

153

int dot1 = ipAddr.find(".", 0); assert(dot1 != string::npos); info1 = ipAddr.substr(0, dot1);

info2???

Page 17: Parameter Passing Mechanisms

4. The function computes

info2, changing part2

17

Memory

part1

part2

part3

ipAddress 153.106.4.23

153.106.4.23

part4

info1

info2

info3

info4

ipAddr

153

106

int dot2 = ipAddr.find(".", dot1 + 1); assert(dot2 != string::npos); info2 = ipAddr.substr(dot1 + 1, dot2 - dot1 - 1);

??

Page 18: Parameter Passing Mechanisms

5. The function computes

info3, changing part3

18

Memory

part1

part2

part3

ipAddress 153.106.4.23

153.106.4.23

part4

info1

info2

info3

info4

ipAddr

153

106

4

int dot3 = ipAddr.find(".", dot2 + 1); assert(dot3 != string::npos); info3 = ipAddr.substr(dot2 + 1, dot3 - dot2 - 1);

?

Page 19: Parameter Passing Mechanisms

6. The function computes

info4, changing part4

19

Memory

part1

part2

part3

ipAddress 153.106.4.23

153.106.4.23

part4

info1

info2

info3

info4

ipAddr

153

106

4

23

assert(ipAddr.find(".", dot3 + 1) == string::npos); info4 = ipAddr.substr(dot3 + 1, ipAddr.size() - dot3 - 1);

Page 20: Parameter Passing Mechanisms

7. The function returns, destroying

all parameters

Memory

20

part1

part2

part3

ipAddress 153.106.4.23

part4

153

106

4

23

. . .

}

info1

info2

info3

info4

ipAddr153.106.4.23

Page 21: Parameter Passing Mechanisms

8. part1, ... , part4now contain the information!

21

Memory

part1

part2

part3

ipAddress 153.106.4.23

part4

153

106

4

23

cout << "Network/Host blocks are:\n" << part1 << endl << part2 << endl << part3 << endl << part4 << endl

Page 22: Parameter Passing Mechanisms

NotesNotes

By default, parameters are value parameters.

Reference parameters are specified by placing an ampersand after the parameters type.

Reference parameters must be specified in both a function’s prototype and its definition, or a linking error will occur.

Variables must be passed as arguments for reference parameters to fill, or a compiler error will occur.

22

Page 23: Parameter Passing Mechanisms

An Alternative to Value An Alternative to Value ParametersParameters

Copying argument ipAddress consumes time.

Creating an alias for an argument takes almost no time.

We could speed up calls to our function by making parameter ipAddr a reference parameter.

However, we then run the risk of changing ipAddress if we mistakenly change ipAddr. 23

Page 24: Parameter Passing Mechanisms

Constant reference parameters are reference parameters whose declaration is preceded by the keyword const.

void chopIPAddress(const string & ipAddr, // TO string & info1, // BACK string & info2, // BACK string & info3, // BACK string & info4) // BACK// ... Const reference parameters are read-only reference parameters -- aliases of their arguments -- but they cannot be changed.

24

Page 25: Parameter Passing Mechanisms

0. Before the function 0. Before the function callcall

25

Memory

ipAddress

part1

part2

part3

153.106.4.23

part4

????

Page 26: Parameter Passing Mechanisms

1. ipAddr is created as a

const reference of originalipAddr

26

read-only

Memory

ipAddress

part1

part2

part3

153.106.4.23

part4

Page 27: Parameter Passing Mechanisms

2. The rest of the function proceeds as

before, except:• all accesses to ipAddr now access ipAddress instead of the copy.

• Any attempt to change ipAddr will generate a compiler error (which makes sense, since its movement is IN, not OUT).

27

Page 28: Parameter Passing Mechanisms

DiscussionDiscussion

28

Copying time is not significant for simple types (e.g., int, char, double, ...), but it is significant for class types (e.g., string, RandomInt, ...).So use value parameters to store simple type arguments whose movement is TO.

Use reference parameters for arguments whose movement is BACK or TO & BACK

Use const reference parameters to store class arguments whose movement is TO.