calling functions within functions

This is a discussion on calling functions within functions within the C Programming forums, part of the General Programming Boards category; Hi, just a question about functions. For example, i have a main function, which calls a displayMenu func, this then ...

  1. #1
    Registered User
    Join Date
    Mar 2005
    Posts
    31

    calling functions within functions

    Hi, just a question about functions. For example, i have a main function, which calls a displayMenu func, this then calls a series of other functions until finally the gameOver function is called. At the end of this function, i call displayMenu again to show the menu, so that the user may replay the game if they wish. My question is, would my computer create the 2nd displayMenu function within the gameOver function (already an extension of my first displayMenu function) or would it overwrite as such, the first menu call i did at the beginning of my program. I think i've explained that diabolically, so i'll try and draw a little graphic:

    Would my calling the 2nd menu function make the computer do something like this:

    Code:
    main
      displayMenu
        someotherFuncs
          gameOver
            displayMenu
              someotherFunctions
                gameOver
                  displayMenu
    etc etc...

    or would it do this:

    Code:
    main
      displayMenu
        someotherFuncs
          gameOver
      displayMenu
        someotherFuncs
          gameOver
      displayMenu
    etc etc...

    --------------------

    I am currently writing a snake game, and at first i had the main menu running in a while loop and calling appropriate functions until the user entered the exit command. however this produced some weird results, so i changed to calling the main menu function whenever the game finished instead of just exiting back into the loop. This got me thinking about my above question, as if the answer is indeed the first case, this cant be very good for my computer when running the program at all hehe.

    ps. I think i'm going for the record on most uses of the word 'function' in a thread.

    any help greatly appreciated

    Thanks.

  2. #2
    Super Moderator
    Join Date
    Sep 2001
    Posts
    4,913
    If I understand you correctly, then your program is actually in an infinite loop, and will just use up resources until it can't support itself and it crashes. I would either redesign the structure of your program, or make some provision to allow the program to break out of this cycle at a certain point. I recommend the first. If you need some help, post your code.

  3. #3
    Registered User
    Join Date
    Jan 2005
    Posts
    847
    Easiest thing to do would be to call DisplayMenu from main in a loop which loops untill DisplayMenu returns a value indicating the user wishes to quit and return to main after the GameOver() call.
    For reference unless you've used the inline directive functions aren't added to other functions the program jumps to that function and the return makes the program jump to where it came from so you only get one copy of each function.

  4. #4
    Registered User
    Join Date
    Mar 2005
    Posts
    31
    Thanks for the help

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 12
    Last Post: 04-12-2009, 06:49 PM
  2. Replies: 9
    Last Post: 01-26-2008, 03:12 AM
  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, 10:46 AM
  5. Calling functions help
    By ForlornOdium in forum C++ Programming
    Replies: 14
    Last Post: 09-29-2003, 09:40 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21