Nesting

Nesting

Top  Previous  Next

Since a procedure is an independent piece of code, it can itself contain procedures. Procedures can be nested inside each other. For example:

 

        procedure ONE;

 

                procedure TWO;

 

                        procedure THREE;

                        begin

                        ...

                        end;

 

                begin   \TWO

                ...

                end;

 

        begin   \ONE

        ...

        end;

 

Look at how these procedures are nested. Procedure THREE is nested inside procedure TWO, which in turn is nested inside procedure ONE.

 

Procedures can be nested up to eight levels deep. Here ONE is at the highest level, and THREE is at the lowest level. Note that the block for the highest level routine is last, but is executed first.

 

The same order applies to an entire program. The code for the main routine is always the last block in the program, and this highest-level block is always executed first. In fact, a program is just one big procedure.