Recent

Author Topic: recursion issue  (Read 3460 times)

wgrandinetti

  • Newbie
  • Posts: 1
recursion issue
« on: June 12, 2015, 03:36:41 pm »
Hi!

   I have the following issue, which can be bypassed in two ways but I just want to report it in case that it were a bug.

Consider this program:

Code: [Select]
program issue;

function suma:integer;
var num: integer;
begin
     read(num);
     if num = -1
        then suma:=0
        else suma:= num + suma;
end;

begin
    writeln('enter a sequence of integer numbers ended by -1 ');
    writeln('the sum is: ',suma);
    readln;
end.   

This program yields an erroneos result because it interprets that the identifier suma in the expression (recursive call) is a variable instead of the parameterless call.

Solution 1: call parameterless functions always with () for instance suma()
Solution 2: add {$mode tp}{$H-}{$X+}

Bart

  • Hero Member
  • *****
  • Posts: 5290
    • Bart en Mariska's Webstek
Re: recursion issue
« Reply #1 on: June 12, 2015, 04:12:15 pm »
It's documented behaviour, so not a bug.

Bart

Leledumbo

  • Hero Member
  • *****
  • Posts: 8757
  • Programming + Glam Metal + Tae Kwon Do = Me
Re: recursion issue
« Reply #2 on: June 12, 2015, 07:34:40 pm »
Documented here.

User137

  • Hero Member
  • *****
  • Posts: 1791
    • Nxpascal home
Re: recursion issue
« Reply #3 on: June 15, 2015, 01:05:25 am »
Solution 3:
Code: [Select]
function suma: integer;
var num: integer;
begin
  read(num);
  if num = -1 then result:=0
  else result:=num + suma();
end;

jc99

  • Hero Member
  • *****
  • Posts: 553
    • My private Site
Re: recursion issue
« Reply #4 on: June 15, 2015, 02:02:11 am »
@User137:
To me this is Solution1,
you just replaces the return variable with 'result', which is of course better readable. And wgrandinetti should code this way.
But this does not make this a new Solution. Or please explain why you think it differs from solution1.
OS: Win XP x64, Win 7, Win 7 x64, Win 10, Win 10 x64, Suse Linux 13.2
Laz: 1.4 - 1.8.4, 2.0
https://github.com/joecare99/public
'~|    /''
,_|oe \_,are
If you want to do something for the environment: Twitter: #reduceCO2 or
https://www.betterplace.me/klimawandel-stoppen-co-ueber-preis-reduzieren

User137

  • Hero Member
  • *****
  • Posts: 1791
    • Nxpascal home
Re: recursion issue
« Reply #5 on: June 15, 2015, 07:52:01 pm »
He didn't mention the use of result.

jc99

  • Hero Member
  • *****
  • Posts: 553
    • My private Site
Re: recursion issue
« Reply #6 on: June 16, 2015, 04:36:32 am »
He didn't mention the use of result.
Yes, He didn't you are right. And using your code is much better.
But just using result and omitting the brackets would leave You with the same Problem.
OS: Win XP x64, Win 7, Win 7 x64, Win 10, Win 10 x64, Suse Linux 13.2
Laz: 1.4 - 1.8.4, 2.0
https://github.com/joecare99/public
'~|    /''
,_|oe \_,are
If you want to do something for the environment: Twitter: #reduceCO2 or
https://www.betterplace.me/klimawandel-stoppen-co-ueber-preis-reduzieren

 

TinyPortal © 2005-2018