Arrays and Functions |
|
Returning an Array |
Instead of returning a single value, a function can be made to return an array of values. When declaring the function, to indicate that it returns an array, precede its name with the array handle declaration. Here is an example: array<int> ^ GetStories(); When implementing the function, you can do what is necessary but before exiting the body of the function, make sure you return an array of handles of the appropriate type. For example, in the body of the function, you can declare an array of handles, initialize it, and then return it. Here is an example: |
array<int> ^ GetStories() { array<int> ^ Stories = gcnew array<int>(5); Stories[0] = 4; Stories[1] = 2; Stories[2] = 3; Stories[3] = 1; Stories[4] = 3; return Stories; } When calling the function, remember that, since it returns an array of handles, you can assign it to such a variable. Here is an example: using namespace System; array<int> ^ GetStories() { array<int > ^ Stories = gcnew array<int>(5); Stories[0] = 4; Stories[1] = 2; Stories[2] = 3; Stories[3] = 1; Stories[4] = 3; return Stories; } int main() { array<int > ^ Stories = GetStories(); Console::WriteLine(L"Levels: {0}", Stories[0]); Console::WriteLine(L"Levels: {0}", Stories[1]); Console::WriteLine(L"Levels: {0}", Stories[2]); Console::WriteLine(L"Levels: {0}", Stories[3]); Console::WriteLine(L"Levels: {0}", Stories[4]); Console::WriteLine(); return 0; } This would produce: Levels: 4 Levels: 2 Levels: 3 Levels: 1 Levels: 3 Press any key to continue . . . You can also make the function return an array of handles. To do this, between the <> operator, include the ^ operator. In this case also, remember to return an appropriate array that corresponds to the type on the left side of the function name. Here is an example: using namespace System; // This function must return an array of handles array<int ^> ^ GetStories() { array<int ^> ^ Stories = gcnew array<int ^>(5); Stories[0] = gcnew int(4); Stories[1] = gcnew int(2); Stories[2] = gcnew int(3); Stories[3] = gcnew int(1); Stories[4] = gcnew int(3); return Stories; } int main() { array<int ^> ^ Stories = GetStories(); Console::WriteLine(L"Levels: {0}", Stories[0]); Console::WriteLine(L"Levels: {0}", Stories[1]); Console::WriteLine(L"Levels: {0}", Stories[2]); Console::WriteLine(L"Levels: {0}", Stories[3]); Console::WriteLine(L"Levels: {0}", Stories[4]); Console::WriteLine(); return 0; } Because you can initialize an array using its own curly brackets, the array in the above function could also be initialized as follows: // This function must return an array of handles array<int ^> ^ GetStories() { array<int ^> ^ Stories = gcnew array<int ^>(5) { gcnew int(4), gcnew int(2), gcnew int(3), gcnew int(1), gcnew int(3) }; return Stories; }
A function can take an array as argument. When declaring such a function, in its parentheses, indicate your intention with an array variable. Here is an example: void ShowBathrooms(array<float> ^ rooms); In the body of the function, you can use the array appropriately, following the rules of an array. For example, you can access each element and display its value. When calling the function, make sure you pass a valid array to it. Here is an example: using namespace System; array<float> ^ GetBathrooms() { array<float> ^ baths = gcnew array<float>(6); baths[0] = 2.5F; baths[1] = 1.0F; baths[2] = 1.5F; baths[3] = 3.0F; baths[4] = 1.5F; return baths; } void ShowBathrooms(array<float> ^ rooms) { Console::WriteLine(L"Bathrooms: {0:F}", rooms[0]); Console::WriteLine(L"Bathrooms: {0:F}", rooms[1]); Console::WriteLine(L"Bathrooms: {0:F}", rooms[2]); Console::WriteLine(L"Bathrooms: {0:f}", rooms[3]); Console::WriteLine(L"Bathrooms: {0:F}\n", rooms[4]); } int main() { array<float> ^ Bathrooms = GetBathrooms(); ShowBathrooms(Bathrooms); return 0; } This would produce: Bathrooms: 2.50 Bathrooms: 1.00 Bathrooms: 1.50 Bathrooms: 3.00 Bathrooms: 1.50 Press any key to continue . . . Remember that, as mentioned for the other arrays, an array is passed by reference. This means that, if a function modifies an array, its elements keep the new values when the function exits. You can use this characteristic to return an array even if the function is declared as void. Here is an example: using namespace System; void GetBedrooms(array<unsigned int> ^ beds) { beds[0] = 5; beds[1] = 3; beds[2] = 6; beds[3] = 3; beds[4] = 1; beds[5] = 2; } void ShowBedrooms(array<unsigned int> ^ rooms) { Console::WriteLine(L"Property: {0} bedrooms", rooms[0]); Console::WriteLine(L"Property: {0} bedrooms", rooms[1]); Console::WriteLine(L"Property: {0} bedrooms", rooms[2]); Console::WriteLine(L"Property: {0} bedrooms", rooms[3]); Console::WriteLine(L"Property: {0} bedroom", rooms[4]); Console::WriteLine(L"Property: {0} bedrooms\n", rooms[5]); } int main() { array<unsigned int> ^ Bedrooms = gcnew array<unsigned int>(6); GetBedrooms(Bedrooms); ShowBedrooms(Bedrooms); return 0; } This would produce: Property: 5 bedrooms Property: 3 bedrooms Property: 6 bedrooms Property: 3 bedrooms Property: 1 bedroom Property: 2 bedrooms Press any key to continue . . . You can also pass an array with values that each is a handle. Here is an example: using namespace System; void GetBedrooms(array<int ^> ^ beds) { beds[0] = 5; beds[1] = 3; beds[2] = 6; beds[3] = 3; beds[4] = 1; beds[5] = 2; } void ShowBedrooms(array<int ^> ^ rooms) { Console::WriteLine(L"Property: {0} bedrooms", rooms[0]); Console::WriteLine(L"Property: {0} bedrooms", rooms[1]); Console::WriteLine(L"Property: {0} bedrooms", rooms[2]); Console::WriteLine(L"Property: {0} bedrooms", rooms[3]); Console::WriteLine(L"Property: {0} bedroom", rooms[4]); Console::WriteLine(L"Property: {0} bedrooms\n", rooms[5]); } int main() { array<int ^> ^ Bedrooms = gcnew array<int ^>(6); GetBedrooms(Bedrooms); ShowBedrooms(Bedrooms); return 0; }
|
|
||
Previous | Copyright © 2007-2013, FunctionX | Home |
|