areymond Contributor.
Contributor.
2834 views

User defined functions

Jump to solution

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

0 Likes
1 Solution

Accepted Solutions
Micro Focus Expert
Micro Focus Expert

RE: User defined functions

Jump to solution

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.

View solution in original post

0 Likes
3 Replies
Micro Focus Expert
Micro Focus Expert

RE: User defined functions

Jump to solution

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.
0 Likes
areymond Contributor.
Contributor.

RE: User defined functions

Jump to solution

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

0 Likes
Micro Focus Expert
Micro Focus Expert

RE: User defined functions

Jump to solution

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.

View solution in original post

0 Likes
The opinions expressed above are the personal opinions of the authors, not of Micro Focus. By using this site, you accept the Terms of Use and Rules of Participation. Certain versions of content ("Material") accessible here may contain branding from Hewlett-Packard Company (now HP Inc.) and Hewlett Packard Enterprise Company. As of September 1, 2017, the Material is now offered by Micro Focus, a separately owned and operated company. Any reference to the HP and Hewlett Packard Enterprise/HPE marks is historical in nature, and the HP and Hewlett Packard Enterprise/HPE marks are the property of their respective owners.