I was woundering, should one declare the functions they are going to be using in their program before or after main()?
I was woundering, should one declare the functions they are going to be using in their program before or after main()?
Personal preference really, but if you code the function definitions after main then you will need to provide your function prototypes prior to using them. i.e. before main.
"Queen and huntress, chaste and fair,
Now the sun is laid to sleep,
Seated in thy silver chair,
State in wonted manner keep."
Here's an example to get you on your way. This is the way I was taught to do it. I like it; I use it; I suggest it.
I hope that helpedCode:#include <iostream> using namespace std; // Function prototypes void Hello(); double Square(double x); double Cube(double x); void GoodBye(); int main() { double x; Hello(); cout << "Enter a number: "; cin >> x; cout << x << " squared is " << Square(x) << endl; cout << x << " cubed is " << Cube(x) << endl; GoodBye(); return 0; } // Function implementations void Hello() { cout << "Hello. Welcome to my program." << endl; } double Square(double x) { return x * x; } double Cube(double x) { return x * x * x; } void GoodBye { cout << "Thank you for using this program.\nHave a nice day."; }
FAQ
"The computer programmer is a creator of universes for which he alone is responsible. Universes of virtually unlimited complexity can be created in the form of computer programs." -- Joseph Weizenbaum.
"If you cannot grok the overall structure of a program while taking a shower, you are not ready to code it." -- Richard Pattis.
Since I usually think up my code on the spot, it would mess up my thought process if I used prototypes =I
I am against the teaching of evolution in schools. I am also against widespread
literacy and the refrigeration of food.
It's easier without that extra step
I am against the teaching of evolution in schools. I am also against widespread
literacy and the refrigeration of food.
I dont like to use prototypes. I feel they're a waste of time. I usually just define all my functions before main(). It seems to help organize the code a lot better since you dont have declarations and definitions all over the place.
- Dean
But what if you want to call main() in the middle of your function, you would have to put the function prototype before the function that calls main(), right? Or is there another way?
What on earth—a function calling main?! Functions don't call main, so I'm not sure I understand your question.Originally posted by Munkey01
But what if you want to call main() in the middle of your function, you would have to put the function prototype before the function that calls main(), right? Or is there another way?
FAQ
"The computer programmer is a creator of universes for which he alone is responsible. Universes of virtually unlimited complexity can be created in the form of computer programs." -- Joseph Weizenbaum.
"If you cannot grok the overall structure of a program while taking a shower, you are not ready to code it." -- Richard Pattis.
Functions dont really call main(), man. If I understand what you're saying, I guess you could put prototypes before main(), and then define your functions afterward.Originally posted by Munkey01
But what if you want to call main() in the middle of your function, you would have to put the function prototype before the function that calls main(), right? Or is there another way?
- Dean
No, I think he really wants to call main() inside a function. He should refer himself to his sigOriginally posted by LordVirusXXP
Functions dont really call main(), man. If I understand what you're saying, I guess you could put prototypes before main(), and then define your functions afterward.
I am against the teaching of evolution in schools. I am also against widespread
literacy and the refrigeration of food.
I mean like terminating the function right there and returning to main().
Code:#include <iostream> void function() { std::cout << "Hello World!\n"; return; std::cout << "You can put whatever you want here, because it won't show up"; } int main() { function(); return 0; }
I am against the teaching of evolution in schools. I am also against widespread
literacy and the refrigeration of food.