Function ---- Beginning Visual C #,
For more articles, see my personal homepage: zhongxiewei.com.
When the function transmission 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}
The out keyword does not have similar keywords in c ++ or the usage of the out keyword is similar to that of ref, but there are two differences. 1) when using the ref parameter, passing parameters 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. As follows:
Static void doubleVal (out int val) {val * = 2; // you want to use a value in val. // However, val is considered not to be assigned a value, 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 ;}
In c #, the scope of variables is more rigorous. For example, the Code similar to the following can be compiled and run successfully in c ++, but cannot be compiled in c, the uninitialized variables are 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 Overloading is similar to that in c ++. However, because c # introduces the ref keyword for "Reference, 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 #,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;}
Folder in microsoft visual c 60 Where functions are stored
Include files in
C: \ Program Files \ Microsoft Visual Studio \ VC98 \ Include
Using c language in microsoft visual c ++ does not seem to work in calling functions.
# Include <stdio. h>
Int main (void)
{
Float a, B;
Float c, f (float x, float y); // pay attention to this definition
A = 3; B = 4;
C = f (a, B );
Printf ("% f \ n", c );
}
Float f (float x, float y)
{
Float h;
H = x + y;
Return h;
}