📜  门| Gate IT 2007 |第34章

📅  最后修改于: 2021-06-29 22:34:12             🧑  作者: Mango

考虑下面的假设程序语言,该程序允许使用全局变量并可以选择静态或动态作用域。

int i ;
program main ()
{
    i = 10;
    call f();
}
  
procedure f()
{   
    int i = 20;
    call g ();
}
procedure g ()
{   
    print i;
}

令x为静态作用域下打印的值,y为动态作用域下打印的值。则x和y为
(A) x = 10,y = 10
(B) x = 20,y = 10
[C) x = 10,y = 20
(D) x = 20,y = 20答案: (C)
说明:静态作用域:

int i ;
program main ()
{
    i = 10;
    call f();
}
  
procedure f()
{   
    int i = 20;
    call g ();
}
procedure g ()
{   
    print i; //as i=20 is scoped only within f() so it will point to global i
}
  So, 10 is printed
Dynamic scoping:
int i ;
program main ()
{
    i = 10;
    call f();
}
  
procedure f()
{   
    int i = 20; // here global scoped i is changed
    call g ();
}
procedure g ()
{   
    print i; // global value changed so, i=20 printed
} 

这个问题的测验
如果您在以上帖子中发现任何错误,请在下面发表评论