Question

Given the following program in a C-like syntax, what does F1(m) return assuming static scoping and...

Given the following program in a C-like syntax, what does F1(m) return assuming static scoping and dynamic scoping? You can assume that expressions are evaluated from left to right.

int m = 3;
int F1(int i) {
    return (i + F2(m) + m);  }
int F2(int j) {
  j++;
  return (j * m); 
}
int main() {
  int m = 5;
  F1(m); }

Explain in details how you got the answers to earn full credit. Don't just write the answer.

a- If parameters are passed by value.

b- If parameters are passed by reference.

c- If parameters are passed by value-result.

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

(1)static scoping is used
int m = 3;
//in F1() function global m(3) will be used
int F1(int i) {
return (i + F2(m) + m); }//5+12+3 = 20

//in F2() function global m(3) will be used
int F2(int j) {
j++; //3+1 = 4
return (j * m); //return 4*3 = 12
}
//Here local m(5) will be used
int main() {
int m = 5;
F1(m); }

Here F1(m) will return 20

(2)Dynamic scoping is used
int m = 3;
//in F1() function m(5) will be used
int F1(int i) {
return (i + F2(m) + m); }//5+30+5 = 40

//in F2() function global m(5) will be used
int F2(int j) {
j++; //5+1 = 6
return (j * m); //return 6*5 = 30
}
//Here local m(5) will be used
int main() {
int m = 5;
F1(m); }

Here F1(m) will return 40


a- If parameters are passed by value.
Any modifications to the formal parameter variable inside the called function or method affect only the separate storage location and will not be reflected in the actual parameter in the calling environment. Hence output will be 20(static scoping considered)

b- If parameters are passed by reference.
Any changes to the formal parameter are reflected in the actual parameter in the calling environment as formal parameter receives a reference (or pointer) to the actual data.
int m = 3;

int F1(int i) {
return (i + F2(m) + m); }//5+16+4 = 25

//in F2() will modify value of m as m is passed as reference
int F2(int j) {
j++; //3+1 = 4 also m=4
return (j * m); //return 4*4 = 16
}
//Here local m(5) will be used
int main() {
int m = 5;
F1(m); }

output = 25

c- If parameters are passed by value-result.
Just before the control is transferred back to the caller, the value of the formal parameter is transmitted back to the actual parameter.

int m = 3;

int F1(int i) {
return (i + F2(m) + m); }//5+12+4 = 21(Here m get modified)


int F2(int j) {
j++; //3+1 = 4 Here m=3(as m has not modified ,it will get modified only when control back to F1())
return (j * m); //return 4*3 = 12
}
//Here local m(5) will be used
int main() {
int m = 5;
F1(m); }

output = 21

Add a comment
Know the answer?
Add Answer to:
Given the following program in a C-like syntax, what does F1(m) return assuming static scoping and...
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
  • Consider the following program written in C syntax int a , b , c ; //...

    Consider the following program written in C syntax int a , b , c ; // first declaration void g() { print(a,b,c); } int f(int a) // parameter declaration { int b; // second declaration b = a + 1; g(); // first call { int a; // third declaration int c; // fourth declaration c = b; a = b + c; g(); // second call } g(); // third call return a + b ; } int main()...

  • For the given program determine what is printed after each write statement assuming static scoping, deep...

    For the given program determine what is printed after each write statement assuming static scoping, deep binding and parameters are passed-by-value-result. program main;    var X: integer;    procedure Q(var I: integer; function R(J: integer):integer);             var X: integer;             begin              X:=4;              write(“In Q, before call of R, I=”, I, “X=”, X);              I=R(I);              write(“In Q, after call of R, I=”, I, “X=”, X);           end;        procedure P;            var I: integer;           ...

  • Consider the following program written in C syntax:

    Question 4: Consider the following program written in C syntax:void swap(int a, int b) { int temp;temp = a; a = b;b = temp;}void main() {int value = 4, list[5] = {1, 3, 5, 7, 9}; swap(value, list[0]);swap(list[0], list[1]); swap(value, list[value]);}For each of the following parameter-passing methods, what are all of the values of the variables value and list after each of the three calls to swap?1.  Passed by value2.  Passed by reference3.  Passed by result   By valueBy referenceBy result Output 

  • 4. Consider the following C program: (2 points) int fun( int *j){ + 11; return 9;...

    4. Consider the following C program: (2 points) int fun( int *j){ + 11; return 9; } void main(){ int k 2; k + fun( &k); k What is the value of k after the assignment statement in main, assuming operands are evaluated left to right. operands are evaluated right to left. Please, also explain how you got the values in detail.

  • Consider the following code C++ like program: int i, j, arr[5]; //arr is an array starting...

    Consider the following code C++ like program: int i, j, arr[5]; //arr is an array starting at index 0 void exchange(int x, int y) { int temp:= x; x:= y; y:= temp; } main(){ for (j = 0; j < 5; j++) arr[j]:= j; i:= 1; exchange(i, arr[i+1]); output(i, arr[2]); //print i and arr[2] } What is the output of the code if both parameters in function swapping are passed by: a- value? b- reference? c- value-result?

  • Consider the following program: # include <iostream> int x = 3, y = 5; void foo(void)...

    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...

  • XCORE Question 1 Consider the following program void Elint x, int y Y = y +...

    XCORE Question 1 Consider the following program void Elint x, int y Y = y + 1 cout<<x<<"*«y << endl; void nain) 1 int i, a13): all) = 15; a 2) - 203 a13) = 25; cout <i«"" <all) <<"" << a12) << ""« a[3] << endl; cout <i<** <all) << "" << a12) <<""« a[3] << endl; What values of the variable and array A are printed with the following rules. a parameters are passed by value bi parameters...

  • Using C programming language Question 1 a) through m) Exercise #1: Write a C program that...

    Using C programming language Question 1 a) through m) Exercise #1: Write a C program that contains the following steps (make sure all variables are int). Read carefully each step as they are not only programming steps but also learning topics that explain how functions in C really work. a. Ask the user for a number between 10 and 99. Write an input validation loop to make sure it is within the prescribed range and ask again if not. b....

  • C++ CODE /* This is program project 2 on page 695. * Before you begin the...

    C++ CODE /* This is program project 2 on page 695. * Before you begin the project, please read the project description * on page 695 first. * * Author: Your Name * Version: Dates */ #include <iostream> #include <cmath> #include <cassert> using namespace std; class Fraction { public: // constructor Fraction(int a, int b); // generate a fraction which is a/b Fraction(int a); // generate a fraction which is a/1 Fraction(); // generate a fraction which is 0/1. i.e...

  • OPERATING SYSTWM Question 31 What is the output of this C program? #include #include void main()...

    OPERATING SYSTWM Question 31 What is the output of this C program? #include #include void main() int mptr, *cptr mptr = (int*)malloc(sizeof(int)); printf("%d", "mptr); cptr = (int)calloc(sizeof(int),1); printf("%d","cptr); garbage 0 000 O garbage segmentation fault Question 8 1 pts If this program "Hello" is run from the command line as "Hello 12 3" what is the output? (char '1'is ascii value 49. char '2' is ascii value 50 char'3' is ascii value 51): #include<stdio.h> int main (int argc, char*argv[]) int...

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