That limits the application of recursion but it still

This preview shows page 3 - 6 out of 14 pages.

That limits the application of recursion, but it still remains very wide. There are many tasks where recursive way of thinking gives simpler code, easier to maintain. The execution context and stack Now let’s examine how recursive calls work. For that we’ll look under the hood of functions. The information about the process of execution of a running function is stored in its execution context . The execution context is an internal data structure that contains details about the execution of a function: where the control flow is now, the current variables, the value of this (we don’t use it here) and few other internal details. One function call has exactly one execution context associated with it. When a function makes a nested call, the following happens: The current function is paused. The execution context associated with it is remembered in a special data structure called execution context stack . The nested call executes. After it ends, the old execution context is retrieved from the stack, and the outer function is resumed from where it stopped. Let’s see what happens during the pow(2, 3) call. pow(2, 3) In the beginning of the call pow(2, 3) the execution context will store variables: x = 2, n = 3 , the execution flow is at line 1 of the function. We can sketch it as: Context: { x: 2, n: 3, at line 1 } pow(2, 3) That’s when the function starts to execute. The condition n == 1 is false, so the flow continues into the second branch of if :
Image of page 3

Subscribe to view the full document.

function pow(x, n) { if (n == 1) { return x; } else { return x * pow(x, n - 1); } } alert( pow(2, 3) ); The variables are same, but the line changes, so the context is now: Context: { x: 2, n: 3, at line 5 } pow(2, 3) To calculate x * pow(x, n - 1) , we need to make a subcall of pow with new arguments pow(2, 2) . pow(2, 2) To do a nested call, JavaScript remembers the current execution context in the execution context stack . Here we call the same function pow , but it absolutely doesn’t matter. The process is the same for all functions: 1. The current context is “remembered” on top of the stack. 2. The new context is created for the subcall. 3. When the subcall is finished – the previous context is popped from the stack, and its execution continues. Here’s the context stack when we entered the subcall pow(2, 2) : Context: { x: 2, n: 2, at line 1 } pow(2, 2) Context: { x: 2, n: 3, at line 5 } pow(2, 3) The new current execution context is on top (and bold), and previous remembered contexts are below. When we finish the subcall – it is easy to resume the previous context, because it keeps both variables and the exact place of the code where it stopped. Here in the picture we use the word “line”, but of course it’s more precise. pow(2, 1) The process repeats: a new subcall is made at line 5 , now with arguments x=2 , n=1 .
Image of page 4
A new execution context is created, the previous one is pushed on top of the stack: Context: { x: 2, n: 1, at line 1 } pow(2, 1) Context: { x: 2, n: 2, at line 5 } pow(2, 2) Context: { x: 2, n: 3, at line 5 } pow(2, 3) There are 2 old contexts now and 1 currently running for pow(2, 1) .
Image of page 5

Subscribe to view the full document.

Image of page 6

{[ snackBarMessage ]}

What students are saying

  • Left Quote Icon

    As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

    Student Picture

    Kiran Temple University Fox School of Business ‘17, Course Hero Intern

  • Left Quote Icon

    I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern