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:
 ``12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273`` ``````// // FunctionDemo - demonstrate how to use a function // to simplify the logic of the program. // #include #include #include 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:
 ``12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061`` ``````// // FunctionDemo - demonstrate how to use a function // to simplify the logic of the program. // #include #include #include 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...
 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.