0

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:

  1. The language supports static binding
  2. 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?

David Rodríguez - dribeas
  • 204,818
  • 23
  • 294
  • 489
wannabe programmer
  • 653
  • 1
  • 9
  • 23
  • It looks like this question is about lexical versus dynamic *scope*, not static versus dynamic binding. See [this](https://en.wikipedia.org/wiki/Scope_%28computer_science%29#Lexical_scoping_vs._dynamic_scoping) article. – molbdnilo Jun 19 '14 at 12:00
  • I read it, and it seems like it is what it's about, but I'm still having hard time understanding it due to the very confusing mutual recursion. Can you help me in understanding the output expectations? – wannabe programmer Jun 19 '14 at 12:03
  • Moreover, currently I think neither will compile. The const str lines don't look like pascal. – Marco van de Voort Jun 19 '14 at 12:03
  • it's not supposed to be compiled, it's half pseudo, half pascal. this is why I wrote "pascal-like" code. just to make the syntax clear. – wannabe programmer Jun 19 '14 at 12:05

0 Answers0