1.reference parameter
The following two functions are equivalent and are only called in a different way:
1>
1 int Reset (int i) {2 i =; 3 return} 5 6 int{8 int j=0; 9 j = Reset (j); COUT&L t;<j<<endl;11 System ("PAUSE"); return 0;
2>
1 void Reset (int &i) {2 i =} 4 5 int{7 int Reset (j); 9 Cout<<j<<
ENDL;10 System ("PAUSE"
); return 0;
function differences, the first must have a return value, the second does not have to.
Called when the difference is in line 9th.
The void reset () function references parameter I just another name of the variable J, which is essentially the variable J, which does not require a return value, and changing the contents of I is equivalent to changing the contents of J.
2. Implementing a function With reference parameters returns multiple results
As we all know, a function can only have one return value.
However, the effect of returning multiple results can be achieved by reference parameter.
1 //find the first occurrence of a character Pos, and draw a total number of occurrences occurs2 3#include <iostream>4 5 using namespacestd;6 7 string:: Size_type Find_char (Const string&s,CharCstring:: Size_type &occurs) {8Auto ret =s.size ();9Occurs =0;Ten for(Decltype (ret) i =0; I<s.size (); ++i) { One if(s[i]==B) { A if(ret = =s.size ()) { -RET =i; - } the++occurs; - } - } - returnret; + } - + intMain () A { at strings ="CASUTXODOIHSDFD"; - string:: Size_type occurs=0; -Cout<<find_char (s),'D', POS) <<Endl; -cout<<occurs<<Endl; -System"PAUSE"); - return 0; in}
Output
7 3
Where occurs is the referenced parameter, its value is changed when the function is called.
"C + +" reference parameter-reference parameter