ibmi-brunch-learn

Announcement

Collapse
No announcement yet.

Nested sub procedures

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • Nested sub procedures

    I have a program that has several sub procedures. I'll call them sub1 and sub2 in the interest of brevity. I want sub1 to call sub2. I put the prototype for sub2 inside of sub1. I placed them in my program consecutively, each one having a DCL-PROC and END-PROC. When I tried to compile my program, I got a compile error at the call 2 sub2, indicating it couldn't find sub2. I took the prototype of sub2 out of sub1 and placed it in the D specs of the main program and it compiled OK. I suspect, though I will have some scope issues. Should I have/can I have "nested" sub2 within sub1 and left the prototype of sub2 inside of sub1.

  • #2
    I have never tried putting a procedure prototype for a subprocedure inside another procedure, as opposed to globally. So I'm not sure if it should or should not work.
    And I have certainly never tried putting one procedure inside another.

    I don't see the logic in putting the prototype for sub2 inside sub1. Because that means if you ever needed to add sub3 and wanted that to also call sub2, you would have to duplicate the prototype

    But it's all irrelevant anyway, as subprocedures do not actually need prototypes, at least not in v7.2 (I don't know how many versions back this was true).

    // This compiles and works just fine for me in 7.2:
    Code:
           // Mainline
           sub1('A');
           *inlr = *on;
           return;
    
           // Procedure sub1, has no prototype
           Dcl-Proc sub1;
           Dcl-Pi *N;
             parm1     char(1)   const;
           End-Pi;
    
             sub2(parm1);
    
           End-Proc;
    
           // Procedure sub2, has no prototype
           Dcl-Proc sub2;
           Dcl-Pi *N;
             parm1     char(1)   const;
           End-Pi;
    
             // do something
    
           End-Proc;

    Comment


    • #3
      Sorry, Greg, I can't follow what you're asking. A quick example of the syntax you're having trouble with would help a lot.

      Comment


      • #4
        Greg, the compiler doesn't know that the "sub2" prototype within the "sub1" procedure is for the "sub2" procedure in your source.

        You should either just omit the prototypes, or put the prototypes in the global part of the source.

        If you actually needed to put the prototype for sub2 inside the sub1 procedure for some reason, you'd have to export the sub2 procedure to enable the binder to find it.

        Comment


        • #5
          (RPG doesn't support nesting subprocedures.)

          Comment

          Working...
          X