0 votes 0 votes Output of this program - int rec(int x) { static int f; if(x == 1) return(1); else __Y__ ; return f ; } What is the value returned by rec(5) a) when Y is f = f * 1 + rec(x-1) ; b) when Y is f = f * x + rec(x-1) ; What is proper way to solve this ? Programming in C programming-in-c recursion + – Himanshu1 asked Jan 15, 2016 Himanshu1 2.8k views answer comment Share Follow See all 7 Comments See all 7 7 Comments reply Show 4 previous comments Anirudh Pandey commented Jan 16, 2016 reply Follow Share i was correct sir :( 0 votes 0 votes Himanshu1 commented Jan 16, 2016 reply Follow Share yes u r correct, :) still there is some need for reasoning, for particular behaviour.. as I have given 7 cases in above comment, and need to know why a statement is behaving in a particular manner.. 0 votes 0 votes Daggerhunt commented Dec 22, 2016 reply Follow Share @Himanshu1 sir, I can't understand this line - f = f * x + rec(x-1) ; gives 1 // not using updated value of f Can you please explain a bit more? 0 votes 0 votes Please log in or register to add a comment.
3 votes 3 votes int rec(int x) { static int f; if(x == 1) return(1); else f = f * 1 + rec(x-1) ; return f ; } in this f = f * 1 + rec(x-1) ; is equal to writing f=f+rec(x-1) so f has to be computed when rec(x-1) value is returned to function Since f is declared as static so it will be initialized to 0 so rec(5)=8 f=f+rec(4)=4+4 f=f+rec(3)=2+2 f=f+rec(2)=1+1 f=f+rec(1)=0+1 return (rec(1))=1 2) int rec(int x) { static int f; if(x == 1) return(1); else f = f * x + rec(x-1) ; return f ; } in this f = f * x + rec(x-1) ; here multiplication will be done first and then function will be called ,so returning value of function will not change the result and thus changed value of f will not reflect. Since f is declared as static so it will be initialized to 0 so rec(5)=1 f=f*x+rec(4)=0+1 f=f*x+rec(3)=0+1 f=f*x+rec(2)=0+1 f=f*x+rec(1)=0+1 return (rec(1))=1 Anirudh Pandey answered Jan 16, 2016 Anirudh Pandey comment Share Follow See all 0 reply Please log in or register to add a comment.
2 votes 2 votes My Answer is shivanisrivarshini answered Jan 15, 2016 shivanisrivarshini comment Share Follow See all 6 Comments See all 6 6 Comments reply Show 3 previous comments shivanisrivarshini commented Jan 16, 2016 reply Follow Share k fine thank u 0 votes 0 votes Anirudh Pandey commented Jan 16, 2016 reply Follow Share @ shivanisrivarshini if you write int rec(int x) { static int f; if(x == 1) return(1); else f = rec(x-1)+f * x ; return f ; } then you will get 120 bcz then your procedure is correct bcz then value of f is dependent on returning value of rec so first rec will be called and thus latter portion will go to hold and will use the changed of f when it is popped from the stack. 0 votes 0 votes shivanisrivarshini commented Jan 16, 2016 reply Follow Share but f=f+rec(x-1) here also f is not depending on rec() then cant we have as follows rec(5)=f+rec(4)=0+1=1 rec(4)=f+rec(3)=0+1=1 rec(2)=f+rec(1)=0+1=1 rec(1)=1 then why 8??? 3 votes 3 votes Please log in or register to add a comment.