Visual C++ .NET/Function/Definition
Содержание
Call a Function
#include "stdafx.h"
#using <mscorlib.dll>
using namespace System;
void myFunction(int val) {
if (val < 0) return;
if ((val >= 0) && (val < 11))
System::Console::WriteLine("val is between 0 and 10.");
else
System::Console::WriteLine("val is greater than 10.");
}
int main(void) {
int value = 25;
myFunction(value);
return 0;
}
Create a Function Prototype
#include "stdafx.h"
#using <mscorlib.dll>
using namespace System;
void TestRange(int val)
{
if (val < 0) return;
if ((val >= 0) && (val < 11))
System::Console::WriteLine("val is between 0 and 10.");
else
System::Console::WriteLine("val is greater than 10.");
}
int main(void)
{
int value = 25;
TestRange(value);
return 0;
}
global function
// compile with: cl /clr:safe /LD global_function.cpp
#include "stdafx.h"
using namespace System;
void GlobalFunction()
{
Console::WriteLine("Global C++/CLI Function.");
}
public ref class R
{
public:
static void FMember()
{
Console::WriteLine("C++/CLI Static Member Function.");
GlobalFunction();
}
};
Pass value to a Function
#include "stdafx.h"
#using <mscorlib.dll>
using namespace System;
void myFunction(int val1, int val2, String^ val3) {
System::Console::Write("val1 = ");
System::Console::WriteLine(val1);
System::Console::Write("val2 = ");
System::Console::WriteLine(val2);
System::Console::Write("val3 = ");
System::Console::WriteLine(val3);
}
int main(void) {
int value = 25;
System::Console::WriteLine("Hello World");
myFunction(1, 2, "String");
return 0;
}