简体   繁体   中英

Example for RTNPARM keyword and use case

After reading the IBM documentation about RTNPARM keyword, it's still not clear, to me, how I have to handle it.
Could someone give me a small procedure example?

If it is much faster, why IBM didn't make it implicit?

You don't "have to handle it", that's the beauty of it.

 D TestProc1       Pr         32000a

 D TestProcV7      Pr         32000a    RTNPARM

myResults = TestProc1();
myResults = TesProcV7();

The call to the procedure is coded the same regardless of rather or not RTNPARM is specified.

It's not the default (ie. implicit) as it was added at v7. Also it's only faster if the returned value is bigger than 16 bytes.

Plus, if the called procedure has optional parms, you have have to deal with them a bit differently inside the called procedure.

Given

 D MyProc          PI                  LikeDS(myResult)
 D  Compulsory                   20a
 D  Optional                     10a   Options(*NoPass)

Old way:

   If %Parms > 1; 
      DoStuff();
   EndIf;

New way

   If %ParmNum(Optional) <= %Parms; 
      DoStuff();
   EndIf;

More detail and examples above from Large Subprocedure Return Values: V7 Brings Relief

RTNPARM is for procedures that return a very long character value that is frequently only partially used. The return value gets pushed onto the stack, and it takes longer to push a 64K character onto the stack vs. a 10 byte character. Parameters passed by reference only put a pointer on the stack. When you use the RTNPARM keyword, it treats the return value like a parameter passed by reference. Internally, the first parameter becomes the return value passed by reference. You still pass and retrieve the return value the same way you would normally, but if you want to get the number of parameters using %parms() , the parameter numbers will be incremented by one. IBM introduced the %parmnum() built-in to deal with this.

So here is a simple example:

   dcl-proc sample;
     dcl-pi *n Varchar(65535) RtnParm;
       parm1   VarChar(256) const options(*nopass);
     end-pi;

     dcl-s result         Varchar(65535) Inz('');
     dcl-s str1           Varchar(256) Inz('');

     if %parms() >= %parmnum(parm1);
       str1 = parm1;
     endif;

     // do some stuff

     return result;
   end-proc;

Another reason that RTNPARM can't be implicit is that it's a purely RPG thing. If the procedure is written in another programming language or called from another language, RTNPARM can't be used because the other language would assume that the normal return-value mechanism was being used.

如果将子过程与Microsoft VB / VBA进行比较,则带有rtnparm的子过程是一个函数,而没有rtnparm的子过程是一个子过程。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM