Main Menu


Sponsored Links

  


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

Adding New Functions

So far we have only been using the functions that are built into C++, but it is also possible to add new functions. Actually, we have already seen some function definitions: Scene1, Scene2 etc. These functions are special because they indicate where the code for every cartoon scene is located, but the syntax for them is the same as for any other function definition:

void NAME ( LIST OF PARAMETERS )

{

STATEMENTS

}

You can make up any name you want for your function, except that you can’t call it the same name as any other C++ keyword. The list of parameters specifies what information, if any, you have to provide in order to use (or call) the new function.

Scene1 doesn’t take any parameters, as indicated by the empty parentheses () in its definition. The first couple of functions we are going to write also have no parameters, so the syntax looks like thi s:

void ShowTime ()

{

ShowText ( "The System Time is" );


int iTime = GetUTCTime ();

string sHour = IntToString ( GetUTCHour ( iTime ) );

string sMinute = IntToString ( GetUTCMinute ( iTime ) );

ShowText ( sHour + ":" + sMinute );

}

This function is named ShowTime; it contains se veral statements. It displays a text "The System Time is" for one second and then system time for one more second. In our scene we can call this new function using syntax that is similar to the way we call the built-in C++ commands:

void Scene1 ()

{

ShowTime ();

}

The program will output a system time to your cartoon. But what if we want to do this several times? We can call ShowTime several times from Scene1 then:

void Scene1 ()

{

ShowTime ();

ShowTime ();

ShowTime ();

}

Or we can write another function which invoke s ShowTime several times and call it from Scene1:

void ShowTimeThreeTimes ()

{

ShowTime ();

ShowTime ();

ShowTime ();

}


void Scene1 ()

{

ShowTimeThreeTimes ();

}

You should notice a few things about this program:

  1. You can call the same procedure repeatedly. In fact, it is quite common and useful to do so.

  2. You can have one function call another function. In this case, Scene1 calls ShowTimeThreeTimes and ShowTimeThreeTimes calls ShowTime. Again, this is common and useful

So far, it may not be clear why it is worth the trouble to create all these new functions. Actually, there are a lot of reasons, but this example only demonstrates two:

  1. Creating a new function gives you an opportunity to give a name to a group of statements. Functions can simplify a program by hiding a complex computation behind a single command, and by using English words in place of arcane code.

  2. Creating a new function can make a program smaller by eliminating repetitive code. For example, a short way to print a time nine consecutive times is to call ShowTimeThreeTimes three times. How would you print it 27 times?


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