Search code examples
ibm-midrangerpgle

Return string from rpgle program


I want to return string from rpgle program.

/free 
return 'this is simple text';
/end-free

Can any please give me code snippet for this.


Solution

  • A program object must return values via parameters. If you want to return a value with the RETURN op code, you need to use a sub-procedure. A sub-procedure can be contained in a program object or a service program object. If you want to share the sub-procedure with multiple programs, you should use a service program. This is how you would define the sub-procedure for use within a service program:

    dcl-proc MyProcedure Export;
      dcl-pi *n Varchar(25);
      end-pi;
    
      return 'this is simple text';
    end-proc;  
    

    If you are just defining the sub-procedure within a program, you need to omit the Export keyword.

    To call the sub-procedure you then use something like this:

    dcl-s string     Varchar(25);
    
    string = MyProcedure();
    

    To return a value from a Program

    If you really want to return a value from a program you have to do it with parameters like this:

    ctl-opt DftActGrp(*No) ActGrp(*Caller)
            Main(MyProgram);
    
    dcl-proc MyProgram;
      dcl-pi *n;
        string        Varchar(25);
      end-pi;
    
      string = 'this is a simple string';
      return;
    end-proc;
    

    To call the program you would use something like this:

    dcl-s string      Varchar(25);
    
    dcl-pr MyProgram ExtPgm('MYPROGRAM');
      str          Varchar(25);
    end-pr;
    
    MyProgram(string);