what is static scoping how is it different from dynamic sco
what is static scoping ? how is it different from dynamic scoping ?
Solution
Scope is nothing but the visibility by an object.
Dynamic scoping in the part of the program refres to portion of run time and static scoping is nothing but a potion in small area of the program. A fundamental distinction is noting but static scoping. That is at compile time. Otherwise we can call it range of functionality of the variable. These are also called as private variables. Where as dynamic scoping declares outside the range which they are defined. This type is called dynamic scoping.
For Example:
const int a=5;
int abc(){
int b=a+5;
return b;
}
int xyz(){
int a=2;
return abc();
}
int main(){
abc(); // gets 10
xyz(); // gets 10
return 0;
}
const int a=5;
int abc(){
int b=a+5;
return b;
}
int xyz(){
int a=2;
return abc();
}
int main(){
abc(); // gets 10
xyz(); // gets 7
return 0;
}
In the program as first is static will store in heap and dymamic scoping is during run time and stored in stack.
| Static scoping | Dynamic scoping |
| const int a=5; int abc(){ int b=a+5; return b; } int xyz(){ int a=2; return abc(); } int main(){ abc(); // gets 10 xyz(); // gets 10 return 0; } | const int a=5; int abc(){ int b=a+5; return b; } int xyz(){ int a=2; return abc(); } int main(){ abc(); // gets 10 xyz(); // gets 7 return 0; } |

