Sharing Data IN Static Scope AND Dynamic Scope
Sharing Data IN Static Scope AND Dynamic Scope
IN
STATIC SCOPE
AND
DYNAMIC
SCOPE
Introduction
In programming, D a t a s h a r i n g refers to
the ability of different parts of a program to
access and modify the same data. T h i s sharing
can occur between different components,
functions, threads, or processes within a
program or between different programs.
Static Scope Overview
S tati c S c o p e (or Lexical Scope):
# i n c l u d e <stdio.h>
int a = 10;
void func1(int b )
{ printf ("%d\n", a *
b);
}
void func2() {
int a = 20;
func1(30);
}
int m a i n ( ) {
func2();
re t u r n 0;
}
OUTPUT
=300
ADVANTAGES
It is easy to reason ab out the scope of
variables in a program, because it is
determined at compile-time.
It can lead to faster execution because the
scope of variables can be determined
at compile-time, which eliminates the
need for runtime lookups.
It is less error-prone because variables can
only be accessed within their defined
scope.
DYNAMIC SCOPING
OVERVIEW
Dynamic Scope:
1.)Variables in dynamic scope are determined at
runtime.
2.)The scope of a variable is based on the
program's execution flow.
3.)Access to a variable is resolved by looking at
the call stack, which contains information about
the sequence of function calls.
4.)Some older languages, like LISP, use
dynamic scope.
EXAMPLE
# in c lu d e int a = 10;
void func1(int b) {
printf("%d\n", a * b); }
void func2() {
int a = 20;
func1(30);
}
int m ain()
{ func2();
return 0;
}
OUTPUT
=600
ADVANTAGES