Returning a Primitive Type |
|
The main purpose of using a function in a program is to
perform an assignment. In some cases, you may want that function to produce a
result. When declaring or defining a function, you must clearly indicate this to
the compiler. Therefore, the function is said to return a value. To indicate
that a function must return a value, precede its name with the type of value that
it would return. This means that, depending on your intentions, one function can
be made to produce a number, another function can be defined to return a
character, yet another function could produce a Boolean value. Here is an
example of a function that will return a character:
__wchar_t IdentifyHouse()
{
}
In the body of the function, you can perform any type of
assignment that is necessary. The most important rule is that, before the
function is closed, that it, before the closing curly bracket, the function must
have returned a value of the appropriate type. To return a value from a
function, type the return keyword, followed by the value being returned,
followed by a semi-colon. Here is an example:
__wchar_t IdentifyHouse()
{
return L'S';
}
You can also declare a local variable, process it, and
return it from a function. Here is an example:
using namespace System;
__wchar_t GetHouseType();
int main()
{
Console::WriteLine(L"\nType of Home: {0}", GetHouseType());
Console::WriteLine();
return 0;
}
__wchar_t GetHouseType()
{
__wchar_t type;
Console::WriteLine(L"What Type of House Would you Like to Purchase?");
Console::WriteLine(L"S - Single Family");
Console::WriteLine(L"T - Town House");
Console::WriteLine(L"C - Condominium");
Console::Write(L"Your Choice? ");
type = __wchar_t::Parse(Console::ReadLine());
return type;
}
Practical
Learning: Returning a Primitive Type |
|
- To create functions that return some values, change the file as follows:
#include <iostream>
using namespace std;
using namespace System;
long GetPropertyNumber()
{
long propertyNumber;
Console::Write("Property #: ");
propertyNumber = long::Parse(Console::ReadLine());
return propertyNumber;
}
int GetPropertyCondition()
{
int condition;
Console::WriteLine("Property Condition");
Console::WriteLine("0. Unknown");
Console::WriteLine("1. Excellent");
Console::WriteLine("2. Good (may need minor repair");
Console::WriteLine("3. Acceptable (needs major repair)");
Console::WriteLine("4. Even (land is more important)");
Console::Write("Your Choice: ");
condition = int::Parse(Console::ReadLine());
return condition;
}
Byte GetNumberOfStories()
{
Byte stories;
Console::Write("Stories: ");
stories = int::Parse(Console::ReadLine());
return stories;
}
unsigned int GetBedrooms()
{
unsigned int bedrooms;
Console::Write("Bedrooms: ");
bedrooms = int::Parse(Console::ReadLine());
return bedrooms;
}
__wchar_t GetPropertyStype()
{
__wchar_t style;
Console::WriteLine("Style");
Console::WriteLine("U. Unknown");
Console::WriteLine("S. Split Level");
Console::WriteLine("C. Colonial");
Console::WriteLine("G. Georgial");
Console::Write("Your Choice: ");
style = __wchar_t::Parse(Console::ReadLine());
return style;
}
double GetPropertyValue()
{
double marketValue;
Console::Write("Market Value: ");
marketValue = double::Parse(Console::ReadLine());
return marketValue;
}
float GetBathrooms()
{
float bathrooms;
Console::Write("Bathrooms: ");
bathrooms = float::Parse(Console::ReadLine());
return bathrooms;
}
unsigned int GetYearBuilt()
{
unsigned int yearBuilt;
Console::Write("Year Built: ");
yearBuilt = int::Parse(Console::ReadLine());
return yearBuilt;
}
void CreateAndShowProperty()
{
long propertyNumber;
int condition;
Byte stories;
unsigned int bedrooms;
float bathrooms;
unsigned int yearBuilt;
__wchar_t style;
double marketValue;
String ^ strTitle1 = L"=//= Altair Realty =//=";
String ^ strTitle2 = L"-=- Properties Inventory -=-";
Console::WriteLine(strTitle1);
Console::WriteLine(strTitle2);
Console::WriteLine("To create a listing, enter the following information");
propertyNumber = GetPropertyNumber();
condition = GetPropertyCondition();
stories = GetNumberOfStories();
bedrooms = GetBedrooms();
bathrooms = GetBathrooms();
yearBuilt = GetYearBuilt();
style = GetPropertyStype();
marketValue = GetPropertyValue();
system("cls");
Console::WriteLine(strTitle1);
Console::WriteLine(strTitle2);
Console::WriteLine("=//= Property Listing =//=");
Console::WriteLine("Property #: {0}", propertyNumber);
Console::WriteLine("Condition: {0}", condition);
Console::WriteLine("Style: {0}", style);
Console::WriteLine("Bedrooms: {0}", bedrooms);
Console::WriteLine("Bathrooms: {0}", bathrooms);
Console::WriteLine("Stories: {0}", stories);
Console::WriteLine("Year Built: {0}", yearBuilt);
Console::WriteLine("Market Value: {0:C}", marketValue);
}
int main()
{
CreateAndShowProperty();
Console::WriteLine();
return 0;
}
|
- Execute the application to test it
- Close the DOS window
Returning a Native Reference
|
|
As a function can be created
to return a value of a primitive type, a function can also be defined to return
a reference to a primitive type. When declaring such a function, you must
indicate that it is returning a reference by preceding it with the &
operator. Here is an example:
double & GetPropertyValue()
{
}
In the body of the function,
define the desired behavior as you see fit. The most important
rule to apply is that the function must return not only a reference but a
reference to the appropriate type. When returning the value, don't precede the
name of the variable with &. Here is an example:
double & GetPropertyValue()
{
double Value = 406500.00;
double &rValue = Value;
return rValue;
}
When calling a function that
returns a reference, you can proceed as if it returns a regular value but of the
appropriate type. Here is an example:
using namespace System;
double & GetPropertyValue()
{
double Value = 406500.00;
double &rValue = Value;
return rValue;
}
int main()
{
double PropertyValue = GetPropertyValue();
Console::WriteLine(L"Property Value: ${0}\n", PropertyValue);
return 0;
}
This would produce:
Property Value: $406500
Press any key to continue . . .
Returning a Tracking Reference
|
|
Just as you can return a reference to a primitive type, you
can also return a tracking reference either to a primitive type or to a class.
To declare a function that returns a tracking reference, type the % operator
between its return type and its name. In the body of the function, you can
create the behavior you want. Before the close curly bracket of the function,
you must have returned a tracking reference of the indicated type.
When calling a function that returns a tracking reference,
proceed as you would for a native reference. Here is an example:
using namespace System;
Byte % GetStories()
{
Byte lvl;
Console::Write(L"How many levels does the house have? ");
lvl = Byte::Parse(Console::ReadLine());
Byte % Levels = lvl;
return Levels;
}
int main()
{
Byte Stories = GetStories();
Console::WriteLine(L"Number of Stories: {0}\n", Stories);
return 0;
}
Here is an example of running the program:
How many levels does the house have? 4
Number of Stories: 4
Press any key to continue . . .
Instead of a regular value or
even a reference, a function can return a pointer. You can start to specify this
by typing the * operator on the left side of the function's name. Here is an
example:
double * GetBathrooms()
{
}
Use the body of the
function to define it. Before the closing curly bracket of the function,
remember to return a pointer to the return value. Here is an example:
double * GetBathrooms()
{
double rooms = 0;
Console::Write(L"How many bathrooms in the house? ");
rooms = double::Parse(Console::ReadLine());
double *Bathrooms = &rooms;
return Bathrooms;
}
After defining the function,
you can call it. Remember that the asterisk is used to get the value of a
pointer. This means that, when calling the function, if you want to access its
value, make sure you include the asterisk on the left side of the name of the
function. Here is an example:
using namespace System;
double * GetBathrooms()
{
double rooms = 0;
Console::Write(L"How many bathrooms in the house? ");
rooms = double::Parse(Console::ReadLine());
double *Bathrooms = &rooms;
return Bathrooms;
}
int main()
{
double bRooms = *GetBathrooms();
Console::WriteLine(L"Number of Bathrooms: {0:F}\n", bRooms);
return 0;
}
This would produce:
How many bathrooms in the house? 2.5
Number of Bathrooms: 2.50
Press any key to continue . . .
If you want to process a function in the managed heap, you
can make it return a handle. When declaring or defining the function, precede it
with a handle type. When you finish using the function, make sure it returns a
value that is a handle. The rules are the same for pointers.
Practical
Learning: Returning a Handle |
|
- To show different ways of returning a handle, change the file as follows:
#include <iostream>
using namespace std;
using namespace System;
long ^ GetPropertyNumber()
{
long ^ propertyNumber = gcnew long;
Console::Write("Property #: ");
propertyNumber = long::Parse(Console::ReadLine());
return propertyNumber;
}
int ^ GetPropertyCondition()
{
Console::WriteLine("Property Condition");
Console::WriteLine("0. Unknown");
Console::WriteLine("1. Excellent");
Console::WriteLine("2. Good (may need minor repair");
Console::WriteLine("3. Acceptable (needs major repair)");
Console::WriteLine("4. Even (land is more important)");
Console::Write("Your Choice: ");
int ^ condition = int::Parse(Console::ReadLine());
return condition;
}
Byte ^ GetNumberOfStories()
{
Byte ^ stories = gcnew Byte;
Console::Write("Stories: ");
*stories = int::Parse(Console::ReadLine());
return stories;
}
unsigned int ^ GetBedrooms()
{
unsigned int ^ bedrooms = gcnew unsigned int;
Console::Write("Bedrooms: ");
bedrooms = UInt32::Parse(Console::ReadLine());
return bedrooms;
}
__wchar_t GetPropertyStype()
{
__wchar_t style;
Console::WriteLine("Style");
Console::WriteLine("U. Unknown");
Console::WriteLine("S. Split Level");
Console::WriteLine("C. Colonial");
Console::WriteLine("G. Georgial");
Console::Write("Your Choice: ");
style = __wchar_t::Parse(Console::ReadLine());
return style;
}
double GetPropertyValue()
{
double marketValue;
Console::Write("Market Value: ");
marketValue = double::Parse(Console::ReadLine());
return marketValue;
}
float GetBathrooms()
{
float bathrooms;
Console::Write("Bathrooms: ");
bathrooms = float::Parse(Console::ReadLine());
return bathrooms;
}
unsigned int GetYearBuilt()
{
unsigned int yearBuilt;
Console::Write("Year Built: ");
yearBuilt = int::Parse(Console::ReadLine());
return yearBuilt;
}
void CreateAndShowProperty()
{
long^ propertyNumber = gcnew long;
int ^ condition = gcnew int;
Byte ^stories = gcnew Byte;
unsigned int ^ bedrooms = gcnew unsigned int;
float bathrooms;
unsigned int yearBuilt;
__wchar_t style;
double marketValue;
String ^ strTitle1 = L"=//= Altair Realty =//=";
String ^ strTitle2 = L"-=- Properties Inventory -=-";
Console::WriteLine(strTitle1);
Console::WriteLine(strTitle2);
Console::WriteLine("To create a listing, enter the following information");
propertyNumber = GetPropertyNumber();
condition = GetPropertyCondition();
stories = GetNumberOfStories();
bedrooms = GetBedrooms();
bathrooms = GetBathrooms();
yearBuilt = GetYearBuilt();
style = GetPropertyStype();
marketValue = GetPropertyValue();
system("cls");
Console::WriteLine(strTitle1);
Console::WriteLine(strTitle2);
Console::WriteLine("=//= Property Listing =//=");
Console::WriteLine("Property #: {0}", propertyNumber);
Console::WriteLine("Condition: {0}", condition);
Console::WriteLine("Style: {0}", style);
Console::WriteLine("Bedrooms: {0}", bedrooms);
Console::WriteLine("Bathrooms: {0}", bathrooms);
Console::WriteLine("Stories: {0}", stories);
Console::WriteLine("Year Built: {0}", yearBuilt);
Console::WriteLine("Market Value: {0:C}", marketValue);
}
int main()
{
CreateAndShowProperty();
Console::WriteLine();
return 0;
}
|
- Execute the application to test it
- Close the DOS window