Home

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;
}

Practical LearningPractical Learning: Returning Arrays

  1. To return arrays from functions, change the program as follows:
     
    using namespace System;
    
    array<int ^> ^ GetBedrooms();
    array<Byte> ^ GetStories();
    
    int main()
    {
        array<__wchar_t> ^ HouseTypes = gcnew array<__wchar_t>(4)
    	{ L'S', L'C', L'S', L'T' };
        array<int ^> ^ Bedrooms = GetBedrooms();
        array<Byte> ^ Stories = GetStories();
        array<double> ^ Bathrooms = gcnew array<double>(4)
    	{ 3.5, 1, 2.5, 1.5 };
        array<bool> ^ HasGarage = gcnew array<bool>(4)
    	{ true, true, false, false };
       
        array<double ^> ^ Values = gcnew array<double ^>(4)
        {
    	gcnew double(550500.00),
    	gcnew double(115000.00),
    	gcnew double(425000.00),
    	gcnew double(350000.00)
        };
    
        Console::WriteLine(L"House Catalog");
        Console::WriteLine(L"===================================================");
        Console::WriteLine(L"Type Bedrooms Stories Bathrooms Garage    Value");
        Console::WriteLine(L"=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=");
        for(int house = 0; house < 4; house++)
        {
            Console::WriteLine(L"  {0}\t{1}\t{2}\t{3}\t{4}\t{5:F}",
    					   HouseTypes[house], Bedrooms[house],
    					   Stories[house], Bathrooms[house],
    					   HasGarage[house], Values[house]);
            Console::WriteLine(
    		L"---------------------------------------------------");
        }
    
        Console::WriteLine();
        return 0;
    }
    
    array<int ^> ^ GetBedrooms()
    {
        array<int ^> ^ beds = gcnew array<int ^>(4);
    
        beds[0] = 5;
        beds[1] = 2;
        beds[2] = 3;
        beds[3] = 1;
    
        return beds;
    }
    
    array<Byte> ^ GetStories()
    {
        array<Byte > ^ Stories = gcnew array<Byte>(4);
    
        Stories[0] = 4;
        Stories[1] = 2;
        Stories[2] = 3;
        Stories[3] = 1;
    
        return Stories;
    }
  2. Execute the application to test it
  3. Close the DOS window and return to your programming environment

Passing an Array as Argument

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;
}
 

Practical LearningPractical Learning: Passing an Array as Argument

  1. To pass an array as argument to a function, change the program as follows:
     
    using namespace System;
    
    array<int ^> ^ GetBedrooms();
    array<Byte> ^ GetStories();
    void GetBathrooms(array<float ^> ^);
    
    int main()
    {
        array<__wchar_t> ^ HouseTypes = gcnew array<__wchar_t>(4)
    	{ L'S', L'C', L'S', L'T' };
        array<int ^> ^ Bedrooms = GetBedrooms();
        array<Byte> ^ Stories = GetStories();
        array<float ^> ^ Bathrooms = gcnew array<float ^>(4);
        GetBathrooms(Bathrooms);
        array<bool> ^ HasGarage = gcnew array<bool>(4)
    	{ true, true, false, false };
       
    	array<double ^> ^ Values = gcnew array<double ^>(4)
        {
    	gcnew double(550500.00),
    	gcnew double(115000.00),
    	gcnew double(425000.00),
    	gcnew double(350000.00)
        };
    
        Console::WriteLine(L"House Catalog");
        Console::WriteLine(L"===================================================");
        Console::WriteLine(L"Type Bedrooms Stories Bathrooms Garage    Value");
        Console::WriteLine(L"=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=");
        for(int house = 0; house < 4; house++)
        {
            Console::WriteLine(L"  {0}\t{1}\t{2}\t{3}\t{4}\t{5:F}",
    					   HouseTypes[house], Bedrooms[house],
    					   Stories[house], Bathrooms[house],
    					   HasGarage[house], Values[house]);
            Console::WriteLine(
    		L"---------------------------------------------------");
        }
    
        Console::WriteLine();
        return 0;
    }
    
    array<int ^> ^ GetBedrooms()
    {
        array<int ^> ^ beds = gcnew array<int ^>(4);
    
        beds[0] = 5;
        beds[1] = 2;
        beds[2] = 3;
        beds[3] = 1;
    
        return beds;
    }
    
    array<Byte> ^ GetStories()
    {
        array<Byte > ^ Stories = gcnew array<Byte>(4);
    
        Stories[0] = 4;
        Stories[1] = 2;
        Stories[2] = 3;
        Stories[3] = 1;
    
        return Stories;
    }
    
    void GetBathrooms(array<float ^> ^ baths)
    {
        baths[0] = 2.5F;
        baths[1] = 1.0F;
        baths[2] = 1.5F;
        baths[3] = 3.0F;
    }
  2. Execute the application to test it
  3. Close the DOS window and return to your programming environment
 

Previous Copyright © 2007-2013, FunctionX Home