Home

Conditional Statements and Functions

 

Returning an Enumeration

After creating an enumeration, its becomes a type of data. As such, it can be returned from a function. To start, you can precede the name of a function with the name of an enumeration to indicate that it is the type it would return. As compared to native types, you cannot just request the value of an enumeration from the user. This means that, most of the time, you should perform some processing in the body of the function and then return a value that is of the type of the enumeration.

When a function returns an enumeration, you can use its return value the same way you would use a member of the enumeration.

Conditional Return

Some functions are meant to return a value that is conditional of their processing. The fact that a function indicates the type of value it would return may not be clear at the time the function is closed but a function defined other than void must always return a value. You can write a conditional statement, such as if, inside of a function and return a value from that condition. Here is an example:

using namespace System;

enum HouseType { Unknown, SingleFamily, TownHouse, Condominium };
HouseType GetHouseType();

int main()
{
	HouseType TypeOfHouse;

	TypeOfHouse = GetHouseType();
	
	switch(TypeOfHouse)
	{
	case SingleFamily:
		Console::WriteLine(L"\nType of Home: Single Family");
		break;
	case TownHouse:
		Console::WriteLine(L"\nType of Home: Town House");
		break;
	case Condominium:
		Console::WriteLine(L"\nType of Home: Condominium");
		break;
	case Unknown:
		Console::WriteLine(L"\nType of Home:: Unknown");
		break;
	}

	Console::WriteLine();

	return 0;
}

HouseType GetHouseType()
{
	int type;

	Console::WriteLine(L"What Type of House Would you Like to Purchase?");
	Console::WriteLine(L"1 - Single Family");
	Console::WriteLine(L"2 - Town House");
	Console::WriteLine(L"3 - Condominium");
	Console::Write(L"Your Choice? ");
	type = int::Parse(Console::ReadLine());

	if( type == 1 )
		return SingleFamily;
	else if( type == 2 )
		return TownHouse;
	else if( type == 3 )
		return Condominium;
}

This GetHouseType() function indicates when one of three values is returned. In reality, this function could get a value other than the three that are considered. If the user enters such a value, the current version of the function would not know what to do. For this reason, the compiler would display a warning:

warning C4715: 'GetHouseType' : not all control paths return a value

To solve this problem, you must provide a statement that would include any value other than those considered. You can do this by writing a final return that has its own value. Here is an example:

using namespace System;

enum HouseType { Unknown, SingleFamily, TownHouse, Condominium };
HouseType GetHouseType();

int main()
{
	HouseType TypeOfHouse;

	TypeOfHouse = GetHouseType();
	
	switch(TypeOfHouse)
	{
	case SingleFamily:
		Console::WriteLine(L"\nType of Home: Single Family");
		break;
	case TownHouse:
		Console::WriteLine(L"\nType of Home: Town House");
		break;
	case Condominium:
		Console::WriteLine(L"\nType of Home: Condominium");
		break;
	case Unknown:
		Console::WriteLine(L"\nType of Home:: Unknown");
		break;
	}

	Console::WriteLine();

	return 0;
}

HouseType GetHouseType()
{
	int type;

	Console::WriteLine(L"What Type of House Would you Like to Purchase?");
	Console::WriteLine(L"1 - Single Family");
	Console::WriteLine(L"2 - Town House");
	Console::WriteLine(L"3 - Condominium");
	Console::Write(L"Your Choice? ");
	type = int::Parse(Console::ReadLine());

	if( type == 1 )
		return SingleFamily;
	else if( type == 2 )
		return TownHouse;
	else if( type == 3 )
		return Condominium;

	return Unknown;
}

 

 

Previous Copyright © 2006 FunctionX, Inc.