# Thread: Feedback on recursive function

1. ## Feedback on recursive function

The recursive function is bolded, i got feedback and was told that the static variable made the function seem a lot like a iterative function but he did not say why. Can someone explain it to me?

Code:
```#define MAX 100
#include <string.h>
#include <stdio.h>

int checkPalindrome(char string[MAX]);
int checkRecPalindrome(char string[MAX]);

int main(void){
char checkString[MAX], chr[5] = {"exit"};
do{
printf("input string to check if it is a palindrome, or input exit to end\n");
fflush(stdout);
gets(checkString);
if(strcmp(checkString, chr) != 0){
/*call copies and reverses string to copy and sends back result of strcmp*/
if(checkPalindrome(checkString) == 1)
printf("The word is a palindrome(iterative)\n");
else
printf("The word is not a palindrome(iterative)\n");
if (checkRecPalindrome(checkString))
printf("The word is a palindrome (recursive)\n");
else
printf("The word is not a palindrome(recursive)\n");
}
else
printf("Quitting...");
/*loop is continued until checkString matches chr with strcmp*/
}while(strcmp(checkString, chr) != 0);
return 0;
}
int checkPalindrome(char string[MAX]){             /*strrev could also be used for a shorter code*/
char revString[MAX];
int a, b = (strlen(string) - 1) ;
for(a = 0; a < strlen(string); ++a){
revString[b] = string[a];
--b;
}
revString[strlen(string)] = '\0';
if(strcmp(string, revString) == 0)
return 1;
else
return 0;
}
int checkRecPalindrome(char string[MAX]){
static int a = 0;
int strLgt = strlen(string) - a;
if(strLgt > a){
if(string[a] == string[strLgt - 1]){
a++;
return checkRecPalindrome(string);
}
else
return 0;
}
a = 0;
return 1;
}```

2. There are situations where a function needs to keep a static state; this is not one of them. That value would normally be passed as one of the parameters to the function. (Actually you would probably pass the "length" of the string -- the original caller would pass the actual length, and then the recursive function calls would knock two off each time.)

I don't know exactly how that makes it seem "iterative" though.

3. Originally Posted by Brolaf Broski
i got feedback and was told that the static variable made the function seem a lot like a iterative function but he did not say why. Can someone explain it to me?
Then ask the person who gave you feedback. I can guess that it feels like you're using a loop index, but the person may have a different reason.

Personally, I avoid static local variables with recursion because I find that it makes it harder to reason about the recursion since you have to consider state that persists from call to call.

Are you permitted to add a parameter to checkRecPalindrome? Are you permitted to modify the string, or at least make a copy of it and then do the recursion in another function with the copy? I ask this because given just one parameter (pointer to the first element of the string) seems insufficient for a recursive solution without the use of a static local variable (or global variable, for that matter), if the string is not to be modified and no copy can be made.

4. the assignment said to create the recursive function with 1 parameter only.

5. /*strrev could also be used for a shorter code*/
Code:
```static int pali_(const char *start, const char *end)
{
return start >= end ? 1 : *start == *end && pali_(start + 1, end - 1);
}

int pali(const char *s)
{
return *s != '\0' && pali_(s, s + strlen(s) - 1);
}```

6. Originally Posted by Brolaf Broski
the assignment said to create the recursive function with 1 parameter only.
Right. Are you then permitted to modify the string? I presume that having a recursive helper function won't do (as in Barney McGrew's example) since your instructor would then say that the recursive function has more than one parameter.

7. You can do it with one parameter and the return value

8. actually he said that we could use a helper function I had no idea what he meant. I will take a look at Barney's functions. This forum is great for a beginner!