IBMi (AS400) fans only ‘ Recursion in RPGfree






To my friend.

I want to dedicate this short post to a dear friend of mine, a developer who uses the C language and other modern programming tools, who repeatedly asked me: "Is the RPG language recursive?".


Until some time ago I had to answer: "unfortunately not". I defended myself by explaining that the RPG language is a programming language primarily used for developing business applications for the IBM i and there is no need for advanced features.




Bullshit! Actually the RPG language was not recursive, STOP!
But now, finally and with great satisfaction, I can answer yes, it is!


What does "Recursion" mean?

Recursion is a programming technique or approach where a function or a subroutine calls itself directly or indirectly to solve a problem. It involves breaking down a complex problem into simpler subproblems that can be solved easily, and then using the same function to solve those subproblems repeatedly until the base case is reached.


Recursion can be a powerful tool in programming because it allows you to write more elegant and concise code that is often easier to understand and maintain. However, recursion should be used with caution, as it can lead to stack overflow errors if not implemented properly.


Practical example:

Look my dearest friend, this is the source code for Display File:

RECU00V.DSPF


                                            DSPSIZ(*DS3)
                                            CHGINPDFT(UL CS)
                                            PRINT
                                            CF03(03)
                                            CF05(05)
                                            CF06(06)
      *******************************************************
      *  F01
      *******************************************************
                R F01
                                            OVERLAY
                                        1 34'Factorial'
                                            DSPATR(HI)

                                        3 10'Factorial of:'
                  PPFNUM         2Y 0B    +1EDTWRD('0 ')

                                          +4'is:'
                  PPRESU        15Y 0B    +1EDTCDE(Z)

                  F1ERR1        78A  O 22  2COLOR(RED)

                  F1ERR2        78A  O 23  2COLOR(RED)
      *******************************************************
      *  P00
      *******************************************************
                R P00
                                       24  3'F3=Exit'
        60                                +3'F6=Calculate' 





this is the source code for RPG program:


RECU00.RPGLE

**free
  // *************************************
  // Recursion in RPG                    *
  // *************************************
  ctl-opt dftactgrp(*no);

  dcl-f RECU00V workstn;//indds(Dspf);

  // *********************************
  // start working                   *
  // *********************************
  dow (1 = 1) ;

    write p00;
    exfmt f01;

    if (*in03);
      leave;
    endif;
    select;
    when PPFNUM = 0; // defined as 1 by
      PPRESU = 1;    // mathematical convention.
    when PPFNUM > 16;
      PPRESU = *zero;
      F1ERR1 = 'Too big. I calculate since 16';
    when PPFNUM >0 and PPFNUM <= 16;
      F1ERR1 = *blanks;
      PPRESU = factorial(PPFNUM);
    other;
    endsl;

  enddo;

  *inlr = *on;
  // **********************************
  dcl-proc factorial; //              *
  // **********************************
    dcl-pi *n zoned(15);
      Parm1 zoned(15) const;
    end-pi;

    if Parm1 = 1;
      return 1;
    else;
      return Parm1 * (factorial(Parm1 - 1));
    endif;

  end-proc;  



Compile both source and type call recu00




Calcualte Factorial value






Calcualte Factorial value
















That's it!





I appreciate all the comments made on this blog.







Comments

  1. This is not new. This has been around for the last 13 years since 7.1 came out.

    ReplyDelete
    Replies
    1. I know. My friend is an old friend. I just wanted to tell a story ;-)

      Delete
    2. Actually recursion came into RPG IV almost from the beginning WAY before 7.1. It was first available with V3R2/6 some 27+ years ago!

      Delete
  2. It is even possible at program level, if you create the program with activation group *new. Of course that is pretty much expensive though possible.

    ReplyDelete
  3. First time I wrote recursive programming 10 years ago in V7R1.

    ReplyDelete
  4. quel dow 1=1 grida vendetta :-)

    ReplyDelete

Post a Comment

Popular posts from this blog

IBMi (AS400) fans only ‘ Memories (IBM Coding Forms)

IBM i (AS400) fans only ' How to read a TXT file in the IFS with SQL