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:
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.