COBOL

COBOL

COBOL

COBOL is responsible for the efficient, reliable, secure, and unseen day-to-day operations of the world's economy.

 View Only
Expand all | Collapse all

Calling using intrinsic functions as parameters

  • 1.  Calling using intrinsic functions as parameters

    Posted Mon April 03, 2023 12:54 AM

    None of the following work, and I think they should all be made to work (regardless of the COBOL standard, which I've not checked in this case).

    CALL 'mysubr1' USING BY CONTENT FUNCTION LENGTH(my-field)
    (You CAN use the LENGTH OF special register in this context)

    CALL 'mysubr2' USING BY CONTENT FUNCTION TRIM(my-field) FUNCTION LENGTH(FUNCTION TRIM(my-field))

    Additionally, you should also be able to do a CALL RETURNING in to an ADDRESS OF special register. 
    CALL 'subr-returning-pointer' RETURNING ADDRESS OF my-based-field

    And, while I don't have Enterprise COBOL v6.4 yet, also allow the result of a function to go in to ADDRESS OF.  Maybe that one is already allowed.
    SET ADDRESS OF my-based-field TO FUNCTION func-returning-pointer()

    Passing function results BY VALUE should also be similarly allowed.

    Would you vote yes for an official request of the above?



    ------------------------------
    Frank Swarbrick
    ------------------------------


  • 2.  RE: Calling using intrinsic functions as parameters

    Posted Tue April 04, 2023 02:20 AM
    Edited by Denis FALLAI Tue April 04, 2023 03:00 AM

    Hi Franck,

    Same problem exists also for some intrinsic functions and MOVE statement and can only be used in a COMPUTE statement : they can't be used as a sending operand of a MOVE statement. 

    From COBOL Language Reference:

    Numeric functions

    A numeric function can be used only where an arithmetic expression can be specified.
    A numeric function can be referenced as an argument for a function that allows a numeric argument.
    A numeric function cannot be used where an integer operand is required, even if the particular reference would yield an integer value. The INTEGER or INTEGER-PART functions can be used to force the type of a numeric argument to be an integer.

    Table of returned value by FUNCTION: https://www.ibm.com/docs/en/cobol-zos/6.4?topic=functions-function-definitions


    FUNCTION LENGTH returns an integer, so should be authorized to be an operand of a CALL.

    FUNCTION TRIM as no explicit length, so I unterstand it cannot be used as an operand of a CALL.

    ADDRESS OF is a special register and as a type and a length, (4 for LP32 or 8 bytes for LP64), so should be authorized to be an operand of a RETURNING clause.



    ------------------------------
    Denis FALLAI
    ------------------------------



  • 3.  RE: Calling using intrinsic functions as parameters

    Posted Wed April 05, 2023 01:22 AM

    Hi Denis, 

    There is no check of length in a call. So the normal approachen of parsing both the length and the string, would  add the problem of a need to call TRIM twice. An alternative could be that TRIM returns a dynamic and we could  parse that to a sub-module.  So yet another wish for enhancement :).  



    ------------------------------
    Per Trangbæk
    ------------------------------



  • 4.  RE: Calling using intrinsic functions as parameters

    Posted Wed April 05, 2023 01:00 AM

    Hi Frank

    I think it is a stretch to call it a FUNCTION, if it can not be placed everywhere where the return type can be placed.

    You have my vote!



    ------------------------------
    Per Trangbæk
    ------------------------------



  • 5.  RE: Calling using intrinsic functions as parameters

    Posted Wed April 05, 2023 02:30 AM

    I don't plan to add additional requests to my original request, but the ideas sound good and others are welcome to do so.  Or I may request them separately.
    Does anyone have any recommendation on how to word my original request(s)?



    ------------------------------
    Frank Swarbrick
    ------------------------------



  • 6.  RE: Calling using intrinsic functions as parameters

    Posted Wed April 05, 2023 02:33 AM

    By the way, TRIM does seem to have a (dynamic) length.  The following works (FUNCTION keyword omitted):
    COMPUTE FIELD-LEN = LENGTH(TRIM(EDIT-SQLCODE LEADING))



    ------------------------------
    Frank Swarbrick
    ------------------------------



  • 7.  RE: Calling using intrinsic functions as parameters

    Posted Wed April 05, 2023 03:11 AM

    You may be right, I'm unsure how the internal works. The manual (6.3) just state it is a string returned, not if it is a dynamic. If you are right, it should be even easier for the compiler people to fix :) 

    You omitting the keyword FUNCTION show how annoying it is and is yet another good enhancement request.

    By the way, I measured a few metodes to find the length of a string, minus leading spaces. Under some circumstances your method was the fastes.  



    ------------------------------
    Per Trangbæk
    ------------------------------



  • 8.  RE: Calling using intrinsic functions as parameters

    Posted Wed April 05, 2023 05:37 AM
    Edited by Frank Swarbrick Wed April 05, 2023 10:13 AM

    You can already omit the FUNCTION keyword (in COBOL 6.3 and later, at least; not sure about earlier) by using a REPOSITORY paragraph:

     

    ENVIRONMENT DIVISION.

    CONFIGURATION SECTION.

    REPOSITORY.

        FUNCTION ALL INTRINSIC.

     

    By doing this you no longer need the word FUNCTION elsewhere.


    The information contained in this electronic communication and any document attached hereto or transmitted herewith is confidential and intended for the exclusive use of the individual or entity named above. If the reader of this message is not the intended recipient or the employee or agent responsible for delivering it to the intended recipient, you are hereby notified that any examination, use, dissemination, distribution or copying of this communication or any part thereof is strictly prohibited. If you have received this communication in error, please immediately notify the sender by reply e-mail and destroy this communication. Thank you.






  • 9.  RE: Calling using intrinsic functions as parameters

    Posted Tue April 18, 2023 12:26 AM

    This really shows I should spend some more time reading the manual! Thanks.



    ------------------------------
    Per Trangbæk
    ------------------------------



  • 10.  RE: Calling using intrinsic functions as parameters

    Posted Wed April 05, 2023 10:22 AM
    Edited by Frank Swarbrick Wed April 05, 2023 10:38 AM

    Here's a somewhat OK workaround using a dynamic length string.  This is an actual usage, not just an example.

    77  EDIT-SQLCODE                PIC +++999. 
    77  DYNLEN-STRING               PIC X DYNAMIC.
        [...]
        MOVE LS-SQLCODE TO EDIT-SQLCODE                     
        MOVE TRIM(EDIT-SQLCODE LEADING) TO DYNLEN-STRING    
        CALL 'INSERT-MSG-DATA'                              
             USING SQL-INFO-TOKEN                           
                   CONTENT DYNLEN-STRING AS FIXED LENGTH 254
                   VALUE LENGTH OF DYNLEN-STRING            
                         1                                  


    where INSERT-MSG-DATA is defined as the following:

     id division.                                             
     program-id. 'INSERT-MSG-DATA'.                           
                                                              
     data division.                                           
     local-storage section.                                   
     01  isrtdata.                                            
         05  isrt_len                pic s9(4) comp-5.        
         05  isrt_txt.                                        
             10  pic x occurs 0 to 254 depending on isrt_len. 
                                                              
     linkage section.                                         
     01  condtok                     pic x(12).               
     01  isrt-no                     pic s9(9) comp-5.        
     01  isrt-len                    pic s9(9) comp-5.        
     01  isrt-text.                                           
         05  pic x occurs 0 to 254 depending on isrt-len.     
                                                              
     procedure division using condtok isrt-text               
                              by value isrt-len isrt-no.      
         compute isrt_len = isrt-len                          
         move isrt-text to isrt_txt                           
         call 'CEECMI' using condtok isrt-no isrtdata omitted 
         goback.                                              
                                                              
     end program 'INSERT-MSG-DATA'.                           



    ------------------------------
    Frank Swarbrick
    ------------------------------