Menu

User-defined function: Arithmetic expression in parameter input causes underflow. If variable is passed, no issues arise.

2024-08-06
2024-08-06
  • Chibamu Niusrec

    Chibamu Niusrec - 2024-08-06

    Sample program: recursive Fibonacci function.

           identification division.
           program-id. pmain.
           environment division.
           configuration section.
           repository.
               function fib-func.
           data division.
           working-storage section.
           01 n binary-char unsigned.
           procedure division.
           100-main.
               move 8 to n
               display "fibonacci(",n,"): ",fib-func(n) *> Works for n<=8.
               move 9 to n
               display "fibonacci(",n,"): ",fib-func(n) *> Segfaults due to underflow in function.
    
               display fib-func(7) *> Also segfaults. Function cannot handle numeric literals.
    
               exit program.
           end program pmain.
    
          *> //////////////////////////////////////
          *> FIBONACCI FUNCTION   
          *> //////////////////////////////////////
           identification division.
           function-id. fib-func.
           data division.
           linkage section.
           01 n binary-char unsigned.
           01 f-n binary-long unsigned.
           procedure division using n returning f-n.
           100-main.
               evaluate true
               when n=0
                   move 0 to f-n
               when n=1
                   move 1 to f-n
               when other
                   compute f-n = fib-func(n - 1) + fib-func(n - 2)
               end-evaluate
               goback .
           end function fib-func.
    

    It seems that GNUCobol functions cannot consistently handle numeric literals or arithmetic expressions. However, this is not what I found """"documented"""" here, for instance. So, am I doing something wrong or is this expected behavior? If it is, then it defeats the purpose of functions, no? Why use them over subroutines/subprograms if I need to declare intermediate variables anyway? I do not mean any of this as criticism.

    The correct code is shown below:

           identification division.
           program-id. pmain.
           environment division.
           configuration section.
           repository.
               function fib-func.
           data division.
           working-storage section.
           01 n binary-char unsigned.
           procedure division.
           100-main.
               move 7 to n
               display "fibonacci(",n,"): ",fib-func(n) *> Works as expected.
               move 8 to n
               display "fibonacci(",n,"): ",fib-func(n) *> Works as expected.
               move 9 to n
               display "fibonacci(",n,"): ",fib-func(n) *> Works as expected.
    
               exit program.
           end program pmain.
    
          *> //////////////////////////////////////
          *> FIBONACCI FUNCTION   
          *> //////////////////////////////////////
           identification division.
           function-id. fib-func.
           data division.
           local-storage section.
           01 prev-n binary-char unsigned.
           01 twice-prev-n binary-char unsigned.
           linkage section.
           01 n binary-char unsigned.
           01 f-n binary-long unsigned.
           procedure division using n returning f-n.
           100-main.
               evaluate true
               when n=0
                   move 0 to f-n
               when n=1
                   move 1 to f-n
               when other
               *> Removed arithmetic computations from fucntion arguments.   
                   compute prev-n = n - 1
                   compute twice-prev-n = n - 2
                   compute f-n = fib-func(prev-n) + fib-func(twice-prev-n)
               end-evaluate
               goback .
           end function fib-func.
    
     
  • László Erdős

    László Erdős - 2024-08-06

    Hi,
    for this there is a bug. It was created many years ago. Functions should handle the parameters similar like CALLs. If somebody doesn't know this bug, then he/she can get errors, where not easy to find out the real problem. If I right remember, this is not the first entry about this problem in the forum.

    László

     
    👍
    2

    Last edit: László Erdős 2024-08-06

Anonymous
Anonymous

Add attachments
Cancel