0% found this document useful (0 votes)
7 views39 pages

Lecture 15 & 16

The document discusses the concepts of scope, lifetime, and referencing environments in programming languages, highlighting the differences between static and dynamic scoping. It explains the visibility of variables, the use of named constants, and the significance of blocks in defining variable scope. Additionally, it provides examples in C++ to illustrate these concepts, including the use of the scope resolution operator.

Uploaded by

mmuzammal2003
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views39 pages

Lecture 15 & 16

The document discusses the concepts of scope, lifetime, and referencing environments in programming languages, highlighting the differences between static and dynamic scoping. It explains the visibility of variables, the use of named constants, and the significance of blocks in defining variable scope. Additionally, it provides examples in C++ to illustrate these concepts, including the use of the scope resolution operator.

Uploaded by

mmuzammal2003
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 39

COMSATS University Islamabad,

Wah Campus

Lecture No. 15 & 16


Scope, Scope and Lifetime, Referencing Environments, Named Constants, Static
Scope • Blocks • Declaration Order • Global Scope • Dynamic Scope • Different
types of lifetimes• Examples • Example with global variable, Static and Dynamic
Scoping, Data types: character, string

Theory Of Programming Languages


Fall 2024 1
~~~ Dr. Khalid Iqbal Khattak ~~~
Scope
• Scope is a concept that refers to where values and functions
can be accessed.
• Various scopes include:
– Global scope (a value/function in the global scope can be used anywhere in
the entire program)
– File or module scope (the value/function can only be accessed from within
the file)
• The context in which a name is visible is called its scope.
– For example, if you declare a variable x within a function, x is only
visible within that function body

Theory Of Programming Languages


Fall 2024 2
~~~ Dr. Khalid Iqbal Khattak ~~~
Scope
• The scope of a variable is the range of statements in which
the variable is visible.
• A variable is visible in a statement if it can be referenced or
assigned in that statement.
• C++ Example:

Theory Of Programming Languages


Fall 2024 3
~~~ Dr. Khalid Iqbal Khattak ~~~
Scope and Lifetime
• Scope refers to the region of code where a variable is visible and can be
accessed.
• Lifetime refers to the time during which a variable exists and holds its
value.
• The scope of a local variable is limited to the block in which it is declared
(e.g., within a function or a loop).

Theory Of Programming Languages


Fall 2024 4
~~~ Dr. Khalid Iqbal Khattak ~~~
Static Lifetime
• Bound to memory cells before execution begins and remains
bound to the same memory cell(s) throughout execution
• Examples:
– All FORTRAN 77 variables
– C and C++ static variables
• Advantages:
– Efficiency (direct addressing)
– History-sensitive subprogram support
• Disadvantage:
– Lack of flexibility (no recursion)
• Scope and lifetime are sometimes closely related, but are
different concepts.
– Consider a static variable in a C or C++ function
Theory Of Programming Languages
Fall 2024 5
~~~ Dr. Khalid Iqbal Khattak ~~~
Functions/Block, Local & Global Variables

Theory Of Programming Languages


Fall 2024 6
~~~ Dr. Khalid Iqbal Khattak ~~~
Static and Dynamic Scoping

Lexical scoping, also known as It refers to setting the scope, or range of


functionality, of a variable so that it may be
static scoping, is a convention called (referenced) from within the block of
used with many modern PLs. code in which it is defined.

Dynamic scope refers to execution context.

In technical terms, each name has a global stack of bindings.

A local variable with name x pushes a binding onto the global x stack
(which may have been empty), which is popped off when the control flow
leaves the scope

Theory Of Programming Languages


