Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Make a digram showing the lifetimes of the local variables in this program. void

ID: 3813137 • Letter: M

Question

Make a digram showing the lifetimes of the local variables in this program.

void main() {

                   int f;

                   f = factorial(3);

}

int factorial ( int n ) {

                  int p, i;

                  p = 1;

                  for ( i = 2; i < n; i++)

                               p *= i;

                  return p;

}

b) Repeat with above version of the factorial function replaced by following recursive version:

int factorial (int n) {

                 if ( n > 1)

                      return n * factorial (n-1);

               else return 1;

}

Explanation / Answer

The lifetime of local variables are local to that function only.once the execution comes out of that function the variable got destroyed.

Let see the below table.

Here in this f is initialised with main function

Then it will be there in memory for the next statement also

But once got out from main function it will be destroyed

B) like wise for b also in this recursive function only n variable will get intialised and declared but after that function it will be destroyed.

Here in this f is initialised with main function

Then it will be there in memory for the next statement also

But once got out from main function it will be destroyed

Then in thr factorial functiin n,p,and i got initialised those will be in memory upto that factorial function after that these all will be destroyed.
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote