Function ---- Beginning Visual C #
When the function transfer parameters are passed through the reference method, the syntax is different // in cppvoid doubleVal (int & val ){//...} int main () {int val = 20; doubleVal (val); cout <val <endl; // output: 40} // in c sharpstatic void doubleVal (ref int val) // use ref to identify {val * = 2;} static void Main (string [] args) {int val = 20; doubleVal (ref val); Console. writeLine (val); // output: 40} out keyword. There is no similar keyword in c ++ or the usage of out keyword is similar to that of ref, but there are two differences, 1) when using the ref parameter, pass the parameter without a value assignment Is invalid. Compilation fails. However, the out parameter is valid. 2) When the out parameter is used, the parameter is treated as not assigned a value, and a compilation error occurs when the value is called. Static void doubleVal (out int val) {val * = 2; // you want to use a value in val. // However, val is considered to be unassigned, therefore, a compilation error occurs.} static void Main (string [] args) {int val = 3; doubleVal (out val); Console. writeLine (val); // output: 40} for non-quantitative parameter input keywords: param, the specific implementation is as follows: static int sum (param int [] num) // note that the param parameter must be at the end of the parameter list {int sumVal = 0; foreach (int n in num) {sum + = n ;} return sum;} the variable scope detection in c # is more rigorous. For example, the Code similar to the following can be compiled in c ++ and run successfully. In c #, compilation fails because no initialization variable is called in the same scope. String text; for (int I = 0; I <1; I ++) {text = "hello";} Console. writeLine (text); // in cpp: cout <text <endl; function overload is similar to that in c ++, but because c # references ", the ref keyword is introduced. Therefore, functions that reference and do not reference parameters can also be overloaded in c #, which is unreasonable in c ++. For example: // in c # static void showDouble (ref int val) {} static void showDouble (int val) {} // in cpp is errorvoid showDouble (int & val) {} void showDouble (int val) {} for functions in c #, there is also the delegate keyword. The meaning of this keyword is used to create something that can be used to represent other functions. This is a bit like a pointer function in c. For example: // in c # delegate double processDelegate (double param1, double param2); static double Multiply (double param1, double param2) {return param1 * param2 ;} static double Divide (double param1, double param2) {return param1/param2;} static void Main (string [] args) {processDelegate process; process = new processDelegate (Multiply); Console. writeLine ("{0}", process (3, 4); process = new processDelegate (Divide); Console. writeLine ("{0}", process (3, 4);} // in cpp # include <iostream> using namespace std; int multi (int a, int B) {return a * B;} int add (int a, int B) {return a + B;} int main () {int (* fn) (int, int ); fn = multi; cout <"multi" <fn (2, 3) <endl; fn = add; cout <"add" <add (2, 3) <endl ;}