In: Computer Science
PLEASE READ CAREFULLY AND EXPLAIN YOUR ANSWER IF POSSIBLE
(JAVASCRIPT ONLY)
Write out the call stack for this program if x is 5.
function factorial(x) { if (x === 0) { return 1; } return x * factorial(x-1); } console.log(factorial(x));
Use "not in function" if not in a function, and put the function name along with the arguments if in a function.
Not in function in function_name(arg, ...) ...
function factorial(x) {
if (x === 0) {
return 1;
}
return x * factorial(x-1);
}
console.log(factorial(x));
Given x = 5 so the function call would be factorial(5)
In the beginning - factorial(5) the execution context will store x:5 and the execution flow is at the first line of the function. This can be sketched as:
Context : {x : 5, at line 1} not in function |
The function execution starts, the if condition is false as x is not equal to 0 so the else part is executed. Line number 5 will be executed, variables are same but the parameter changes i.e x becomes 4 and the context is:
Context : {x : 4, at line 5} in function 5*factorial(4) |
To calculate x*factorial(x-1) a subcall is made to factorial with new argument 4 and new context is created for the subcall and the context stack after new subcall will be
Context : {x : 4, at line 5} in function factorial(4) returns 5*factorial(4) |
Context : {x : 5, at line 1} not in function |
The new current execution context is on top. Once the subcall is finished, it's context is popped out from the stack.
Again the function execution starts for the new subcall and again a call to the factorial function is made with new argument x = 3.
Context : {x : 3, at line 5} in function factorial(3) returns 4*factorial(3) |
Context : {x : 4, at line 5} in function factorial(4) returns 5*factorial(4) |
Context : {x : 5, at line 1} not in function |
After the next subcall:
Context : {x : 2, at line 5} in function factorial(2) returns 3*factorial(2) |
Context : {x : 3, at line 5} in function factorial(3) returns 4*factorial(3) |
Context : {x : 4, at line 5} in function factorial(4) returns 5*factorial(4) |
Context : {x : 5, at line 1} not in function |
After next subcall:
Context : {x : 1, at line 5} in function factorial(1) returns 2*factorial(1) |
Context : {x : 2, at line 5} in function factorial(2) returns 3*factorial(2) |
Context : {x : 3, at line 5} in function factorial(3) returns 4*factorial(3) |
Context : {x : 4, at line 5} in function factorial(4) returns 5*factorial(4) |
Context : {x : 5, at line 1} not in function |
After next subcall:
Context : {x : 0, at line 5} in function factorial(0) returns 1*factorial(0) |
Context : {x : 1, at line 5} in function factorial(1) returns 2*factorial(1) |
Context : {x : 2, at line 5} in function factorial(2) returns 3*factorial(2) |
Context : {x : 3, at line 5} in function factorial(3) returns 4*factorial(3) |
Context : {x : 4, at line 5} in function factorial(4) returns 5*factorial(4) |
Context : {x : 5, at line 1} not in function |
Now when the subcall is made, the if condition is satisfied as x=0 so the return value is 1
Context : {x : 0, at line 5} | returns 1*1= returns 1 and gets popped out of the stack |
Context : {x : 1, at line 5} | returns 2*1= returns 2 and gets popped out of the stack |
Context : {x : 2, at line 5} | returns 3*2= returns 6 and gets popped out of the stack |
Context : {x : 3, at line 5} | returns 4*6=returns 24 and gets popped out of the stack |
Context : {x : 4, at line 5} | returns 5*24=returns 120 and gets popped out of the stack |
Context : {x : 5, at line 1} | prints 120 and gets popped out of the stack |
So, the output = 120