Fall 2024 7
~~~ Dr. Khalid Iqbal Khattak ~~~
Static Scoping Example
// A C program to demonstrate static scoping.
#include<stdio.h>
int x = 10;
// Called by g()
int f()
{ return x;}
// g() has its own variable
Output 10
// named as x and calls f()
int g() Keyword Keyword
Perl suppor “my” “local”
{ int x = 20; ts both defines a defines a
return f();} dynamic statically dynamically
int main() and static scoped scoped
local
{ printf("%d", g());scoping. local
variable, variable.
printf("\n");
return 0;}
Fall 2024
Theory Of Programming Languages
8
~~~ Dr. Khalid Iqbal Khattak ~~~
Dynamic Scoping Example
// Since dynamic scoping is very uncommon in
// the familiar languages, we consider the
// following pseudo code as our example.
int x = 10;
// Called by g()
int f() { return x;}
// g() has its own variable
// named as x and calls f()
int g()
{ int x = 20;
return f();}
main(){ printf(g());}
Theory Of Programming Languages
Fall 2024 9
~~~ Dr. Khalid Iqbal Khattak ~~~
Referencing Environments
• The referencing environment of a statement is
the collection of all variables that are visible in
the statement.
• The referencing environment of a statement in a
static-scoped language is the variables declared in
its local scope plus the collection of all variables
of its ancestor scopes that are visible. OR
• Reference Environment mean the environment
used for testing prior to deploying to Production

Theory Of Programming Languages


Fall 2024 10
~~~ Dr. Khalid Iqbal Khattak ~~~
Referencing Environments

Theory Of Programming Languages


Fall 2024 11
~~~ Dr. Khalid Iqbal Khattak ~~~
Referencing Environments: Python Example

Theory Of Programming Languages


Fall 2024 12
~~~ Dr. Khalid Iqbal Khattak ~~~
Named Constants
• A named constant is a variable that is bound to a value only once.
– Named constants are useful as aids to readability and program
reliability.
– Readability can be improved, for example, using the name pi instead
of the constant 3.14159265.
• Use of named constants is to parameterize a program

Theory Of Programming Languages


Fall 2024 13
~~~ Dr. Khalid Iqbal Khattak ~~~
Blocks
• A block is a set of statements that are grouped and
treated as a single unit.
• Blocks are used to define the scope of variables,
control the flow of execution in conditional
statements and loops, and encapsulate code in
functions, methods, or classes

Theory Of Programming Languages


Fall 2024 14
~~~ Dr. Khalid Iqbal Khattak ~~~
Blocks
• A block is a set of statements that are grouped and treated as
a single unit.
• Blocks are used to define the scope of variables, control the
flow of execution in conditional statements and loops, and
encapsulate code in functions, methods, or classes

Theory Of Programming Languages


Fall 2024 15
~~~ Dr. Khalid Iqbal Khattak ~~~
Declaration Order
• A declaration is a language construct
specifying identifier properties:
– It declares a word's (identifier's) meaning.
– Declarations are most commonly used for
functions, variables, constants, and classes, but
can also be used for other entities such as
enumerations and type definitions

Theory Of Programming Languages


Fall 2024 16
~~~ Dr. Khalid Iqbal Khattak ~~~
Declaration Order
• A declaration is a language construct specifying
identifier properties:
– It declares a word's (identifier's) meaning.
– Declarations are most commonly used for functions,
variables, constants, and classes, but can also be used for
other entities such as enumerations and type definitions

Theory Of Programming Languages


Fall 2024 17
~~~ Dr. Khalid Iqbal Khattak ~~~
Global Scope
• Variables defined outside a function, are
called global variables and their scope is
global.
• Variables defined inside a function are known
as local variables.

Theory Of Programming Languages


Fall 2024 18
~~~ Dr. Khalid Iqbal Khattak ~~~
Scope resolution Operator example
1. // C++ program to show that we can access a global variable
2. // using scope resolution operator :: when there is a local
3. // variable with same name
4. #include<iostream>
5. using namespace std;
Output
6. int x; // Global x
7. int main()
Value of
8. { global x is 0

9. int x = 10; // Local x


10. cout << "Value of global x is " << ::x;
11. cout << "\nValue of local x is " << x; Value of
local x is 10
12. return 0;
13. }

Theory Of Programming Languages


Fall 2024 19
~~~ Dr. Khalid Iqbal Khattak ~~~
Scope resolution Operator example
1. // C++ program to show that scope resolution operator :: is
2. // used to define a function outside a class
3. #include <iostream>
4. using namespace std;
5. class A {
6. public: Output

7. // Only declaration
8. void fun(); };
9. // Definition outside class using ::
10. void A::fun() { cout << "fun() called"; } fun() called

11. int main()


12. { A a;
13. a.fun();
14. return 0;
Theory Of Programming Languages
Fall 2024 20
15. } ~~~ Dr. Khalid Iqbal Khattak ~~~
To access a class’s static variables.
1. // C++ program to show that :: can be used to access static
2. // members when there is a local variable with same name
3. #include<iostream>
4. using namespace std;
5. class Test
6. { static int x;
7. public:
8. static int y;
9. // Local parameter 'x' hides class member
10. // 'x', but we can access it using ::
11. void func(int x)
12. { // We can access class's static variable Output
13. // even if there is a local variable
14. cout << "Value of static x is " << Test::x;
15. cout << "\nValue of local x is " << x;
16. } };
17. // In C++, static members must be explicitly defined
18. // like this
Value of static x is 1
19. int Test::x = 1; Value of local x is 3
20. int Test::y = 2; Test::y = 2
21. int main()
22. { Test obj;
23. int x = 3 ;
24. obj.func(x);
25. cout << "\nTest::y = " << Test::y;
26. return 0;} Theory Of Programming Languages
Fall 2024 21
~~~ Dr. Khalid Iqbal Khattak ~~~
Multiple Inheritance Example
1. // Use of scope resolution operator in multiple inheritance.
2. #include<iostream>
3. using namespace std;
4. class A {protected:
5. int x;
6. public:
7. A() { x = 10; } };
8. class B {protected:
9. int x;
Output
10. public:
11. B() { x = 20; } };
12. class C: public A, public B { public: B's x A's x
13. void fun()
14. { cout << "A's x is " << A::x;
is 20 is 10
15. cout << "\nB's x is " << B::x;
16. } };
17. int main()
18. { C c;
19. c.fun();
20.2024return 0; }
Fall
Theory Of Programming Languages
22
~~~ Dr. Khalid Iqbal Khattak ~~~
Class inside another class: Example
1. // Use of scope resolution class inside another class.
2. #include <iostream>
3. using namespace std;
4. class outside {
5. public:
6. int x;
If a class exists inside
7. class inside { another class we can
8. public: use the nesting class
9. int x; to refer the nested
10. static int y; class using the scope
11. int foo(); resolution operator
12. }; };
13. int outside::inside::y = 5;
14. int main()
15. { outside A;
16. outside::inside B; }
Theory Of Programming Languages
Fall 2024 23
~~~ Dr. Khalid Iqbal Khattak ~~~
Global Scope
• C, C++, PHP, JavaScript, and Python, allow a program
structure i.e.
– a sequence of function definitions,
• variable definitions can appear outside the functions.
• Definitions outside functions in a file create global variables (visible
to those functions).
• C and C++ have
– Declarations and definitions of global data
– Declaration specify types of attributes without storage allocation.
– Definitions specify attributes with storage allocation.
• In C++, a global variable that is hidden by a local with the same
name can be accessed using the scope operator (::).
• E.g. if x is a global that is hidden in a function by a local named
x, the global could be referenced as ::x.
Theory Of Programming Languages
Fall 2024 24
~~~ Dr. Khalid Iqbal Khattak ~~~
Global Scope
• A PHP variable declared implicitly outside any function is a global variable;
• Variables implicitly declared in functions are local variables
• Global variables are not implicitly visible in any function.
• If there is no local variable in the function with the same name as the global,
the global can be made visible by including it in a global declaration
statement.

JavaScript has no access


mechanism to implicitly
declared global variable
like PHP in a function
with same name.
Theory Of Programming Languages
Fall 2024 25
~~~ Dr. Khalid Iqbal Khattak ~~~
Dynamic Scope
• APL, SNOBOL4, and the early versions of Lisp, Perl and Common
LISP have dynamic scope.
• Spatial relationships refer to the connections and
arrangements between objects or elements in a physical space
• Dynamic scoping is based on the calling sequence of
subprograms, not on their spatial relationship to each other.
Thus, refers to execution context.
– First, big calls sub1, which calls sub2
– Search proceeds from the local procedure,
sub2, to its caller, sub1, where a declaration
for x is found.
The reference is
x declared in sub2 is called dynamic parent
to the x
sub1 directly from big of sub2 is big
declared in big
Theory Of Programming Languages
Fall 2024 26
~~~ Dr. Khalid Iqbal Khattak ~~~
Different types of lifetimes

Theory Of Programming Languages


Fall 2024 27
~~~ Dr. Khalid Iqbal Khattak ~~~
Example: Accessing variable in Outer blocks
• int main()
• {
• { int x = 10;
• }
• { // Error: x is not accessible here
• printf("%d", x);
• }
• return 0; Error

• }
prog.c:8:15: error: printf("%d", x);
'x' undeclared
In function 'main': // Error: x is not ^
(first use in this
function) accessible here

Theory Of Programming Languages


Fall 2024 28
~~~ Dr. Khalid Iqbal Khattak ~~~
Example: Accessing variable in inner blocks

Theory Of Programming Languages


Fall 2024 29
~~~ Dr. Khalid Iqbal Khattak ~~~
Example: variable of same name in inner scope

Theory Of Programming Languages


Fall 2024 30
~~~ Dr. Khalid Iqbal Khattak ~~~
Example: inner scope inherits variables from outer scope

Theory Of Programming Languages


Fall 2024 31
~~~ Dr. Khalid Iqbal Khattak ~~~
Example: accessibility of variable of in inner scope

Theory Of Programming Languages


Fall 2024 32
~~~ Dr. Khalid Iqbal Khattak ~~~
Example: Local variables in C/C++ are variables defined inside a function

Theory Of Programming Languages


Fall 2024 33
~~~ Dr. Khalid Iqbal Khattak ~~~
Fall 2024

Submission deadline: 11-11-2024

ASSIGNMENT 03

Theory Of Programming Languages


34
~~~ Dr. Khalid Iqbal Khattak ~~~
Question CLO-3
• Create/Develop a program having variables with their results
in the table or an error generated by the C++ Compiler?
• Complete the table as screen outputs of your tested program
with code & result screen [15]
Results/Variables status Function Name Declared Not Declared
Local variable
Global Variable
Scope of Variable
Lifetime of Variable
Inner variable Scope
Outer variable Scope
Inner Variable lifetime
Outer variable lifetime
Final Output
Fall 2024 Theory Of Programming Languages
35
05-Nov-24 09:31:44 AM ~~~ Dr. Khalid Iqbal Khattak ~~~
Question: guidelines on Assignment 03
• Purpose of Local Variables:
– To hold (important) values that is only needed inside the function the where the local variable is defined
• Lifetime of a local variable:
– Begins at the location where the local variable is defined
– Ends at the end of the scope in which the local variable is defined
• Scope of a local variable:
– Start at the location where the local variable is defined
– Ends at the end of the scope in which the local variable is defined
• Global variables in C/C++ are variables that are NOT defined inside any function
• Lifetime of global variables
– Global variable begins to exist in the program (space reserved) when the program begins its execution.
– Global variable ceases to exist in the program (space freed) when the program terminates its execution.
• C/C++ scoping rule for global variables
– Global variables is visible/accessible in every function except when a local variable of the same name is defined in the
given or an outer scope
• Local variables in C/C++ are variables defined inside a function
• In C/C++: a variable with the same name in an inner scope will prevent a variable with that name in an outer scope to be
accessed - this is called: shadowing

Fall 2024 Theory Of Programming Languages


36
05-Nov-24 09:31:44 AM ~~~ Dr. Khalid Iqbal Khattak ~~~
Data Types: Introduction, Primitive Data Types
A data type is an attribute associated with a piece of data that tells a computer
system how to interpret its value.

A data type refers to the type of value a variable has and what type of
mathematical, relational or logical operations can be applied without
causing an error

Fall 2024 Theory Of Programming Languages


37
05-Nov-24 09:31:44 AM ~~~ Dr. Khalid Iqbal Khattak ~~~
Data Types: Introduction, Primitive Data Types

Data Type Represents Examples


integer whole numbers -5, 0, 123
floating point (real) fractional numbers -87.5, 0.0, 3.14159
string A sequence of characters "Hello world!"
Boolean logical true or false true, false
nothing no data null

Fall 2024 Theory Of Programming Languages


38
05-Nov-24 09:31:44 AM ~~~ Dr. Khalid Iqbal Khattak ~~~
_______________________________

END

_______________________________

You might also like