Question

List the five implementation models of parameter passing. Consider the following closure in JavaScript: 1- function...

List the five implementation models of parameter passing.

Consider the following closure in JavaScript:

1- function makeAdder(x) {

2- return function(y) {return x + y;}

3- } . . .

4- var add1 = makeAdder(0);

5- var add2 = makeAdder(10);

6- document.write(add1(20));

7- document.write(add2(20));

Questions:

a) Show the output.

b) What is the closure subprogram in this example?

c) Explain how the variable x referenced at line 2 is bound.

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

In case of any query do comment. Please rate answer. Thanks

Answer a: 20 30

Explanation: makeAdder(x) is taking one parameter as x and return a new function which takes another parameter as y

and then return the x+y. so when on line 4 you created add1 = makeAdder(0) (which is closure in Java script), which is in turn returning a function with value of x as 0 so when called in line 6 as add1(20) i.e. x is 0 here and y is 20 here so the result would be 0 + 20 =20

in the same way when on line 5 you created add2 = = makeAdder(10) (which is closure in Java script), which is in turn returning a new function with value of x as 10 so when called in line 7 as add2(20) i.e. x is 10 here and y is 20 here so the result would be 10 + 20 =30

Answer b: add1 and add2 are both closure subprograms.

Explanation: Whenever we are using makeAdder() to create a new subprogram, it will be a closure subprogram which shares same definition.

Answer c: As I also explained in answer a, Closure is javscript construct which allows lexical scoping. It means you can access a variable from outer function inside the inner function. so basically makeAdder is returning a function with two variable one is from outer scope as x and another one from inner scope as y and returning the result as x+y

Add a comment
Know the answer?
Add Answer to:
List the five implementation models of parameter passing. Consider the following closure in JavaScript: 1- function...
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 JavaScript-like syntax: // main program var x, y, z; function...

    Consider the following program, written in JavaScript-like syntax: // main program var x, y, z; function sub1() { var a, y, z; . . . } function sub2() { var a, b, z; . . . } function sub3() { var a, x, w; . . . } Given the following calling sequences and assuming that dynamic scoping is used, what variables are visible during execution of the last subprogram activated? Include with each visible variable the name of the...

  • Consider the following JavaScript-like program: 1 function subl) 2 var x 4 function sub2) // crea...

    Consider the following JavaScript-like program: 1 function subl) 2 var x 4 function sub2) // creates a popup box alert (x) 9 function sub3) 10 var x x3 sub4 (sub2); 12 13 14 15 function sub4 (subx) 16 17 18 19 20 21 22 ub30 23 24 subl 0; var x subx ) Give the output of this program when executed using a. (2p) deep binding Solution b. (2p) shallow binding Solution C. (2p) ad-hoc binding Solution Consider the following...

  • 1. Write a statement that calls a function named showSquare, passing the value 10 as an...

    1. Write a statement that calls a function named showSquare, passing the value 10 as an argument. 2. Write the function prototype for a function called showSquare. The function should have a single parameter variable of the int data type and areturn type of void. 3. Write the function prototype for a function called showScoreswith a parameter list containing four integer variables and a return type of void. 4. Look at the following function definition: double getGrossPay(int hoursWorked, double payRate)...

  • python: def functionSolver(function: callable)->str You will be given a function as a parameter, the function you...

    python: def functionSolver(function: callable)->str You will be given a function as a parameter, the function you are given only accepts two number parameters and produces a float value. It is your job to figure out what mathematical operation the function you are given is performing by passing it many different parameters. The possible operations the function can perform are: add, subtract, multiply, and divide. The given function will only perform a single operation, it will not change after consecutive invocations....

  • [PYTHON] Objective Complete the function definitions generateRandomPoint,isIncircle, and ApproximatePI generateRandomPoi...

    [PYTHON] Objective Complete the function definitions generateRandomPoint,isIncircle, and ApproximatePI generateRandomPoint(): Returns a list (x,y) where x and y are uniformly sampled from the range [0,4 isIncircle(x,y): Returns True if p is in the circle; otherwise, False (Note: The point p is in the circle if its distance from the center of the circle is less than or equal to the radius of the circle.) ApproximatePI (n): Generates n random points within the square and returns 4 P(A) Example #1 Input:...

  • IT Review    JAVASCRIPT (Can someone please help me answer the below questions. It would be nice...

    IT Review    JAVASCRIPT (Can someone please help me answer the below questions. It would be nice if you add comments explaining what you are doing? If unable to add comments the answer would be fantastic. Thank you. Given the following function definition: function percent(x) {      return x / 100; } Which of the following statements correctly calls the percent function: percent(10); percent: 10; percent(); percent = 10; Indicate whether each of the following evaluates to true or false, given...

  • help Question 12 (20 points) Write a function named inverse that takes a single parameter, a...

    help Question 12 (20 points) Write a function named inverse that takes a single parameter, a dictionary. In this key is a student, represented by a string. The value of each key is a list of courses, each represented by a string, in which the student is enrolled. dictionary each The function inverse should compute and return a dictionary in which each key is a course and the associated value is a list of students enrolled in that course For...

  • C++ program, inventory.cpp implementation Mostly need the int load(istream&) function. Implementation: You are supp...

    C++ program, inventory.cpp implementation Mostly need the int load(istream&) function. Implementation: You are supposed to write three classes, called Item, Node and Inventory respectively Item is a plain data class with item id, name, price and quantity information accompanied by getters and setters Node is a plain linked list node class with Item pointer and next pointer (with getters/setters) Inventory is an inventory database class that provides basic linked list operations, delete load from file / formatted print functionalities. The...

  • ----JAVA IMPLEMENTATION URGENTLY NEEDED ------ //Ques 3 /** * Computes the standard Rayleigh distribution probability density...

    ----JAVA IMPLEMENTATION URGENTLY NEEDED ------ //Ques 3 /** * Computes the standard Rayleigh distribution probability density function (The mathematical function describing the standard Rayleigh distribution is given by: y = [ x / (σ)^2 ] * e ^ [ (-x^2) / (2σ^2) ] where σ =sigma is the scale variable of Rayleigh distribution. The function y is called the probability density function of the standard Rayleigh distribution. Complete the method rayleigh(double x, int sigma) that returns the value of y...

  • please help Write a simple program with Student class having STL list of Record's structure as...

    please help Write a simple program with Student class having STL list of Record's structure as a member. 1. Records of the Students are not accessible to the outside world. 2. Student shall output its standing (Freshman, Sophomore etc). 3. A Student can only be created with the name 4. A class can only be added if there is a class name and the passing grade. Driver program creates a Student, adds few classes to the Student's records, then prints...

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