woensdag 20 juni 2007

Two kinds of local variables

long double probability(unsigned numbers, unsigned picks)
{
long double result = 1.0; // here come some local variables
long double n;
unsigned p;

for (n = numbers, p = picks; p > 0; n--, p--)
result = result * n / p ;
return result;
}

The probability() function illustrates two kinds of local variables you can have in a function. First there are the formal parameters (numbers and picks), which are declared in the function header before the opening brace. Then come the other local variables (result, n and p). They are declared in between the braces bounding the function definition.
The main difference between the formal parameters and the other local variables is that the formal parameters get their values from the function that calls probability(), whereas the other variables get values from within the function.

Geen opmerkingen: