Parameter Passing and Pointers Parameter passing and functions I: - - PowerPoint PPT Presentation

parameter passing and pointers
SMART_READER_LITE
LIVE PREVIEW

Parameter Passing and Pointers Parameter passing and functions I: - - PowerPoint PPT Presentation

Parameter Passing and Pointers Parameter Passing and Pointers Outline Parameter Passing and Pointers Parameter passing and functions I: reference parameters call-by-value vs call-by-name call-by-name by using reference parameters Martin Emms


slide-1
SLIDE 1

Parameter Passing and Pointers

Parameter Passing and Pointers

Martin Emms November 25, 2020

Parameter Passing and Pointers Outline

Parameter passing and functions I: reference parameters call-by-value vs call-by-name call-by-name by using reference parameters what’s the story with structs and classes?

Parameter Passing and Pointers Outline

Reference parameters

Parameter Passing and Pointers Parameter passing and functions I: reference parameters call-by-value vs call-by-name

Call by Value

◮ When one function calls another, information has to be passed to the

called function. C/C++ adhere to what is termed a call-by-value semantics for function calls.

◮ consider:

f( .. ) { g(int y) { int x; : : ... g(x) ... } : }

◮ call-by-value means:

◮ in the function call g(x), g’s parameter y is given the same value as f’s

variable x.

◮ g’s y and f’s x remain distinct variables. ◮ g may alter the value of y, but this will not alter f’s variable x.

slide-2
SLIDE 2

Parameter Passing and Pointers Parameter passing and functions I: reference parameters call-by-value vs call-by-name

Call by name (not C++)

◮ consider:

f( .. ) { g(int y) { int x; : : ... g(x) ... } : }

◮ contrast to a call-by-name semantics for function calls:

◮ the function call g(x) causes f’s variable x to become an input parameter

variable of the function g

◮ g’s y and f’s x do not remain distinct variables ◮ if g alters its input parameter variable, it will alter f’s variable x also. Parameter Passing and Pointers Parameter passing and functions I: reference parameters call-by-value vs call-by-name

call-by-value illustration

main() { int b, r; b = 2; r = power(b,2); cout << b << ’ ’ << r << ’\n’; b = 3; r = power(b,3); cout << b << ’ ’ << r << ’\n’; } int power(int input, int n) { int m; m = input; input = 1; for (int i = 1; i <= n; i = i + 1) { input = input * m; } return input; }

◮ When you run this code, you should see:

2 4 3 27

◮ main has b

power has input power called with power(b,2) value of b passed to power’s input. power does lots of updates to input but back in main, b has not changed.

Parameter Passing and Pointers Parameter passing and functions I: reference parameters call-by-name by using reference parameters

call-by-name

◮ call-by-name behaviour can be stipulated by using reference parameter

  • syntax. For any type T, a function can declare a parameter to have type

T&. T and T& are not really different types: its just telling the compiler about what semantics to use for parameter passing.

◮ f( .. ) {

g(int& y) { int x; : : y = ... ... g(x) ... : : } }

◮ g behaves as if it has been handed f’s variable x ◮ so once g has finished, the value of f’s variable x will have changed.

Parameter Passing and Pointers Parameter passing and functions I: reference parameters call-by-name by using reference parameters

call-by-name illustration

int power(int& input, int n); main() { int b, r; b = 2; r = power(b,2); cout << b << ’ ’ << r << ’\n’; b = 3; r = power(b,3); cout << b << ’ ’ << r << ’\n’; } int power(int& input, int n) { int m; m = input; input = 1; for (int i = 1; i <= n; i = i + 1) { input = input * m; } return input; }

◮ When you run this code, you should see:

4 4 27 27

◮ main has int b

power has int& input power called with power(b,2) in power input works as if it were b so back in main, b’s value has changed.

slide-3
SLIDE 3

Parameter Passing and Pointers Parameter passing and functions I: reference parameters call-by-name by using reference parameters

◮ designing power to use a reference parameter just being pointlessly

fiddly, but there are genuine practical applications

◮ one kind of case arises in situations where you really need multiple

return values

◮ another kind of case is when what you are passing around is some kind

  • f handle on a single particular resource, like a file stream and you really

don’t want copying: you pass a reference: my_stream_function(ifstream& f,...)

Parameter Passing and Pointers Parameter passing and functions I: reference parameters call-by-name by using reference parameters

use case I: multiple return values

◮ A function can only return return 1 value. ◮ some tasks involve ’returning’ 2 values:

any kind of splitting task : split a string into 2 pieces an category and its probability . . .

◮ solution:

  • 1. declare the function as having a reference parameter
  • 2. it is passed a variable
  • 3. it sets the variable to contain the ’extra’ result.
  • 4. it returns the ’other’ result

Parameter Passing and Pointers Parameter passing and functions I: reference parameters call-by-name by using reference parameters

The template for this kind of code is:

B f(.., A& p, ..); // 1 main() { A part1; B part2; : part2 = f(.., part1, ..); // 2 cout << part1 << part2 } B f(.., A& p, ..) { : // make some A value v p = v; // 3 : // make some B value b return b; // 4 }

Parameter Passing and Pointers Parameter passing and functions I: reference parameters call-by-name by using reference parameters

use case II: really don’t want copying

◮ a file stream is a ’handle’ on a particular area of disk-space, controlling

where the next output item will go, for example

◮ you might create the a stream in one function, but want to delegate

interactions with it to other function

◮ you really don’t the called function to work on a copy of the calling

functions file stream: this will probably mess up where the outputs appear on disk. This needs a reference parameter.

slide-4
SLIDE 4

Parameter Passing and Pointers Parameter passing and functions I: reference parameters call-by-name by using reference parameters

use case II continued

Here’s a minimum illustration:

#include <iostream> #include <fstream> using namespace std; void PrintToFile(ofstream& outstream); main() {

  • fstream out_stream;
  • ut_stream.open("thisfile");

PrintToFile(out_stream);

  • ut_stream << "This will be the second line in the file\n";
  • ut_stream.close();

} void PrintToFile(ofstream& str) { str << "This will be the first line in the file\n"; }

Parameter Passing and Pointers Parameter passing and functions I: reference parameters what’s the story with structs and classes?

structs and classes and reference params

◮ above all applies to instances of any user-defined types : when an object

  • f a user-defined class (or struct) is passed to a function, it is passed by

value, using copying.

◮ class C {

void f() { void g(C o) { ... C o; : }; : } g(o); }

◮ function f has an object C o, and calls g(o), the data at o’s address is

copied to g’s parameter’s address: whatever g does will not alter f’s

  • bject o.

◮ This is not the same behaviour as with Java, where the called function

will alter the calling function’s object

Parameter Passing and Pointers Parameter passing and functions I: reference parameters what’s the story with structs and classes?

to avoid the copying

◮ if you don’t want copying to happen, you declare the called function as

having a reference parameter

◮ class C {

void f() { void g(C& o) { public: C o;

  • .x = 2

int x; : } ... g(o); }; }

◮ this all applies to string and vector as well. It applies to everything