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.Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.