Answer
Here is your answer if you have any doubt please comment, i am here to help you.
Here ,The scope of a variable 'a' is the region of the program in which uses of a refers to its declaration.
Scoping is generally divided into two classes:
1) Mosy of the programming language such as c++,c and etc are supporting static scoping. Here above the code will give the following output.
11
5
Here is the explanation of each line.
#include<stdio.h>
int a=9; // global variable initilized with 9.
void g()
{
a=a+2;
printf("%d\n",a);
}
int main()
{
int a=4; //local variable inside the main initilized with 4.
g(); //calling function g(). This will print 11, because that function uses the global variable.
a=a+1; //this will increment the variable inside the main(local varibale), because local variable hides the global variable
printf("%d\n",a); //this will print the value 5
//so the output of the above program will 11 and 5
}
This is done in C. I put comment in each line.
output
Explanation:
Here the programming language support the static scoping, So, when we created a global varibale(a).It can be accessible anywhere in the program. We can update the value of that variable. But we defined the a new variable inside main with same name of the global variable(a). Inside that main block it will treated as local variable. Local variable have great priority rather than global inside the block. So any updation will take place it will not affect the global part , it only affect the localpart. (local variable hides global varaible inside the block.).
arrow indicates calling the function and returning the values.
2) It is very uncommon in modern language, but perl supports dynamic scope.It does not care about how the code is written, but care about how it executes. every time of execution of new function, a new scope is pushed onto the stack.
Here the output is similar to static scoping . 11 and 5. In dynamic scoping the compiler first searches the current block and then successively all the calling functions.
Here is the perl script for above probelm
$a = 10;
sub g
{
$a =$a+2;
print $a."\n";
}
g();
# Since local is used, a uses dynamic scoping.
local $a=4;
$a=$a+1;
print $a;
This program doesn't show the dynamic scoping, I will another example for this.
$a = 10;
sub r
{
return $a;
}
sub g
{
# dynamic scoping.
$a =$a+2;
print r()."\n"; #if it is static scope it will print value 10 but, here it will print 12
}
g();
# Since local is used, a uses dynamic scoping.
local $a=4;
$a=$a+1;
print $a;
In the above , the function checks will take only its own variable.
output
But in c/c+ it doesn't support.
any doubt please comment, i am here to help you. I used perl because , i don't know other languages which support dynamic scoping
any doubt please comment.
Thanks in advance
Consider the following pseudocode. int a = 9; //global variable void go {a = a +2;...
Consider the following program: # include <iostream> int x = 3, y = 5; void foo(void) { x = x + 2; y = y + 4; } void bar(void) { int x = 10; y = y + 3; foo( ); cout << x << endl; cout << y << endl; } void baz(void) { int y = 7; bar( ); cout << y << endl; } void main( ) { baz( ); } What output does this program...
(10pts). Consider the following: void fun1(void); void fun2(void); void fun3(void); void main() { Int a,b,c; … } void fun1(void){ Int b,c,d; … } void fun2(void){ Int c,d,e; … } void fun3(void){ Int d,e,f; … } Given the following calling sequences and assuming that dynamic scoping is used. What variables are visible during execution of the last function called? Include with each visible variable the name of the function in which it was...
Will rate, thank you. Consider the following pseudocode, assuming nested subroutines, lexical scope, and that local variables (including formal parameters) are stored in the stack. procedure main() g: integer procedure B(a: integer) x: integer procedure A(n: integer) g := n procedure R(m: integer) write_integer (x) x := x/2 -- integer division if x > 1 R(m + 1) else A (m) --body of B x := a * a R(1) --body of main B(3) write_integer(g) (a) What does this program...
problem 2 and 3 1. For each variable (a-g) describe whether the variable itself is global or local, static or not static, and initialized or uninitialized. (Hints: Global variables are always static. Local variables become static when declared with 'static'. For pointers, consider the pointer itself, not anything the pointer might point to.) 2. For each variable (a-g), describe where it is located when DoFunctionB is running. That is, for each variable, describe whether it is on the stack, in...
Consider the following code: int a:=10 //global int b:=12 //global proc F a:= a-b proc P (M:proc) int a:=2 M() proc K int b:=3 P(F) K() //main program print(a) //built in function a- what does this code print if it uses dynamic scoping and deep binding? b- what does this code print if it uses dynamic scoping and shallow binding?
2. Consider the following programs (using C's syntax): #include <stdio.h> int a- 1, b-2; int foo (int x) 1 return (x+a); void ba r () { printf("%d, %d\n",a,b); void foobar) } printf("%d, %d\n", a, b) ; int a -4; bar bfoo (b); bar int main)( int b 8; foobar printf ("%d, %d\n", a, b) ; return 0; (a) What does the program print under static scoping? (b) What does the program print under dynamic scoping?
Consider the following C++ program: #include <iostream> using namespace std; void f1(int); void f2(int); void f3(int); int main() { f1(10); return 0; } void f1(int n) { f2(n + 5); } void f2(int n) { f3(n - 2); } void f3(int n) { cout << n << endl; // LINE 1 } Just before the program statement marked with the comment "LINE 1" is executed, how many stack frames will be on the program call stack?
Consider the following codes: public class TestThread extends Thread { public static void main(String[] args) { TestThread thread = new TestThread(); } @Override public void run() { printMyName(); } private void printMyName() { System.out.println("Thread is running"); } } Test Stem / Question Choices 1: What method should you invoke to start the thread TestThread? A: start() B: run() C: No. Thread will run automatically when executed. D: TestThread is not...
In the following C program, indicate the variable type (global, local, or static local), the lifetime (when the program is run or when the function is called), and the memory allocation (from stack or from heap) for each variable (x, y, z, and w) defined in the program. int w; void fun (int x) { int y; static float z; ... } void main() { fun(w); }
programming languages in #f language QUESTION 4 Consider the following skeletal C program: void funi(void); /* prototype */ void fun2(void); /* prototype */ void fun3(void); /* prototype */ void main() { int a, b, c; void fun1(void) { int b, c, d; void fun2 (void) { int c, d, e; void fun3(void) { int d, e, f, Assuming that dynamic scoping is used, what variables are visible during execution of the last function called in following calling sequence: main calls...