MG Mud User | 88f1247 | 2016-06-24 23:31:02 +0200 | [diff] [blame^] | 1 | CONCEPT |
| 2 | references |
| 3 | |
| 4 | DESCRIPTION |
| 5 | Call by reference can be used to have a function that passes |
| 6 | more than one value to the caller, without using arrays that |
| 7 | have to be unpacked thereinafter. |
| 8 | There is nothing special to declare in the calling function, |
| 9 | you simply do an assignment to a parameter of the function. |
| 10 | The caller has to pass references explicitely; this is done by |
| 11 | prefixing an lvalue with '&' . |
| 12 | To pass a reference to an element of an array, you have to |
| 13 | enclose the indexed lvalue in round brackets. |
| 14 | |
| 15 | EXAMPLE |
| 16 | |
| 17 | void assign(mixed destination, mixed source) { |
| 18 | destination = source; |
| 19 | } |
| 20 | |
| 21 | void extract_number(int destination, string source) { |
| 22 | sscanf(source, "%d", destination); |
| 23 | } |
| 24 | |
| 25 | void test() { |
| 26 | int i; |
| 27 | float f; |
| 28 | mixed *a; |
| 29 | |
| 30 | extract_number(&i, "42 palantirs"); |
| 31 | assign(&f, 3.141592653589793); |
| 32 | assign(&a, ({ i, f })); |
| 33 | assign(&(a[<0..<1]), ({1,2,3,"sink","x","y","x"})); |
| 34 | assign(&(a[5][0]), 'w'); |
| 35 | assign(&(a[5][<1]), 'g'); |
| 36 | printf("%O", a)); |
| 37 | } |
| 38 | |
| 39 | ({ /* sizeof() == 9 */ |
| 40 | 42, |
| 41 | 3.14159, |
| 42 | 1, |
| 43 | 2, |
| 44 | 3, |
| 45 | "wing", |
| 46 | "x", |
| 47 | "y", |
| 48 | "x" |
| 49 | }) |