Game Development Reference
In-Depth Information
int _tmain(int argc, _TCHAR* argv[])
{
int sum = 0;
ReturnSum(3, 6, sum);
cout << sum << endl;
return 0;
}
We now use the reference operator on the type for outValue ; this looks like int& . We're beginning
to see the same operators being used for different purposes in different contexts. When defining
a variable, the & operator after the typename tells the compiler to make the variable a reference.
When placed before a variable name, when assigning to a variable, or when passing to a function it
becomes the address-of operator.
Sooner or later in our programs we will want to pass large amounts of data into a function. One
option could be to add many parameters to the function signature, but this makes the code difficult
to maintain, as every time we change the function signature we will also have to change every call.
Luckily structures provide an alternative.
Structures
So far all of the variables we have been using have been single variables. Individual variables are
useful, but sometimes they can only take us so far. We will alter the sum example once again to
show how we can use a structure with a function. Listing 5-6 shows an example of passing a
structure to a function.
Listing 5-6. Passing a Structure to a Function
#include <iostream>
using namespace std;
struct SumParameters
{
int valueA;
int valueB;
int result;
};
void ReturnSum(SumParameters& params)
{
params.result = params.valueA + params.valueB;
}
int _tmain(int argc, _TCHAR* argv[])
{
SumParameters sum;
sum.valueA = 3;
sum.valueB = 6;
 
Search WWH ::




Custom Search