Function with Arguments

Hi gang, I've just started out with Beginning C++ for dummies and the Function with Argument has me stumped. I just cannot work out what the hell is going on. I appreciate that it's looping the factorial so I can ask as many as I like until I input a negative number, but the function part has me entirely confused.

nTarget seems to have no value, and is not a variable. I cannot see a reason why it is in the factorial() bit, and in the for(), as nValue will always be greater than or equal to 0, unless you want to exit the loop.

Am I missing something in how this works? The book's explanation is:
"The declaration of factorial() include an argument of nTarget of int. Looking ahead, you can see that this is intended to be the value to calculate the factorial of. The return value of the function is the calculated factorial"

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
//
//  FactorialFunction - rewrite the factorial code as
//                a separate function.
//
#include <cstdio>
#include <cstdlib>
#include <iostream>
using namespace std;

//
// factorial - return the factorial of the argument
//             provided. Returns a 1 for invalid arguments
//             such as negative numbers.
int factorial(int nTarget)
{
    // start with an accumulator that's initialized to 1
    int nAccumulator = 1;
    for (int nValue = 1; nValue <= nTarget; nValue++)
    {
        nAccumulator *= nValue;
    }

    return nAccumulator;
}

int main(int nNumberofArgs, char* pszArgs[])
{
    cout << "This program calculates factorials"
         << " of user input.\n"
         << "Enter a negative number to exit" << endl;

    // stay in a loop getting input from the user
    // until he enters a negative number
    for (;;)
    {
        // enter the number to calculate the factorial of
        int nValue;

        cout << "Enter number: ";
        cin  >> nValue;

        // exit if the number is negative
        if (nValue < 0)
        {
            break;
        }

        // display the result
        int nFactorial = factorial(nValue);
        cout << nValue << " factorial is "
             << nFactorial << endl;
    }

    // wait until user is ready before terminating program
    // to allow the user to see the program results
    system("PAUSE");
    return 0;
}
nValue in main() is not the same as nValue in factorial().
In this:
int nFactorial = factorial(nValue);
nTarget is assigned the value of the nValue set to it from main().

The nValue in factorial() is simply a counter that is normally expressed as i. Had it been written
for (int i = 1; i <= nTarget; i++)
it would probably been less confusing.
So the nValue in main(), will be assigned to nTarget in factorial().

Inputting 3 in nValue in main() will assign 3 to nTarget in factorial(). Int nValue then starts at 1, and then will increment up to 3?

I've altered the code in my program and it works so that explains that. What still stumps me is how nValue in main() is assigned to nTarget in factorial().

Yeeeeaaaah, I get it now. I was looking too far in depth and hadn't noticed the
int nFactorial = factorial(nValue);
in main, which assigns the nValue in main to whatever is factorial(HERE).

I had to take a step back and look at what it actually happening. I get it now. Thank you very much
Topic archived. No new replies allowed.