reference parameters (6.2.3) we might need a function to return more than one value find roots of a...

12
Reference parameters (6.2.3) We might need a function to return more than one value Find roots of a quadratic 2 return values. What are they? Get first and last name of a user and return them 2 return values Functions are limited to one return value using return When you need to return several values back from a function Use reference parameters The idea is that when the value of the parameter is modified, function modifies the value of the corresponding argument as well

Upload: alaina-harris

Post on 14-Dec-2015

215 views

Category:

Documents


1 download

TRANSCRIPT

Page 1: Reference parameters (6.2.3) We might need a function to return more than one value Find roots of a quadratic 2 return values. What are they? Get first

Reference parameters (6.2.3)We might need a function to return more than one value

Find roots of a quadratic 2 return values. What are they?

Get first and last name of a user and return them 2 return values

Functions are limited to one return value using returnWhen you need to return several values back from a

functionUse reference parameters The idea is that when the value of the parameter is modified,

function modifies the value of the corresponding argument as well

Page 2: Reference parameters (6.2.3) We might need a function to return more than one value Find roots of a quadratic 2 return values. What are they? Get first

Value Parameters - Pass by valueThe parameters we have seen so far are value

parameterstheir arguments are passed by valueif you change the value of a parameter in function,

corresponding argument does NOT change in the caller function Example: see passbyvalue.cpp (not in book) parameter a changes in function average, but corresponding

argument, num1, does not change in main

Enter two integers: 10 15in main before calling average: num1 = 10, num2 = 15beginning of function average: a = 10, b = 15end of function average: a = 25, b = 15average is 12.5in main after calling average: num1 = 10, num2 = 15

Page 3: Reference parameters (6.2.3) We might need a function to return more than one value Find roots of a quadratic 2 return values. What are they? Get first

Reference Parameters – Pass by ReferenceReference parameters are used to change the value of

the argument in the caller function as wellampersand (&) between type and name

void myfunction (int & num)

if you change the value of a reference parameter in function, corresponding argument changes in the caller function

Argument of a reference parameter must be a variable This is a reasonable rule, because otherwise its value won’t change

See passbyreference.cpp (not in book) parameter a changes in function average; corresponding argument, num1, changes in main as well

Enter two integers: 10 15in main before calling average: num1 = 10, num2 = 15beginning of function average: a = 10, b = 15end of function average: a = 25, b = 15average is 12.5in main after calling average: num1 = 25, num2 = 15

Page 4: Reference parameters (6.2.3) We might need a function to return more than one value Find roots of a quadratic 2 return values. What are they? Get first

Underlying MechanismsFor value parameters, the arguments’ values are copied

into parametersarguments and parameters have different memory locations

double average (int a, int b)

average(num1, num2)

10 15

10 15

copy value copy value

main function

Page 5: Reference parameters (6.2.3) We might need a function to return more than one value Find roots of a quadratic 2 return values. What are they? Get first

Underlying Mechanisms

For reference parameters, the parameter and the argument share the same memory locationparameter is an alias of argument

double average (int & a, int b)

average(num1, num2)10

15

15

refers to the same memory location

copy value

main function

Page 6: Reference parameters (6.2.3) We might need a function to return more than one value Find roots of a quadratic 2 return values. What are they? Get first

Example: Swap Write a function that swaps the values of its two integer parameters

void swap(int & a, int & b){ int temp;

temp = a; a = b; b = temp;

} How do we use this in main?

int a=5, b=8;swap(a,b); // a becomes 8, b becomes 5swap(a,5); // syntax error, arguments must be variables

Page 7: Reference parameters (6.2.3) We might need a function to return more than one value Find roots of a quadratic 2 return values. What are they? Get first

ExamplesWhat’s prototype for a void function that takes a string as

parameter and returns the number of vowels and consonants in it?

void letters(string s, int & vowels, int & consonants);

What’s prototype for a void function that returns the number of hours, minutes in N seconds. Where N is a parameter?

void TimeConversion(int N, int & hours, int & minutes);

Page 8: Reference parameters (6.2.3) We might need a function to return more than one value Find roots of a quadratic 2 return values. What are they? Get first

Reference Parameters are not only to return multiple values

Even if you have a single value to return, you may prefer to return it as a reference parameter, not as the return value of the function.ToLower function, defined in strutils, changes its argument

to all lowercase. It is actually a void function, i.e. does not return anything as the function’s return value

Prototype is void ToLower(string & s);

Example usestring s = "HeLLo";ToLower(s); // s becomes “hello”

Page 9: Reference parameters (6.2.3) We might need a function to return more than one value Find roots of a quadratic 2 return values. What are they? Get first

Example (See roots.cpp)Roots of a quadratic equationax2 + bx + c = 0

what could be a prototype?

void roots(double a, double b, double c, double & r1, double & r2);

What happens if one root? no roots?

how will you inform the caller function about the number of roots? necessary in order to let the caller function to interpret arguments for r1 and r2

Solution: let the function return (as the return value) an integer value for the number of roots So, the prototype becomes

int roots(double a, double b, double c, double & r1, double & r2);

Page 10: Reference parameters (6.2.3) We might need a function to return more than one value Find roots of a quadratic 2 return values. What are they? Get first

Parameter Passing: const-referencePass by value (value parameters) has overheads

copying the valuememory allocation for both parameter and argument

Sometimes we want to avoid the overhead of making the copy, but we don’t want to allow the argument to be changed.const-reference parameters avoid copies, but cannot be

changed in the function trying to change a const-reference parameter is a syntax error

defined with const before a reference parameter

void demo (const int & num, const string & s);

Page 11: Reference parameters (6.2.3) We might need a function to return more than one value Find roots of a quadratic 2 return values. What are they? Get first

ExampleCount number of occurrences of a letter in a string

Write a function for itLook at every character in the string

int LetterCount(const string& s, const string& letter)// post: return number of occurrences of letter in s{ int k, count = 0, len = s.length(); for(k=0; k < len; k++) { if (s.substr(k,1) == letter) { count++; } } return count;}

Calls below are legal

int ec = LetterCount("elephant", "e");string s = "hello"; cout << LetterCount(s, "a");

Page 12: Reference parameters (6.2.3) We might need a function to return more than one value Find roots of a quadratic 2 return values. What are they? Get first

General rules for ParametersConst-reference parameters allow constants and literals to be

passedFor example, “elephant” cannot be passed as an argument for a

reference parameter, but it is ok with const-reference

Some good-programming tips Built-in types (int, double, bool, char) - pass by value unless you change

it and return from the functionAll other types - pass by const-reference unless you change it and return

from the functionWhen you change and want to return the changed value, use reference

parameters

What about using classes as the parameter type?use reference parameters if you are changing the state of the parameter

object that is why we used reference parameters for Robot objects

use const reference if you are not changing the state of the parameter object