Question

B) The following code is given. Determine the outputs if scoping is static and dynamic. static scope output dynamic scope out

0 0
Add a comment Improve this question Transcribed image text
Answer #1

The 2 functions function1 and function2 are defined inside main. So, the values of x relies on the main function. The function3 is defined inside function1 and function2.

The dependency of values in static scoping is related as follows :

main() function 10 function3) function2 function3

The first value of x will be printed in function1 which will be returned by function3.

function1 depends on main( ) and the value of x will be passed from main( ) to function1 as y and the value is 10.

function1 will compute the new value of x as x = y + 5 = 10 + 5 = 15 and this will be returned by function3.

The second value of x will be printed in function2 which will be returned by function3.

function2 depends on main( ) and the value of x will be passed from main( ) to function2 as y and the value is 10.

function2 will compute the new value of x as x = y + 10 = 10 + 10 = 20 and this will be returned by function3.

So, the final output using static scoping is :

15

20

The dynamic scoping chain is :

main( ) -----> function1( ) ---------> function3( ) ---------> function2( ) ----------> function3( )

The dependency of values in dynamic scoping is related as follows :

main function10) function() function2) function3

The first value of x is printed inside function1.

function1 depends on main( ) and the value of x will be passed from main( ) to function1 as y and the value is 10.

function1 will compute the new value of x as x = y + 5 = 10 + 5 = 15 and this will be returned by function3.

The second value of x is printed inside function2.

function2 will operate after function1 has completed execution and the value of x is dynamically changes to 15 by function1 and function2 is dependent on function1 and function3.

The value of x passed to function2 as y is 15 and not 10.

In function2, the new value of x is calculated as x = y + 10 = 15 + 10 = 25 and this is returned by function3.

So, the final output using dynamic scoping is :

15

25

static scope output dynamic scope output
15 15
20 25
Add a comment
Know the answer?
Add Answer to:
B) The following code is given. Determine the outputs if scoping is static and dynamic. static...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • 2. Consider the following programs (using C's syntax): #include <stdio.h> int a- 1, b-2; int foo...

    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?

  • please help me asap 1 Determine the output for the following code: #include<stdio.h> int main static...

    please help me asap 1 Determine the output for the following code: #include<stdio.h> int main static int num; fornum : ++ num: num++) -- printf("%d". num): if num ==8) break; [10] COI retum 1 b) #include<stdio.h> int main int test: fortest - 0: tests=5: test); printer test: retum

  • 6) Show the output of the following program. You mus trace the code to show how...

    6) Show the output of the following program. You mus trace the code to show how you reached the answer. (5 points) #include <stdio.h> int x 18; int y = 5; int z ·e; void my first function) void my_second_function); int main() int ys y = x; if (z) my_first functionO else my-second-function(); x++ : if (z) my_first_function(); else my_second_function(); return e; void my_first_function) x+y); is %d\n", printf("The value of x+y in my-first-function() void my_second_function) ( x=100; is %d\n", x);...

  • C Programming 1 point Given the following code, if Static() is called twice, what will be...

    C Programming 1 point Given the following code, if Static() is called twice, what will be printed to the screen from the second call? int x = 10; void Print(int x) printf("%d", x); } { void Output() { printf("%d", x); > void Static) { static int x = 0; printf("%d", x++); } Type your answer 8 1 point Given the following code, if Static() is called, what will be printed to the screen? int x = 10; void Print(int x)...

  • /* •   The following code consists of 5 parts. •   Find the errors for each part...

    /* •   The following code consists of 5 parts. •   Find the errors for each part and fix them. •   Explain the errors using comments on top of each part. */ #include <stdio.h> #include <string.h> int main( ) { ////////////////////////////////////////////////////////////////////////// //////////////        Part A. (5 points)                ////////////////// int g(void) { printf("%s", Inside function g\ n " ); int h(void) {       printf(" % s ", Inside function h\ n "); } } printf("Part A: \n "); g(); printf(" \n"); ////////////////////////////////////////////////////////////////////////// //////////////       ...

  • 22. (6 points) Determine the output for the following coe # include < stdio.h > int...

    22. (6 points) Determine the output for the following coe # include < stdio.h > int main(void) ( int z 1, total -0, y printf(d , x) ) //end-for printf("The total %d\n", total) ) //end-main

  • 10 What does the last printf in this code print to the screen? a)n-7 b)n- 0...

    10 What does the last printf in this code print to the screen? a)n-7 b)n- 0 c)n--1 d) None of the above 鬐include< stdio. h> int main) int n-0 for (n-7: n<O; n-- printf (n") printf ("n *%d", n); - return 11-What does the code print to the screen? 鬐include< stdio.h> void fun (int) int main) b) 8 d) None of the above fun (3) return void fun (int a) int x-10 while(a !_ 3 x > s} if a...

  • What will be the value of x printed by the following code (modify_x() function)? #include <stdio.h>...

    What will be the value of x printed by the following code (modify_x() function)? #include <stdio.h> void modify_x(void) int x 10; x+ 20 printf("X is : %d\n", x); int main(void) modify_x(); modify_x0: return O; First time:_ Second time: In earlier question (modify_x() function based), what would you do to have different values in the output upon execution of the function each time? HTML Editor

  • #include<stdio.h> int functionl (int x, int y); int main() int ij=2,k; for(i=1;i<=5; i++) k = function1(ij);...

    #include<stdio.h> int functionl (int x, int y); int main() int ij=2,k; for(i=1;i<=5; i++) k = function1(ij); printf("k=%d\n",k); return 0; int function] (int x, int y) int z; z=x*2+y; return z;

  • PLEASE USE VERY BASIC REGISTERS AND CODE TO DO THE FOLLOWING Objectives: -write assembly language programs...

    PLEASE USE VERY BASIC REGISTERS AND CODE TO DO THE FOLLOWING Objectives: -write assembly language programs to:             -define a recursive procedure/function and call it.             -use syscall operations to display integers and strings on the console window             -use syscall operations to read integers from the keyboard. Assignment Description: Implement a MIPS assembly language program that defines "main", and "function1" procedures. The function1 is recursive and should be defined as: function1(n) = (2*n)+9                      if n <= 5              =...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT