Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdf
Concept of scoping in programming languages
1. Concept of Scoping in
Programming Languages
Mohammed Jafar Sadik
ID-1620660042
Dept. of ECE, NSU
2. What Is Scoping?
▪ Scope refers to the visibility of variables. In other words, which parts
of your program can see or use it. Normally, every variable has a
global scope. Once defined, every part of your program can access
the variable.
3. Why Scoping?
▪ In General, Scoping is necessary if you want to reuse variable names
▪ Example:
int fn1()
{
int a =10;
}
int fn2()
{
int a=20;
} //We are using same variable in two different functions.
4. Why Scoping? (Contd.)
▪ Scoping rules are crucial in modular programming, so a change in one part of program
does not break an unrelated part.
▪ Example: Imagine you are a project manager in a software company, you have 10
developers working on your project.You divided the work in 10 parts, let’s assume they
are working on 10 different Java classes.They are defining the variables by their own.
They don’t even know if the variable they are defining, are being defined or has been
defined by other developers in other classes too.They shouldn’t even have to worry
about that too. Because the SCOPING of the variables are helping that they’ll work
only in their class and will not effect other or upper tier functionalities.
▪HOW IS IT POSSIBLE???
5. Why Scoping (contd.)
▪ A class defined by developer1:
public class Dog
{ String breed, int age, String
color;
void barking() { // }
void hungry() { // }
void sleeping() { // }
}
▪ A class defined by developer2:
public class Cat
{ String breed, int age, String
color;
void meaowing() { // }
void hungry() { // }
void sleeping() { // }
}
**Here two different developers defining two different classes with same variable
names.They shouldn’t be worried about this.Though they are working on the
same project but their variable scope is limited within their own class.
6. Types of Scoping…
▪ There are two types of Scoping:
1. Static Scoping or Lexical Scoping
2. Dynamic Scoping
7. Static or Lexical Scoping
▪ In Static scoping, definition of a variable is resolved by searching its containing block or function. If
that fails, then searching the outer containing block and so on. Let’s consider an example:
int a=10, b=20;
int fnc()
{ int a =5;
if(condition)
{
int c;
c=b/a;
printf(“%d”,c);
}
} //OUTPUT: 20/5=4
8. Static scoping in depth…
int fun1(int);
int fun2(int);
int a=5;
int main()
{
int a =10;
a=fun1(a);//calling the fun1, so we will jump into it
printf(“%d”, a);
}
int fun1(int b) //b=10
{
b=b+10; //b=20 here
b=fun2(b) //calling the fun2, so we will jump into it.
return b;
}
int fun2(int b) //b=20
{
int c;
c= a+ b; /*b=20 here, but no values for a, so we will go to the
global variable a*/
return c; //5+20=25, return 25
}
9. Static scoping in depth…
int fun1(int);
int fun2(int);
int a=5;
int main()
{
int a =10;
a=fun1(a);//calling the fun1, so we will jump into it
printf(“%d”, a);
}
int fun1(int b) //b=10
{
b=b+10; //b=20 here
b=fun2(b) //calling the fun2, so we will jump into it.
return b;
}
int fun2(int b) //b=20
{
int c;
c= a+ b; /*b=20 here, but no values for a, so we will go to the
global variable a*/
return c; //5+20=25, return 25
}
//b=25
now
10. Static scoping in depth…
int fun1(int);
int fun2(int);
int a=5;
int main()
{
int a =10;
a=fun1(a);//calling the fun1, so we will jump into it
printf(“%d”, a);
}
int fun1(int b) //b=10
{
b=b+10; //b=20 here
b=fun2(b) //calling the fun2, so we will jump into it.
return b;
}
int fun2(int b) //b=20
{
int c;
c= a+ b; /*b=20 here, but no values for a, so we will go to the
global variable a*/
return c; //5+20=25, return 25
}
//b=25
now
//a=25
11. Static scoping in depth…
int fun1(int);
int fun2(int);
int a=5;
int main()
{
int a =10;
a=fun1(a);//calling the fun1, so we will jump into it
printf(“%d”, a);
}
int fun1(int b) //b=10
{
b=b+10; //b=20 here
b=fun2(b) //calling the fun2, so we will jump into it.
return b;
}
int fun2(int b) //b=20
{
int c;
c= a+ b; /*b=20 here, but no values for a, so we will go to the
global variable a*/
return c; //5+20=25, return 25
}
//b=25
now
//a=25
Final output:25
12. ▪ In dynamic scoping, definition of a variable is resolved by searching its containing block and if not found, then
searching its calling function and if still not found then the function which called that calling function will be
searched and so on…
int x=5;
int fn1() int fn3()
{ int x =10; {
fn2(); fn4(); will search for variable x in fn4(), not found.
} } will search for variable x in the function that
int fn2() int fn4() called the fn4(), which is fn3(), no x in fn3(),
will search for x in the function that called fn3(),
{ { which is fn2(), not found, will search for x in the
fn3(); printf(“%d”, x); function which called fn2(), which is fn1(), in fn1()
} } we have variable x=10, output=10; but in case of
static scoping the output would be 5
Dynamic scoping…
13. Dynamic scoping in depth…
int fun1(int);
int fun2(int);
int a=5;
int main()
{
int a =10;
a=fun1(a);//calling the fun1, so we will jump into it
printf(“%d”, a);
}
int fun1(int b) //b=10
{
b=b+10; //b=20 here
b=fun2(b) //calling the fun2, so we will jump into it.
return b;
}
int fun2(int b) //b=20
{
int c;
c= a+ b; /*b=20 here, but no values for a, so we will go to
the calling function of fun2(), which is fun1(), no variable
named a, now will go to the calling function of fun1(), which
is main(). main() has the variable a, which is = 10*/
return c; //10+20=30, return 30
}
14. Dynamic scoping in depth…
int fun1(int);
int fun2(int);
int a=5;
int main()
{
int a =10;
a=fun1(a);//calling the fun1, so we will jump into it
printf(“%d”, a);
}
int fun1(int b) //b=10
{
b=b+10; //b=20 here
b=fun2(b) //calling the fun2, so we will jump into it.
return b;
}
int fun2(int b) //b=20
{
int c;
c= a+ b; /*b=20 here, but no values for a, so we will go to
the calling function of fun2(), which is fun1(), no variable
named a, now will go to the calling function of fun1(), which
is main(). main() has the variable a, which is = 10*/
return c; //10+20=30, return 30
}
//b=30
now
15. Dynamic scoping in depth…
int fun1(int);
int fun2(int);
int a=5;
int main()
{
int a =10;
a=fun1(a);//calling the fun1, so we will jump into it
printf(“%d”, a);
}
int fun1(int b) //b=10
{
b=b+10; //b=20 here
b=fun2(b) //calling the fun2, so we will jump into it.
return b;
}
int fun2(int b) //b=20
{
int c;
c= a+ b; /*b=20 here, but no values for a, so we will go to
the calling function of fun2(), which is fun1(), no variable
named a, now will go to the calling function of fun1(), which
is main(). main() has the variable a, which is = 10*/
return c; //10+20=30, return 30
}
//b=30
now
//a=30
16. Dynamic scoping in depth…
int fun1(int);
int fun2(int);
int a=5;
int main()
{
int a =10;
a=fun1(a);//calling the fun1, so we will jump into it
printf(“%d”, a);
}
int fun1(int b) //b=10
{
b=b+10; //b=20 here
b=fun2(b) //calling the fun2, so we will jump into it.
return b;
}
int fun2(int b) //b=20
{
int c;
c= a+ b; /*b=20 here, but no values for a, so we will go to
the calling function of fun2(), which is fun1(), no variable
named a, now will go to the calling function of fun1(), which
is main(). main() has the variable a, which is = 10*/
return c; //10+20=30, return 30
}
//b=30
now
//a=30
output:30