-3

I have the following code, the result is 42, but why? The answer must be 13, because 7+6=13.

program HelloWorld;

function F (a : integer) : integer;
begin
  if (a = 1) or (a = 2) then
    F := 2
  else
    F := F(a-1) + F(a-2);
end;

begin
  WriteLn(F(8));
end.
Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
Clarys
  • 37
  • 1
  • FWIW, code snippets like you had are only meant for JavaScript. Won't work in Pascal. I removed the "Run code snippet" gizmo. – Rudy Velthuis Apr 08 '19 at 14:56
  • 4
    Did you consider using a debugger? – David Heffernan Apr 08 '19 at 15:07
  • This post will, potentially, live here forever. So we want to make it clear and well formatted for future visitors. As a new user, you should take the [tour]. In fact, doing so is part of the sign up process. Did you perhaps skip it? – David Heffernan Apr 08 '19 at 15:47
  • 1
    You can read about recursive calculation of [Fibonacci sequence](https://en.wikipedia.org/wiki/Fibonacci_number) - you function retuen doubled `Fib(a)`. BTW, 42 is universal answer ;) – MBo Apr 08 '19 at 16:22
  • I'm confused, where do you assign the function result? I'd expect to see `Result :=` not `F :=`. – Jerry Dodge Apr 08 '19 at 16:25
  • 2
    @JerryDodge `F :=` is the Pascal syntax for assigning result to a function. – Keith Miller Apr 08 '19 at 17:03
  • @KeithMiller Interesting, I've always used `Result` to assign a result. Never knew you could use the same function name to assign a result. – Jerry Dodge Apr 08 '19 at 17:50
  • 1
    @JerryDodge At the time, the introduction of `Result` in Delphi 1 was a major improvement over TP's assignment to the function name. It made it much easier to change the function name when refactoring, etc. – MartynA Apr 08 '19 at 21:06
  • 1
    @JerryDodge assigning to `Result` is valid in Delphi only if [Extended Syntax](http://docwiki.embarcadero.com/RADStudio/en/Extended_syntax_(Delphi)) is enabled, which it is by default. – Remy Lebeau Apr 08 '19 at 21:25

2 Answers2

5

You are not adding just 7+6, like you claim. If you want that, you need to change this line:

F := F(a-1) + F(a-2);

To this instead:

F := (a-1) + (a-2);

Otherwise, you are actually adding F(7)+F(6), which is:

(F(6) + F(5)) + (F(5) + F(4))

Which is:

((F(5) + F(4)) + (F(4) + F(3))) + ((F(4) + F(3)) + (F(3) + F(2)))

and so on, for every recursive call to F(a) where a > 2. That is why you end up with a result of 42 instead of 13.

Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
  • i don't understand, how about F(4), the result is 6 – Clarys Apr 08 '19 at 16:11
  • 2
    `F(4)` returns `F(3) + F(2)`, which returns `(F(2) + F(1)) + 2`, which returns `(2 + 2) + 2`, which is `6` – Remy Lebeau Apr 08 '19 at 20:26
  • 1
    FWIW, F(n) returns 2*Fibonacci(n-1): 2, 2, 4, 6, 10, 16, 26, 42, 68, 110. Makes sense. @Clarys: Please read up about [recursion](https://en.wikipedia.org/wiki/Recursion_(computer_science)) to understand what is going on here. – Rudy Velthuis Apr 09 '19 at 08:58
-1
function f(a : Integer) : Integer;
begin
   if (a=1) OR (a=2) then
    Result := 2
   else
    Result := (a-1) + (a-2);                                                        
end;

f(8)
Result = 13
CoyBit
  • 1,592
  • 1
  • 17
  • 19
Elan
  • 1