about recursive functions

Jan 8, 2013 at 2:37pm
Hi,I had a questin about recursive functions.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
// string_reverse.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include <iostream> 
using namespace std;

void sting_reverse(char [],int,int);

int _tmain(int argc, _TCHAR* argv[])
{
	const int size=20;
	char statement[size];
	int x,i=0;
	x=size;

	cout<<"Enter the statement:";
	cin.getline(statement,size,'\n');
	x=strlen(statement);
	sting_reverse(statement, x,i);
	return 0;
}

void sting_reverse(char reverse_str [],int size,int i)
{
	
	if(i==size)
	return;
	 sting_reverse(reverse_str,size,i+1);
		cout<<reverse_str[i];

	
}







1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
// string_reverse.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include <iostream> 
using namespace std;

void sting_reverse(char [],int,int);

int _tmain(int argc, _TCHAR* argv[])
{
	const int size=20;
	char statement[size];
	int x,i=0;
	x=size;

	cout<<"Enter the statement:";
	cin.getline(statement,size,'\n');
	x=strlen(statement);
	sting_reverse(statement, x,i);
	return 0;
}

void sting_reverse(char reverse_str [],int size,int i)
{
	
	if(i==size)
	return;
	 return sting_reverse(reverse_str,size,i+1);
		cout<<reverse_str[i];

	
}





what is difference between these two function?
In other word,when should use "return" in second function call?
Jan 8, 2013 at 3:00pm
In fact these two functions are equivalent. There is no need to specify the second return in the last function.
Jan 8, 2013 at 3:06pm
So there isn't any diffrence between these two codes.Is there?
how about other recursive functions?
for example fibonacci or factorial?
Last edited on Jan 8, 2013 at 3:06pm
Jan 8, 2013 at 3:08pm
It's a void function. You don't need to use return at all. In your first example return used to end function prematurely. Your second example actually will never execute cout<<reverse_str[i]; routine, so it won't work at all.
Jan 8, 2013 at 3:13pm
will never execute cout<<reverse_str[i];


why?
Jan 8, 2013 at 3:19pm
You exit the function with the return statement

return sting_reverse(reverse_str,size,i+1);


So the next statement will not be executed.

cout<<reverse_str[i];


Jan 8, 2013 at 3:21pm
You could write


return ( void ) ( sting_reverse(reverse_str,size,i+1),
cout<<reverse_str[i] );

Last edited on Jan 8, 2013 at 3:29pm
Jan 8, 2013 at 3:27pm

You could write


return void ( sting_reverse(reverse_str,size,i+1),
cout<<reverse_str[i] );




What is difference between this code and my code???!!!
They are similar to each other.Aren't they?
Jan 8, 2013 at 3:33pm
I made in my code snip a typo that I updated already. Should be either

return ( void ) ( sting_reverse(reverse_str,size,i+1), cout<<reverse_str[i] );

or

return void ( ( sting_reverse(reverse_str,size,i+1), cout<<reverse_str[i] ) );

The difference between my code and your code is that I have one return statement while you have two statements: the first one is the return statement and the second one is the expression statement. The expression statement will not be executed because the control exits the function.





Last edited on Jan 8, 2013 at 3:34pm
Jan 8, 2013 at 3:38pm
I think that understood... .

thank you ...
Jan 8, 2013 at 3:49pm
You could write the function simpler

1
2
3
4
void string_reverse( const char *s )
{
	if ( *s ) return ( void( ( string_reverse( s + 1 ), std::cout << *s ) ) );
}


or as

1
2
3
4
void string_reverse( const char *s )
{
	if ( *s ) string_reverse( s + 1 ), std::cout << *s;
}

Last edited on Jan 8, 2013 at 3:51pm
Topic archived. No new replies allowed.