parameter passing and pointers
play

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


  1. 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 what’s the story with struct s and class es? November 25, 2020 Parameter Passing and Pointers Parameter Passing and Pointers Outline 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: Reference parameters 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 .

  2. Parameter Passing and Pointers Parameter Passing and Pointers Parameter passing and functions I: reference parameters Parameter passing and functions I: reference parameters call-by-value vs call-by-name call-by-value vs call-by-name Call by name (not C++) call-by-value illustration ◮ consider: main() { ◮ When you run this code, you should see: int b, r; f( .. ) { g(int y) { b = 2; int x; : r = power(b,2); 2 4 cout << b << ’ ’ << r << ’\n’; : b = 3; 3 27 ... g(x) ... } r = power(b,3); cout << b << ’ ’ << r << ’\n’; : ◮ main has b } } power has input int power(int input, int n) { ◮ contrast to a call-by-name semantics for function calls: int m; power called with power(b,2) m = input; input = 1; value of b passed to power ’s input . ◮ the function call g(x) causes f ’s variable x to become an input parameter for (int i = 1; i <= n; i = i + 1) { variable of the function g power does lots of updates to input input = input * m; } ◮ g ’s y and f ’s x do not remain distinct variables but back in main , b has not changed. return input; ◮ if g alters its input parameter variable, it will alter f ’s variable x also. } Parameter Passing and Pointers Parameter Passing and Pointers Parameter passing and functions I: reference parameters Parameter passing and functions I: reference parameters call-by-name by using reference parameters call-by-name by using reference parameters call-by-name call-by-name illustration ◮ call-by-name behaviour can be stipulated by using reference parameter int power(int& input, int n); syntax. For any type T, a function can declare a parameter to have type main() { ◮ When you run this code, you should see: T& . T and T& are not really different types: its just telling the compiler int b, r; b = 2; about what semantics to use for parameter passing. r = power(b,2); 4 4 cout << b << ’ ’ << r << ’\n’; ◮ f( .. ) { g(int& y) { 27 27 b = 3; r = power(b,3); int x; : cout << b << ’ ’ << r << ’\n’; ◮ main has int b : y = ... } power has int& input ... g(x) ... : int power(int& input, int n) { : int m; power called with power(b,2) m = input; } } input = 1; in power input works as if it were b for (int i = 1; i <= n; i = i + 1) { ◮ g behaves as if it has been handed f ’s variable x so back in main , b ’s value has changed. input = input * m; } ◮ so once g has finished, the value of f ’s variable x will have changed. return input; }

  3. Parameter Passing and Pointers Parameter Passing and Pointers Parameter passing and functions I: reference parameters Parameter passing and functions I: reference parameters call-by-name by using reference parameters call-by-name by using reference parameters use case I: multiple return values ◮ designing power to use a reference parameter just being pointlessly ◮ A function can only return return 1 value. fiddly, but there are genuine practical applications ◮ some tasks involve ’returning’ 2 values: ◮ one kind of case arises in situations where you really need multiple any kind of splitting task : split a string into 2 pieces an category and its probability return values . ◮ another kind of case is when what you are passing around is some kind . . of handle on a single particular resource, like a file stream and you really ◮ solution: don’t want copying: you pass a reference: 1. declare the function as having a reference parameter my_stream_function(ifstream& f,...) 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 Pointers Parameter passing and functions I: reference parameters Parameter passing and functions I: reference parameters call-by-name by using reference parameters call-by-name by using reference parameters use case II: really don’t want copying The template for this kind of code is: B f(.., A& p, ..); // 1 main() { A part1; B part2; ◮ a file stream is a ’handle’ on a particular area of disk-space, controlling : where the next output item will go, for example part2 = f(.., part1, ..); // 2 ◮ you might create the a stream in one function, but want to delegate cout << part1 << part2 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 B f(.., A& p, ..) { : appear on disk. This needs a reference parameter. // make some A value v p = v; // 3 : // make some B value b return b; // 4 }

  4. Parameter Passing and Pointers Parameter Passing and Pointers Parameter passing and functions I: reference parameters Parameter passing and functions I: reference parameters call-by-name by using reference parameters what’s the story with struct s and class es? use case II continued struct s and class es and reference params Here’s a minimum illustration: #include <iostream> #include <fstream> ◮ above all applies to instances of any user-defined types : when an object using namespace std; of a user-defined class (or struct) is passed to a function, it is passed by void PrintToFile(ofstream& outstream); value, using copying. main() { ◮ class C { void f() { void g(C o) { ... C o; : ofstream out_stream; out_stream.open("thisfile"); }; : } g(o); PrintToFile(out_stream); } ◮ function f has an object C o , and calls g(o) , the data at o ’s address is out_stream << "This will be the second line in the file\n"; copied to g ’s parameter’s address: whatever g does will not alter f ’s out_stream.close(); object o . } ◮ This is not the same behaviour as with Java, where the called function void PrintToFile(ofstream& str) { will alter the calling function’s object 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 struct s and class es? 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; o.x = 2 int x; : } ... g(o); }; } ◮ this all applies to string and vector as well. It applies to everything

Download Presentation
Download Policy: The content available on the website is offered to you 'AS IS' for your personal information and use only. It cannot be commercialized, licensed, or distributed on other websites without prior consent from the author. To download a presentation, simply click this link. If you encounter any difficulties during the download process, it's possible that the publisher has removed the file from their server.

Recommend


More recommend