confused with fuctions explaination please?

currently going through C++ for dummies and found problems with some things so i wouldnt be surprised if some of the book is typod. reading cautiously though it

anyways, im in chapter 6 creating functions
he doesnt explain very well
void displayExplanation (void)and
displayExplanation ()and
sumSequence ()
could someone explain these cause i dont understand them. he keeps refering to main() and there is no main () in his testfuntion code. It doesn't make sense
this is his code for explaining these
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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
// FunctionDemo - demonstrate the use of functions

//                by breaking the inner loop of the

//                NestedDemo program off into its own

//                function

#include <cstdio>

#include <cstdlib>

#include <iostream>

using namespace std;



// displayExplanation - prompt the user as to the rules

//                      of the game

void displayExplanation(void)

{

    cout << "This program sums multiple series\n"

         << "of numbers. Terminate each sequence\n"

         << "by entering a negative number.\n"

         << "Terminate the series by entering an\n"

         << "empty sequence.\n"

         << endl;

    return;

}



// sumSequence - add a sequence of numbers entered from

//               the keyboard until the user enters a

//               negative number.

//               return - the summation of numbers entered

int sumSequence(void)

{

    // loop forever

    int accumulator = 0;

    for(;;)

    {

        // fetch another number

        int nValue = 0;

        cout << "Enter next number: ";

        cin  >> nValue;



        // if it's negative...

        if (nValue < 0)

        {

            // ...then exit from the loop

            break;

        }



        // ...otherwise add the number to the

        // accumulator

        accumulator += nValue;

    }



    // return the accumulated value

    return accumulator;

}



int main(int nNumberofArgs, char* pszArgs[])

{

    // display prompt to the user

    displayExplanation();



    // accumulate sequences of numbers...

    for(;;)

    {

        // sum a sequence of numbers entered from

        // the keyboard

        cout << "Enter next sequence" << endl;

        int accumulatedValue = sumSequence();



        // terminate the loop if sumSequence() returns

        // a zero

        if (accumulatedValue == 0)

        {

            break;

        }



        // now output the accumulated result

        cout << "The total is "

             << accumulatedValue

             << "\n"

             << endl;

    }





    cout << "Thank you" << endl;

    // wait until user is ready before terminating program

    // to allow the user to see the program results

    system("PAUSE");

    return 0;

}

plus sorry in advance for the long code...copy and pasted his code and he put empty code line in between everything lol
Last edited on
closed account (N85iE3v7)
Main is where you program starts and runs. Take a look at:
1
2
3
4
int main(int nNumberofArgs, char* pszArgs[])
{
    // main function
}


He is calling those two functions inside the main function.
int main() is on line 107.

This is how it works:

The main() function is always called first. And the first thing main() encounters in this program is displayExplanation() on line 113. It passes no variables to displayExplanation() on account of theres is nothing between the parentheses. So if you look at the function decleration on line 23, it is declared void displayExplanation(void). The void before displayExplanation(void) means it returns nothing and the void in the parentheses means it takes no variables. That void is not necesarry.

You'll learn about functions taking variables soon but don't worry about it for now. Learn how the control is passed through the program first.

Now displayExplanation just outputs some text and then goes back to the main function. The return on line 39 is not necesarry. When this function reaches the end, the only place for it to will be back to main().

The for(;;) on line 61 of main() just keeps an infinite loop going until you break out of it. So on line 129, int accumulatedValue is declared and set equal to sumSequence(), which is a function that also takes no variables. But if you look at the function decleration on line 53, it returns an int.

Now sumSequence() starts another infinite loop until a negative number is read in, line 77, and it breaks out of the loop. It will return whatever the accumulated value is, line 101. This value is stored by main() into accumulatedValue, which is then displayed, lines 149-155.

So that's kind of how functions work. Main() is always called first and when it encounters a function, denoted by a name and (), it performs that function and eventually returns to main or exits. I also read this book and I found that as I got to the more advanced stuff it became hard to follow.
Hope that helps, good luck!

ok now i understand where control is jumping to and from, but is he trying to teach at this point to learn where control is jumping to and from or he is trying to teach something that i am not understanding?

another question...isn't there an easier was to put this program in sequential order to not have control jump around so much(mostly to easily understand) if his goal was to teach to follow where control was going then it all makes sense.

my other question is...isn't the idea to not have control jump around so much? doesn't that make a program run slower than it could?
Last edited on
thanks a lot, nice explanation
lol oh yeah another question, shouldn't this run, i copied and pasted it from his cd, but it gives and error code : undefined reference to 'main'
Last edited on
It ran for me. Make sure you copied it correctly.
yup copied wrong ,but copied from this forum and worked...don't know what happened cuz i copied from my IDE to this forum lmao
Last edited on
Topic archived. No new replies allowed.