[cs13001] FW: Lab6 question

Mikhail Nesterenko mikhail at cs.kent.edu
Fri Oct 5 15:26:12 EDT 2012


> 
> I have a quick question, am I aloud to use bool as a retun value.
> 

Yes, boolean values can certainly be returned. Make sure to specify
"bool" as a return value for the function.

For example, here is a prototype of a function that returns boolean

    bool decision(int);

Here is the definition for this function;

    bool decision (int a){
    	 if (a < 5)
	    return true;
	 else
	    return false;
    } 

The interesting point about functions returning boolean is that they
can be used in expressions of branching or looping statements which
often leads to terse, expressive code. 

For example, the above function can be invoked as follows


    if (decision(33))  // if decision() returns true then "33" is output
       cout << "33";

Or as follows:

   for (int i=0; decision(i); i++) // this loop is executed while the
       	    	 	      	   // decision() returns true
       // do something there

Thanks,
-- 
Mikhail


More information about the cs13001 mailing list