Thread: Problem with pause function.

  1. #1
    Unregistered
    Guest

    Problem with pause function.

    I am just learning C++ and I was doing an example which involved function prototypes, and defining functions and have a problem with pausing the program after it returns it's last value.. This towards the end of the program.

    Code:
    ...
         areaOfYard= Area(lengthOfYard,widthOfYard);
    
         cout << "\nYour yard is ";
         cout << areaOfYard;
         cout << " square feet\n\n";
         return 0;
    
      }
    
      int Area(int l, int w)
      {
           return l * w;
      }
    Now the code before this block just shows the prototype function and the it declares the tye of value lengthOfYard etc. and asks the user for the values. At first I put "system ("pause");" function after return zero, but the final output didn't finish correctly because of the pause function. So I took that out of there and put it after the "return l * w;" but it still ended abruptly, How do I pause the program for future code's like these

  2. #2
    Ethernal Noob
    Join Date
    Nov 2001
    Posts
    1,901
    btw I am using Dev C++.

  3. #3
    Registered User
    Join Date
    May 2002
    Posts
    317
    Place it before the return 0;

  4. #4
    Ethernal Noob
    Join Date
    Nov 2001
    Posts
    1,901

    Talking

    thanks, hehe. But I still don't understand how that works.

  5. #5
    Registered User
    Join Date
    May 2002
    Posts
    317
    The return 0 has to be the last line in your function or main(). This is because that is what your function returns to the calling code(for main() that would be the OS). Therefore by placing system("pause") after your return 0, it never gets processed.

  6. #6
    Ethernal Noob
    Join Date
    Nov 2001
    Posts
    1,901
    But what about the section of code after the "return 0" in the example. Why does that execute?

  7. #7
    Registered User
    Join Date
    May 2002
    Posts
    317
    Physically in the IDE it is after the return 0, however actually it is just a function definition and when the compiler compiles the code it gets placed into the exe at some predefined point which the exe then 'jumps' to when it reaches your function call. The return 0 translates into the closing out code for your exe file which returns control back over to the OS and signals a normal termination, aka an error did not cause the exe to end. It is in all actuality the last line in your exe.

  8. #8
    Ethernal Noob
    Join Date
    Nov 2001
    Posts
    1,901

    Thumbs up

    Thanks, I realized why it did this in the second example. I guess I need to read more before I ask questions. I understand how the functions work now, thanks for the help.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Game Pointer Trouble?
    By Drahcir in forum C Programming
    Replies: 8
    Last Post: 02-04-2006, 02:53 AM
  2. Replies: 5
    Last Post: 02-08-2003, 07:42 PM
  3. structure vs class
    By sana in forum C++ Programming
    Replies: 13
    Last Post: 12-02-2002, 07:18 AM
  4. I need help with passing pointers in function calls
    By vien_mti in forum C Programming
    Replies: 3
    Last Post: 04-24-2002, 10:00 AM
  5. qt help
    By Unregistered in forum Linux Programming
    Replies: 1
    Last Post: 04-20-2002, 09:51 AM