I have this piece of pascal-like code:
const str = "three";
function foo(n : Integer) : String
const str = "two " ^ str;
function bar(n : Integer) : String
const str = "one" ^ str;
begin
if n=0 then bar := str
else bar := foo(n-1);
end;
begin
if n=0 then foo := str
else foo := bar(n-1);
end;
begin
writeln(foo(2));
writeln(foo(3));
end.
and I would like to figure out what the output will be if:
- The language supports static binding
- The language supports dynamic binding
For the first case, I think that it would be: "one one one two three" For the latter case, actually, I'm not sure.
I know that static binding means that the entities (in our case - the const "str") are bound/defined during compilation, it doesn't matter who called which function last, meaning it can be inferred just by reading the functions code. I also know that dynamic binding means that the entities (i.e "str") are bound with dependence on the calls stack. the mutual recursion got me a bit confused. So what output should I expect from each case and why?