would error occurs?

sorry for troubling you guys again..

consider the follow part:
procedure menu;
var choice : integer;
If choice = 1 then
procedure store;
var store_choice : integer;
If store_choice = 1 then
program book;

excuse me, may i ask if set the program as shown above.
when the menu procedure executes, i choose 1 to enter the "store"
then, I choose "menu" at store, would error happened?
i.e. would two procedures execute at the same time?


  • I must point out that this is not my primary language and I am not [italic]that[/italic] good at programming however when looking through the code, I cannot see any reason why the two methods should be operated at the same time.

    The reason being when you jump to another method the thread should move however be aware that in some languages, after the method is complete, the thread jumps back to the original method.

    Also the program cannot run two methods at the same time because you have not introduced multi threading (not necessarily a good practice).

    I hope this is some help at least.

  • With TP and real-mode you cant have multiple procedures executing in the same time normally, unless you'd tweak the int8 (timer) and implement multitasking in real-mode. What you did in your code posted, is sort of a tail-recursion between two procedures, doesn't cause any errors but will eat up stack upon each call. So it can run for a limited time only, till runs out of stack space.
  • thanks for all you guys answering my question...
    As I am only a high school student, is quite difficult to understand experts like all of you....
    But I try to implement these code in Dev-Pascal, a syntax error occurs... May I ask what's happened? thanks=]
  • The code should run, but after a while it will run out of memory.

    Basically, here's what is happening. When you call a procedure, Pascal stores information so that you still have the information when you leave the procedure.

    In this case, when BOOK calls MENU, the computer stores info.
    When MENU calls STORE, it stores more info.
    When STORE calls MENU, it stores more info.

    Now if you keep calling these two procedures back and forth, it keeps storing info until you run out of memory.

    If you look at procedures like a list, after pushing '1' 5 times, it would be:


    This could just keep growing.
    When a procedure ends, it goes back to the point in the code that it was called.

    So if we change STORE to this:
    procedure store;
    var store_choice : integer;
    until (store_choice = 1);

    Then after pushing '1' 5 times, the list would look like:


    If you are still confused, look at this:
    01: procedure menu;
    02: var choice : integer;
    03: begin
    04: readln(choice);
    05: If choice = 1 then
    06: store
    07: end;
    09: procedure store;
    10: var store_choice : integer;
    11: begin
    12: repeat
    13: readln(store_choice);
    14: until (store_choice = 1);
    15: end;
    17: program book;
    18: begin
    19: menu
    20: end.

    Using the line #s above, if we run your program this is how it would execute:
    [color=Red]PUSH '1'[/color]
    [color=Red]PUSH '2'[/color]
    [color=Red]PUSH '1'[/color]

    If you follow the above line numbers you will see how it works. If you want to allow more than one visit to the STORE, you can put a loop in the MENU procedure as well.

    Hope this clears things up a bit.

    EDIT: Of course, looking at this again, I doubt that your error has to do with Out of Memory as each section of information that is stored is only around 10-20 bytes, so even if you only have 1MB of memory in your computer, it would still take you about 500,000 presses of '1' to run out of memory.
    If this is the case, you need to start pressing a different key ;)
Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!