about recursive functions

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?
In fact these two functions are equivalent. There is no need to specify the second return in the last function.
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
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.
will never execute cout<<reverse_str[i];


why?
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];


You could write


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

Last edited on

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?
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
I think that understood... .

thank you ...
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
Topic archived. No new replies allowed.