Main Menu


Sponsored Links

  


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

Return Values

Some of the built-in functions we have used, like the math functions, have produced results. That is, the effect of calling the function is to generate a new value, which we usually assign to a variable or use as part of an expression. For example:

double dE = Exp (1.0);

double dHeight = dRadius * Sin ( dAngle );

But so far all the functions we have written have been void functions; that is, functions that return no value. When you call a void function, it is typically on a line by itself, with no assignment:

ShowTime ();

PrintTime ( 11, 59 );

In this chapter, we are going to write functions that return things, which I will refer to as fruitful functions, for want of a better name. The first example is area, which takes a double as a parameter, and returns the area of a circle with the given radius:

double GetArea ( double dRadius )

{

double dPi = 3.1415926;

double dArea = dPi * dRadius * dRadius;

return dArea;

}

The first thing you should notice is that the beginning of the function definition is different. Instead of void , which indicates a void function, we see double, which indicates that the return value from this function will have type double.

Also, notice that the last line is an alternate form of the return statement that includes a return value. This statement means, “return immediately from this function and use the following expression as a return value.” The expression you provide can be arbitrarily complicated, so we could have written this function more concisely:

double GetArea ( double dRadius)

{

return 3.1415926 * dRadius * dRadius;

}

On the other hand, temporary variables like dArea often make debugging easier. For example if you are unsure if your function is functioning properly you can add a ShowText ( dArea ); statement right before the return statement to verify its value in cartoon before it gets too complicated to find mistakes later.

In either case, the type of the expression in the return statement must match the return type of the function. In other words, when you declare that the return type is double, you are making a promise that this function will eventually produce a double. If you try to return with no expression, or an expression with the wrong type, the compiler will take you to task.

Sometimes it is useful to have multiple return statements, one in each branch of a conditional:

double GetAbsoluteValue ( double dX )

{

if ( dX < 0 )

{

return -dX;

}

else

{

return dX;

}

}


Since these returns statements are in an alternative conditional, only one will be executed. Although it is legal to have more than one return statement in a function, you should keep in mind that as soon as one is executed, the function terminates without executing any subsequent statements.

Code that appears after a return statement, or any place else where it can never be executed, is called dead code . Since it can never be executed, good programming practice would be to delete it to avoid confusion.

If you put return statements inside a conditional, then you must guarantee that every possible path through the program hits a return statement. For example:

double GetAbsoluteValue ( double dX )

{

if ( dX < 0 )

{

return -dX;

}

else if ( dX > 0 )

{

return dX;

} // WRONG!!

}

This program is not correct because if dX happens to be 0, then neither condition will be true, and the function will end without hitting a return statement. As a result, the program may compile and run, but the return value when dX == 0 could be anything, and will probably be different in different environments.


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