recursion

J

jw

Hi all,i have a recursive function and in main for ex it takes 5 as a
parameter,n'll equal to 5 and than decrease.At the end it ll be 1 and
why doesnt the result equal to 1 because of this=>
if(n==1)
return 1


-----
int sum(int n){
if(n==1)
return 1;
else
return sum(n-1)+n;
}
void main(){
cout<<sum(5);
}
 
J

Josh Mcfarlane

jw said:
Hi all,i have a recursive function and in main for ex it takes 5 as a
parameter,n'll equal to 5 and than decrease.At the end it ll be 1 and
why doesnt the result equal to 1 because of this=>
if(n==1)
return 1


-----
int sum(int n){
if(n==1)
return 1;
else
return sum(n-1)+n;
}
void main(){
cout<<sum(5);
}

So output should be
sum(5-1) + 5
5 + 4 + sum(4-1) and onward
So you should end up with
5 + 4 + 3 + 2 + 1, or 15 if my math serves me correctly.

What's the problem?
 
A

Alf P. Steinbach

* jw:
Hi all,i have a recursive function and in main for ex it takes 5 as a
parameter,n'll equal to 5 and than decrease.

In a sense the parameter's value decreases, but don't take that too
literally.

Each call of the function has it's own parameter value.

At the end it ll be 1 and
why doesnt the result equal to 1 because of this=>
if(n==1)
return 1

That specifies only the result for that call of the function.

int sum(int n){
if(n==1)
return 1;
else
return sum(n-1)+n;
}
void main(){

The 'main' return type must be 'int'.

See FAQ item 29.3, "Should I use void main or int main?".
 
A

Alf P. Steinbach

* Josh Mcfarlane:
So output should be
sum(5-1) + 5
5 + 4 + sum(4-1) and onward
So you should end up with
5 + 4 + 3 + 2 + 1, or 15 if my math serves me correctly.

What's the problem?

I think the problem is you solved jw's homework problem instead of what
he literally asked for.
 
R

Ron Natalie

jw said:
Hi all,i have a recursive function and in main for ex it takes 5 as a
parameter,n'll equal to 5 and than decrease.At the end it ll be 1 and
why doesnt the result equal to 1 because of this=>
if(n==1)
return 1


-----
int sum(int n){
if(n==1)
return 1;
else
return sum(n-1)+n;
}
void main(){
cout<<sum(5);
}
Main returns int by the way.

While the sun(1) is the last one CALLED, it's not the one that
returns to main, it returns to the sum(2) call.
 
C

Cy Edmunds

jw said:
Hi all,i have a recursive function and in main for ex it takes 5 as a
parameter,n'll equal to 5 and than decrease.At the end it ll be 1 and
why doesnt the result equal to 1 because of this=>
if(n==1)
return 1


-----
int sum(int n){
if(n==1)
return 1;
else
return sum(n-1)+n;
}
void main(){
cout<<sum(5);
}

The code above is the same as:

int sum1() {
return 1;
}


int sum2() {
return sum1()+2;
}


int sum3() {
return sum2()+3;
}


int sum4() {
return sum3()+4;
}


int sum5() {
return sum4()+5;
}

int main(){ // note: the return type of main() is int, not void
cout<<sum5();
}
 
J

Josh Mcfarlane

Alf said:
I think the problem is you solved jw's homework problem instead of what
he literally asked for.

Whoops, I figured he was wondering why it didn't output 1, and figured
showing him step by step what it was doing would be easiest way to
understand.

Josh McFarlane
 
J

jw

Josh said:
Whoops, I figured he was wondering why it didn't output 1, and figured
showing him step by step what it was doing would be easiest way to
understand.

Josh McFarlane

it is not my homework i saw it in a book and wondered,and if it had
been a homework i wouldnt have asked it to the group because the best
way to learn is doing homeworks without any help.thanks to people who
helped me..
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,007
Latest member
obedient dusk

Latest Threads

Top