User defined functions

Hello,

I know it possible to define functions in a repository :

mytest.cbl

     $set preservecase case
       identification division.
       program-id. mytest.
       environment division.
       repository.
           function MYFUNCTION.
       data division.
       ...
       procedure division.
           move function myfunction(myparameter) to myresult
      .........
      end program mytest.

And then myfunction.cbl :

      $set repository(update ON) preservecase case
       function-id. myfunction,
       ....
       linkage section.
       1 myparam ....
       1 myresult .....
       procedure division using myparam returning myresult
       .......
      end function myfunction.

But we have two separate source files.

Is it possible to create embedded functions in a cobol program ?

Like we have

program-id. a.
  call "b" using myparam, myresult
  goback.
  program-id. b.
  linkage section.
  1 myparam ....
  1 myresult .....
  procedure division using myparam myresult    
     do something
      goback.
  end program b.
end program a.


which would give :

program-id. a.
  move function b(myparam) to myresult
  goback.

  function-id. b.
  linkage section.
  1 myparam ....
  1 myresult .....
  procedure division using myparam returning myresult
     do something
     goback.
  end function b.
end program a.


Regards,


Alain

  • You can define the function within the same source file as the program that calls it.

    Here is an example:

          $set preservecase case repository(update ON)
           id division.      
           function-id. myfunction.
           linkage section.
           01 myparam    pic x(5).
           01 myresult   pic x(5).
           procedure division using myparam returning myresult.
               move myparam to myresult
               goback.
           end function myfunction.
           
           id division.
           program-id. mytest.
           environment division.
           repository.
               function MYFUNCTION.
           data division.
           working-storage section.
           01 myparameter   pic x(5) value "12345".
           01 myresult      pic x(5) value spaces.
           procedure division.
               move function myfunction(myparameter) to myresult
               display myresult
               goback.
      
           end program mytest.
    
  • Thank you for the tip. I should have think about it !

    The only inconvenient in comparison to nested programs are the global/local variable. It can be useful to declare global variables used by a subprogram wihtin a program.

    Regards,

    Alain

  • Verified Answer

    The reason why a function cannot be nested like a program is that it gets stored in the repository which is available to the entire run-unit and not just to one compilation unit.

    Although you cannot use global variables within a function you can use external variables in order to share data other than the parameters which are passed to the function.

    Example:

          $set preservecase case repository(update ON)
           id division.      
           function-id. myfunction.
           working-storage section.
           01 myexternal-data   pic x(5) external.
           linkage section.
           01 myparam    pic x(5).
           01 myresult   pic x(5).
           procedure division using myparam returning myresult.
               move myparam to myresult
               move all "2" to myexternal-data
               goback.
           end function myfunction.
           
           id division.
           program-id. mytest.
           environment division.
           repository.
               function MYFUNCTION.
           data division.
           working-storage section.
           01 myparameter      pic x(5) value "12345".
           01 myresult         pic x(5) value spaces.
           01 myexternal-data  pic x(5) external.
           procedure division.
               move all "1" to myexternal-data
               move function myfunction(myparameter) to myresult
               display myresult
               goback.
           
           end program mytest.