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

Scope Static and Dynamic: Presented By-: Prateek Katiyar ROLL NO.-: 1683910030 Rajkiya Engineering College Kannauj

This document discusses static and dynamic scope in programming languages. It defines scope as the region of a program where a variable declaration is visible. Static scope follows lexical scoping rules, where a variable's scope is the block it is declared in. Dynamic scope determines scope by the call stack at runtime, so a variable is visible to all callers. The document provides examples in C to demonstrate each type of scoping, showing how variable values are resolved differently under static versus dynamic rules.

Uploaded by

Prateek Katiyar
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)
60 views10 pages

Scope Static and Dynamic: Presented By-: Prateek Katiyar ROLL NO.-: 1683910030 Rajkiya Engineering College Kannauj

This document discusses static and dynamic scope in programming languages. It defines scope as the region of a program where a variable declaration is visible. Static scope follows lexical scoping rules, where a variable's scope is the block it is declared in. Dynamic scope determines scope by the call stack at runtime, so a variable is visible to all callers. The document provides examples in C to demonstrate each type of scoping, showing how variable values are resolved differently under static versus dynamic rules.

Uploaded by

Prateek Katiyar
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

SCOPE

STATIC AND DYNAMIC


PRESENTED BY-: PRATEEK KATIYAR
ROLL NO.-: 1683910030
RAJKIYA ENGINEERING COLLEGE KANNAUJ
CONTENTS
WHAT IS SCOPE?
TYPES OF SCOPE
STATIC SCOPE VS DYNAMIC SCOPE
WHAT IS SCOPE?
:: The scope of a variable x is the region of the program
in which uses of x refers to its declaration.
:: One of the basic reasons of scoping is to keep variables
in different parts of program distinct from one another.
:: Since there are only a small number of short variable
names, and programmers share habits about naming
of variables (e.g., i for an array index), in any program
of moderate size the same variable name will be used
in multiple different scopes.
TYPES OF SCOPE
STATIC SCOPE

DYNAMIC SCOPE
STATIC SCOPE
:: Also called lexical scoping
:: If a variable name’s scope is a certain function, then it’s
scope is the program text of the function definition:
within that text, the variable name exists, and is bound
to it’s variable, but outside that text, the variable name
does not exist.
:: In static scoping the compiler first searches in the
current block, then in the surrounding blocks
successively and finally in the global variables.
C program to demonstrate
static scope
#include<stdio.h>
int x = 10;
int f()
{
 return x;
}
int g()
{

 int x = 20; OUTPUT:: 10


 return f();
}
 int main()
{
 printf("%d", g());
 printf("\n");
 return 0;
}
DYNAMIC SCOPE
:: In dynamic scoping, if a variable name’s scope is a
certain function, then it’s scope is the time-period
during which the function is executing.

:: While the function is running, the variable name exists,


and is bound to it’s variable, but after the function
returns the variable name does no t exist.

:: In simpler terms, in dynamic scoping the compiler first


searches the current block and then successively all the
calling functions.
C program to demonstrate dynamic scope
int x = 10;
int f()
{
 return x;
}
int g()
{
 int x = 20; OUTPUT:: 20
 return f();
}

main()
{
 printf(g());
}
STATIC VS DYNAMIC SCOPING
:: In most of the 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. We can see what variables are in the scope just by
looking at the text in the editor.

:: Dynamic scoping does not care how the code is


written, but instead how it executes. Each time a new
function is executed, a new scope is pushed onto the
stack.
THANK YOU

You might also like