So any recursive function works from last call to the first? Because i had something similar but in a "for" cicle but had to do it like with -1 and you do it with +1
That's a really good question. In some sense yes, ultimately for recursion to work the "later" function calls need to return to the "earlier" function calls. But we can have some pretty complex recursion where we have a tree of recursive function calls, and in that sense there isn't always a clear last and first even though there is an order (but it's an order that resembles a tree). And in the same way a loop can work on some problems from front to back or back to front, recursion could do the same even if the function calls have a certain order. So I could see this working with either +1 or -1, if for example we start at the null terminator and somehow work our way back.
shouldn't this recursion function stop at null character? because then we don't pass the conditional statement so how are we printing the characters and going back to s[0]? how?
I left this answer on a similar question below just yesterday, maybe this will help explain things for you as well? :-) It's because we call the function first and then after print the character: print_reverse(s + 1); printf("%c", *s); I'll try to illustrate what's gong on with pseudocode, so if we have print_reverse("abc") then we get: print_reverse("bc") (essentially....) print("a") but then print_reverse("bc") also does the same thing as the above, and so we can think of it as being replaced with the first two lines below: print_reverse("c") print("b") print("a") then print_reverse("c") will be called and we'll have: print("c") print("b") print("a") and recursion will stop because we reach the end of the string, at that point the above prints will occur in order and we'll get "cba".
void _puts_recursion(char *s) { if (*s != '\0') { _putchar(*s); s ++; _puts_recursion(s); } } in this function, where do i place ' ' , so that it doesnt recursively have a next line after each character
Hmm, I'm not sure we can put a ' ' somewhere and not have it show up with each recursive call. Maybe you could try having an else case though? ... else _putchar(' '); That might have the only appear when *s does equal '\0', i.e. the end of the string?
Great question Joseph! :-) It's because we call the function *first* and then after print the character: print_reverse(s + 1); printf("%c", *s); I'll try to illustrate what's gong on with pseudocode, so if we have print_reverse("abc") then we get: print_reverse("bc") (essentially....) print("a") but then print_reverse("bc") also does the same thing as the above, and so we can think of it as being replaced with the first two lines below: print_reverse("c") print("b") print("a") then print_reverse("c") will be called and we'll have: print("c") print("b") print("a") and recursion will stop because we reach the end of the string, at that point the above prints will occur in order and we'll get "cba".
Good question Jacob, I suspect you could do something like this in the function body... if (*s != '\0') { print_reverse(s + 1); printf("%c", *s); } else printf(" ");
@@PortfolioCourses thanks for the reply, that's what I thought as well but the output puts the new line before the text, i guess im not exactly sure what this is doing.
Probably, especially if we were willing to add some parameters. If we are just printing the string I'm not sure why we would want to return a pointer though, and what the pointer would point to. We could make it point to the last character or something though, most likely.
Thanks. I understood more about recursion through your short example than a 2 hours lecture that I had recently....
Thank you for the positive feedback Francisco! I'm really glad to hear the video helped you understand recursion better. 😀
This channel is excellent!
Thank you
You're welcome Momoh! 😀
So any recursive function works from last call to the first? Because i had something similar but in a "for" cicle but had to do it like with -1 and you do it with +1
That's a really good question. In some sense yes, ultimately for recursion to work the "later" function calls need to return to the "earlier" function calls. But we can have some pretty complex recursion where we have a tree of recursive function calls, and in that sense there isn't always a clear last and first even though there is an order (but it's an order that resembles a tree). And in the same way a loop can work on some problems from front to back or back to front, recursion could do the same even if the function calls have a certain order. So I could see this working with either +1 or -1, if for example we start at the null terminator and somehow work our way back.
Thank you for the clear explanation! I finally understand now!
You're welcome! :-) I'm so glad to hear this video helped you out!
shouldn't this recursion function stop at null character? because then we don't pass the conditional statement so how are we printing the characters and going back to s[0]? how?
I left this answer on a similar question below just yesterday, maybe this will help explain things for you as well? :-)
It's because we call the function first and then after print the character:
print_reverse(s + 1);
printf("%c", *s);
I'll try to illustrate what's gong on with pseudocode, so if we have print_reverse("abc") then we get:
print_reverse("bc") (essentially....)
print("a")
but then print_reverse("bc") also does the same thing as the above, and so we can think of it as being replaced with the first two lines below:
print_reverse("c")
print("b")
print("a")
then print_reverse("c") will be called and we'll have:
print("c")
print("b")
print("a")
and recursion will stop because we reach the end of the string, at that point the above prints will occur in order and we'll get "cba".
@@PortfolioCourses got it! thank you!
@@syedsharyarjavaid9853 You're welcome! 🙂
Y we have used underscore after print_reverse
Easy to understand !
Thank you Anshul! :-D
void _puts_recursion(char *s)
{
if (*s != '\0')
{
_putchar(*s);
s ++;
_puts_recursion(s);
}
}
in this function, where do i place '
' , so that it doesnt recursively have a next line after each character
Hmm, I'm not sure we can put a '
' somewhere and not have it show up with each recursive call. Maybe you could try having an else case though?
...
else _putchar('
');
That might have the
only appear when *s does equal '\0', i.e. the end of the string?
yeah its very easy to understand
but i have a question, i am beginner so if i make a mistake understand
does the (char *s in void) work as a pointer
never mind after going through the video again i think i understand it
Ok glad to hear that Ishimwe! :-)
why does all the function calls get unrolled and starts printing backward?
Great question Joseph! :-) It's because we call the function *first* and then after print the character:
print_reverse(s + 1);
printf("%c", *s);
I'll try to illustrate what's gong on with pseudocode, so if we have print_reverse("abc") then we get:
print_reverse("bc") (essentially....)
print("a")
but then print_reverse("bc") also does the same thing as the above, and so we can think of it as being replaced with the first two lines below:
print_reverse("c")
print("b")
print("a")
then print_reverse("c") will be called and we'll have:
print("c")
print("b")
print("a")
and recursion will stop because we reach the end of the string, at that point the above prints will occur in order and we'll get "cba".
@@PortfolioCourses great explanation. Thanks
You're welcome! :-)
what would you do if you wanted to include the
in the function itself?
Good question Jacob, I suspect you could do something like this in the function body...
if (*s != '\0')
{
print_reverse(s + 1);
printf("%c", *s);
}
else printf("
");
@@PortfolioCourses thanks for the reply, that's what I thought as well but the output puts the new line before the text, i guess im not exactly sure what this is doing.
@@jacobmarshall379 Hmm, does the string itself end with a newline? If so that would be output first.
@@jacobmarshall379 Feel free to copy/paste your code into a comment here.
@@PortfolioCourses void print_rev(char *s)
{
if (*s != '\0')
{
print_rev(s + 1);
putchar(*s);
}
else
{
putchar('
');
}
}
int main(void)
{
char *s;
s="hello there";
print_rev(s);
return(0);
}
Well explained thanks
You're welcome Gideon, I'm glad you enjoyed it! :-D
Great work
I'm glad you enjoyed it! :-)
This is the way
This is the way
It is indeed tricky,
Can you do one where you return a char* from the function. so like...
char* print_reverse(char *s)
{
}
Probably, especially if we were willing to add some parameters. If we are just printing the string I'm not sure why we would want to return a pointer though, and what the pointer would point to. We could make it point to the last character or something though, most likely.
Great!
nice vid yo
Thank you very much! :-)