identificationdivision.program-id.pmain.environmentdivision.configurationsection.repository.functionfib-func.datadivision.working-storagesection.01 nbinary-charunsigned.proceduredivision.100-main.move8 tondisplay "fibonacci(",n,"): ",fib-func(n)*> Works for n<=8.move9 tondisplay "fibonacci(",n,"): ",fib-func(n)*> Segfaults due to underflow in function.displayfib-func(7)*> Also segfaults. Function cannot handle numeric literals.exitprogram.endprogrampmain. *> ////////////////////////////////////// *> FIBONACCI FUNCTION *> //////////////////////////////////////identificationdivision.function-id.fib-func.datadivision.linkagesection.01 nbinary-charunsigned.01 f-nbinary-longunsigned.proceduredivisionusingnreturningf-n.100-main.evaluatetruewhenn=0move0 tof-nwhenn=1move1 tof-nwhenothercomputef-n=fib-func(n-1)+fib-func(n-2)end-evaluategoback .endfunctionfib-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:
identificationdivision.program-id.pmain.environmentdivision.configurationsection.repository.functionfib-func.datadivision.working-storagesection.01 nbinary-charunsigned.proceduredivision.100-main.move7 tondisplay "fibonacci(",n,"): ",fib-func(n)*> Works as expected.move8 tondisplay "fibonacci(",n,"): ",fib-func(n)*> Works as expected.move9 tondisplay "fibonacci(",n,"): ",fib-func(n)*> Works as expected.exitprogram.endprogrampmain. *> ////////////////////////////////////// *> FIBONACCI FUNCTION *> //////////////////////////////////////identificationdivision.function-id.fib-func.datadivision.local-storagesection.01 prev-nbinary-charunsigned.01 twice-prev-nbinary-charunsigned.linkagesection.01 nbinary-charunsigned.01 f-nbinary-longunsigned.proceduredivisionusingnreturningf-n.100-main.evaluatetruewhenn=0move0 tof-nwhenn=1move1 tof-nwhenother*> Removed arithmetic computations from fucntion arguments. computeprev-n=n-1computetwice-prev-n=n-2computef-n=fib-func(prev-n)+fib-func(twice-prev-n)end-evaluategoback .endfunctionfib-func.
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
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
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
Sample program: recursive Fibonacci function.
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:
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ó
Last edit: László Erdős 2024-08-06