0% found this document useful (0 votes)
50 views10 pages

Sharing Data IN Static Scope AND Dynamic Scope

Programming

Uploaded by

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

Sharing Data IN Static Scope AND Dynamic Scope

Programming

Uploaded by

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

SHARING DATA

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):

1)Variables in static scope are


determined at compile-time.

2)The scope of a variable is based on


its location in the source code.

3)Access to a variable is resolved by


looking at the program's structure, not its
runtime behavior.

4)Most modern programming


languages, like C, Java, and Python,
use static scope.
EXAMPLE OF STATIC
SCOPING

# 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

It is more flexible, because variables can be accessed from


any part of the program.

It can be easier to write code that is reusable, because


variables can be accessed from anywhere.
Dynamic scoping can make it easier to debug certain types
of code.
WHICH IS BETTER
In most programming languages static
scoping is dominant. This is simply
because in static scoping it’s easy to
reason about and understand just by
looking at code.
Dynamic scoping does not care about
how the code is written, but instead how
it executes.
Thanks!
P R E PA R E D B Y:
KUNAL. RAHUL
2822328. 2822450

You might also like