Thread: Functions calling other functions.

  1. #1
    Registered User
    Join Date
    Aug 2008
    Posts
    33

    Functions calling other functions.

    Hi guys, a bit of a lengthy post, but bear with me please:

    I've had a small problem in my program where I declared functions for a simple menu.
    There are several menus that are embedded - that is, one can lead to the other and back again.

    To do this, i've made each separate menu it's own function. So, I declare my functions before the main (all in one .cpp) and one by one they work. What doesn't work is when one function calls the other in it's own code, like this: (short example)

    Code:
    void showMenuOne()
    {
    if(selection == 1) { showMenuTwo(); }           //1. go to menu two
                                           //2. exit
    
    }
    
    void showMenuTwo()
    {
     if(selection == 2) { showMenuOne(); }                                 //1. go to menu one
                                                                    //2. exit
    
    }
    
    int main()
    {
    
    
    ///
    
    }
    I've left out a bit of code obviously, but basically, I get a compiler error, because the function showMenuOne(), can't see showMenuTwo() and therefore can't execute the instruction within itself.

    I played around with this problem for a little while, and came up with the solution of declaring the functions further up ahead the file, like this:

    Code:
    void showMenuOne();
    void showMenuTwo();
    
    
    void showMenuOne()
    {
       //code as before
    
    }
    
    void showMenuTwo()
    {
    
      //code as before
    
    }
    This works both during compile and run-time. So basically, my question is, was this the correct way of making functions "see" each other, or did I just "hack" my way through? If I did "hack" my way through, what would be the correct approach?

    Thanks,

    -K

  2. #2
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    That is the correct method of making functions visible before they are defined. They're called "function prototypes".

    However, your design is a little suspect. You shouldn't have to resort to recursion like that just to get a menu to display. You could, for example, use something like this:
    Code:
    void master_menu() {
        int id = 1;
        while(id > 0) {
            switch(id) {
            case 1:
                id = showMenuOne();
                break;
            case 2:
                id = showMenuTwo();
                break;
            }
        }
    }
    Here showMenuOne() and showMenuTwo() would return a number indicating which menu should be displayed next: either 1, or 2, or 0 (for "exit").

    That's just one way to do it. Perhaps you can think of others. Or you can stick with what you have, there's nothing wrong with it. It's just not the cleanest solution, I suppose.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  3. #3
    Registered User
    Join Date
    Aug 2008
    Posts
    33
    Hey dwks,

    Thanks for the quick reply and also for your suggestion, you're right, my code isn't the cleanest in the slightest, and using a switch would make a lot more sense for a menu.

    Cheers,

    -K

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problems calling functions
    By barryr in forum C Programming
    Replies: 12
    Last Post: 12-03-2009, 08:44 AM
  2. Replies: 12
    Last Post: 04-12-2009, 05:49 PM
  3. calling functions: exit and return
    By 911help in forum C Programming
    Replies: 3
    Last Post: 12-28-2007, 01:24 PM
  4. I Need Help Defining and Calling Functions
    By jonbuckets in forum C++ Programming
    Replies: 6
    Last Post: 10-25-2007, 09:46 AM
  5. Calling functions help
    By ForlornOdium in forum C++ Programming
    Replies: 14
    Last Post: 09-29-2003, 08:40 PM