Game Development Reference
In-Depth Information
int _tmain(int argc, _TCHAR* argv[])
{
PrintHello();
return 0;
}
Listing 5-1 contains a function named PrintHello . Functions in C++ always follow the same
format and PrintHello is as simple as a function can be. It does not return any values to the
code that calls the function and we also cannot pass any parameters to the function. To tell the
C++ compiler that we do not want the function to return a value, we specify void as its return
type . We can call a function by placing its name in our code followed by parentheses, as we have
done with PrintHello .
Passing Parameters to Functions
Functions tend to be more useful when we can pass values to them. We do this by specifying
parameters in the function signature . Listing 5-2 shows a function that contains parameters in its
signature.
Listing 5-2. Passing Values to Functions Through Parameters
#include <iostream>
using namespace std;
void PrintSum(int valueA, int valueB)
{
cout << valueA + valueB << endl;
}
int _tmain(int argc, _TCHAR* argv[])
{
PrintSum(3, 6);
return 0;
}
The function PrintSum takes two parameters. Both parameters are of the type int and are
named valueA and valueB . When we call PrintSum in _ tmain we pass it the values 3 and 6.
Inside the function the variable valueA contains the value 3 and valueB contains 6. The output
from the program is 9. Another way to achieve this result would be to return the sum from the
function.
 
Search WWH ::




Custom Search