Main Menu


Sponsored Links

  


  
  
Web Cartoon Maker: a Fun Way to Learn C++ Contents Previous Next

Bool Functions

Functions can return bool values just like any other type, which is often convenient for hiding complicated tests inside functions. For example:

bool IsSingleDigit ( int iX )

{

if ( iX >= 0 && iX < 10)

{

return true;

}

else

{

return false;

}

}

The name of this function is IsSingleDigit. It is common to give boolean functions names that sound like yes/no questions. The return type is bool, which means that every return statement has to provide a bool expression.

The code itself is straightforward, although it is a bit longer than it needs to be. Remember that the expression x >= 0 && x < 10 has type bool, so there is nothing wrong with returning it directly and avoiding the if statement altogether:

bool IsSingleDigit ( int iX )

{

return ( iX >= 0 && iX < 10);

}

Y ou can call this function in the usual ways:

void Scene1 ()

{

ShowText ( IsSingleDigit ( 2 ) );

bool bBigFlag = ! IsSingleDigit ( 17 );

}

The first line outputs the value true because 2 is a single-digit number. The second line assigns the value true to bBigFlag because 17 is not a single-digit number.

The most common use of bool functions is inside conditional statements:

if ( IsSingleDigit ( iX ) )

{

ShowText ( "iX is little" );

}

else

{

ShowText ( "iX is big" );

}


Contents Previous Next
  
News

New Tales Animator Video by Alan Sturgess

Alan Sturgess shared an excellent video he made using Tales Animator! You can still download Tales Animator here. Unfortunately it is only available for Wi

...

Simple Online Character Designer

There is a prototype of simple online character designer available HERE. It is only a prototype, it does not contain many pieces yet but it can already generat

...

Book is updated

Now our book "Web Cartoon Maker: A Fun Way to Learn C++" is fully in synch with WCM 1.5! It is available for download and online reading HERE.

...

Web Cartoon Maker 1.5 is here!

Web Cartoon Maker 1.5 is finally here! You can download it HERE! Here is what was updated in version 1.5: Web Cartoon Maker Desktop Edition is now fully standal

...

read more news...


Poll