Thread: declair fucntions before main()?

  1. #1

    Question declair fucntions before main()?

    I was woundering, should one declare the functions they are going to be using in their program before or after main()?

  2. #2
    Refugee face_master's Avatar
    Join Date
    Aug 2001
    Posts
    2,052
    prototyped before main, but write the functions after main.

  3. #3
    Registered User foniks munkee's Avatar
    Join Date
    Nov 2001
    Posts
    343
    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."

  4. #4
    Senior Member joshdick's Avatar
    Join Date
    Nov 2002
    Location
    Phildelphia, PA
    Posts
    1,146
    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.
    Code:
    #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.";
    }
    I hope that helped
    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.

  5. #5
    I knew what they ment. But thanks for the example anyways man.

  6. #6
    Registered User abrege's Avatar
    Join Date
    Nov 2002
    Posts
    369
    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.

  7. #7
    Why not just go back and add them?

  8. #8
    Registered User abrege's Avatar
    Join Date
    Nov 2002
    Posts
    369
    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.

  9. #9
    Registered User LordVirusXXP's Avatar
    Join Date
    Dec 2002
    Posts
    86
    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

  10. #10
    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?

  11. #11
    Senior Member joshdick's Avatar
    Join Date
    Nov 2002
    Location
    Phildelphia, PA
    Posts
    1,146
    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?
    What on earth—a function calling main?! Functions don't call main, so I'm not sure I understand your question.
    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.

  12. #12
    Registered User LordVirusXXP's Avatar
    Join Date
    Dec 2002
    Posts
    86
    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?
    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.
    - Dean

  13. #13
    Registered User abrege's Avatar
    Join Date
    Nov 2002
    Posts
    369
    Originally 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.
    No, I think he really wants to call main() inside a function. He should refer himself to his sig
    I am against the teaching of evolution in schools. I am also against widespread
    literacy and the refrigeration of food.

  14. #14
    I mean like terminating the function right there and returning to main().

  15. #15
    Registered User abrege's Avatar
    Join Date
    Nov 2002
    Posts
    369
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. pointers
    By InvariantLoop in forum C Programming
    Replies: 13
    Last Post: 02-04-2005, 09:32 AM
  2. confused about adding controls to main window
    By terracota in forum Windows Programming
    Replies: 4
    Last Post: 11-24-2004, 12:35 PM
  3. int main (), main (), main (void), int main (void) HELP!!!
    By SmokingMonkey in forum C++ Programming
    Replies: 7
    Last Post: 05-31-2003, 09:46 PM
  4. what to do with the main window
    By stallion in forum Windows Programming
    Replies: 2
    Last Post: 01-28-2003, 08:58 PM
  5. void or int for main?:)
    By bigtamscot in forum C Programming
    Replies: 13
    Last Post: 09-27-2001, 03:11 AM