About functions

I'm a self learner and I just started learning about functions,
I have probably a stupid question, Yet I have to understand it!
So here it is,
This is a program that works:
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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
//
//  FunctionDemo - demonstrate how to use a function
//                 to simplify the logic of the program.
//
#include <cstdio>
#include <cstdlib>
#include <iostream>
using namespace std;
/////////Funcition()////////////////////////////////////////////////////////////////////////////////////
//
// sumSequence() - return the sum of a series of numbers
//                 entered by the user. Exit the loop
//                 when the user enters a negative
//                 number.
int sumSequence()
{
    // create a variable into which we will add the
    // numbers entered by the user
    int nAccumulator = 0;

    for(;;)
    {
        // red another value from the user
        int nValue;
        cout << "Next; ";
        cin  >> nValue;

        // exit if nValue is negative
        if (nValue < 0)
        {
            break;
        }
        // add the value entered to the accumulated value
        nAccumulator += nValue;
    }

    // return the accumulated value to the caller
    return nAccumulator;
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////
int main(int nNumberofArgs, char* pszArgs[])
{
    cout << "This program sums sequences of numbers.\n"
         << "Enter a series of numbers. Entering a\n"
         << "negative number causes the program to \n"
         << "print the sum and start over with a new\n"
         << "sequences. "
         << "Enter two negatives in a row to end the\n"
         << "program." << endl;

    // stay in a loop getting input from the user
    // until he enters a negative number
    for(;;)
    {
        // accumulate the sequences
        int nSum = sumSequence();

        // if the sum is zero...
        if (nSum == 0)
        {
            // ...then exit the program
            break;
        }

        // display the result
        cout << "Sum = " << nSum << endl;
    }

// wait untill the user is ready before terminating program
// to allow the user to see the program results
system("PAUSE");
return 0;
}


My question comes when i try to change the places of the int sumSequence() and the int main(int nNumberofArgs, char* pszArgs[]) Like this:
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
60
61
//
//  FunctionDemo - demonstrate how to use a function
//                 to simplify the logic of the program.
//
#include <cstdio>
#include <cstdlib>
#include <iostream>
using namespace std;
int main(int nNumberofArgs, char* pszArgs[])
{
    cout << "This program sums sequences of numbers.\n"
         << "Enter a series of numbers. Entering a\n"
         << "negative number causes the program to \n"
         << "print the sum and start over with a new\n"
         << "sequences. "
         << "Enter two negatives in a row to end the\n"
         << "program." << endl;

    // stay in a loop getting input from the user
    // until he enters a negative number
    for(;;)
    {
        // accumulate the sequences
        int nSum = sumSequence();

        // if the sum is zero...
        if (nSum == 0)
        {
            // ...then exit the program
            break;
        }

        // display the result
        cout << "Sum = " << nSum << endl;
    }
int sumSequence()
{
    // create a variable into which we will add the
    // numbers entered by the user
    int nAccumulator = 0;

    for(;;)
    {
        // red another value from the user
        int nValue;
        cout << "Next; ";
        cin  >> nValue;

        // exit if nValue is negative
        if (nValue < 0)
        {
            break;
        }
        // add the value entered to the accumulated value
        nAccumulator += nValue;
    }

    // return the accumulated value to the caller
    return nAccumulator;
}
///////////////////////////////////////////////////////////////////////////////////////////////////////////////// 


Why can't it work like that? I mean why cant the program start with the main, And move to the function...
Sorry for the headache.
I mean why cant the program start with the main, And move to the function...

Regardless of which way the code is written, execution always starts with function main(). You could place main() at the start of the program code, or at the end of the code, or even somewhere in the middle if you have multiple functions.

But "move to the function..." - well, I'm not quite sure about the term "move" here. Function main() calls function sumSequence(). When that function ends, the program continues with the remainder of function main(). So it's just passing control to the other function on a temporary basis, before the code in main() resumes control.


There are two things missing from your second version.

One, you lost the closing brace of function main().

(You also lost the pause and return statements, but that's not the reason why it doesn't work).

Secondly, you need to declare before main(), the function prototype, like this:
 
  int sumSequence();


So although the full definition of the function can go at the end, the compiler needs to see the prototype in order to know what type of parameters it takes, and what type of value it will return.
Last edited on
Topic archived. No new replies allowed.