Main Menu


Sponsored Links

  


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

Parameters and Arguments

Some of the built-in functions we have used have parameters, which are values that you provide to let the function do its job. For example, if you want to find the sine of a number, you have to indicate what the number is. Thus, Sin takes a double value as a parameter.

Some functions take more than one parameter, like Pow , which takes two doubles, the base and the exponent

Notice that in each of these cases we have to specify not only how many parameters there are, but also what type they are. So it shouldn’t surprise you that when you write a function definition, the parameter list indicates the type of each parameter. For example

void ShowTextTwice ( string sMessage )

{

ShowText ( sMessage );

ShowText ( "" );

ShowText ( sMessage );

ShowText ( "" );

}

This function takes a single parameter, named sMessage, which has type string. Whatever that parameter is (and at this point we have no idea what it is), it gets displayed twice (it is displayed once for a second, then empty string is displayed for a second, then the text is displayed for one more second and empty string is displayed for one more second).

In order to call this function, we have to provide a string. For example, we might have a Scene function like this:

void Scene1 ()

{

ShowTextTwice ( "WARNING" );

}

The string value you provide is called an argument, and we say that the argument is passed to the function. In this case the value "WARNING" is passed as an argument to ShowTextTwice where it will get displayed twice.

Alternatively, if we had a string variable, we could use it as an argument instead:

void Scene1 ()

{

string sWarning = "WARNING" ;

ShowTextTwice ( sWarning );

}

Notice something very important here: the name of the variable we pass as an argument ( sWarning ) has nothing to do with the name of the parameter ( sMessage ).

Let me say that again: The name of the variable we pass as an argument has nothing to do with the name of the parameter! They can be the same or they can be different, but it is important to realize that they are not the same thing, except that they happen to have the same value (in this case the string "WARNING" ).

The value you provide as an argument must have the same type as the parameter of the function you call. This rule is important, but it is sometimes confusing because C++ sometimes converts arguments from one type to another automatically. For example you can use an integer parameter instead of a string one because it can be converted to string automatically in WCM C++. But for now you should learn the general rule, and we will deal with exceptions later.


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