Whether recursion hides loops

Source: Internet
Author: User
Whether recursion hides the loop first look at the code
function reverse($str){  if(strlen($str)>0){    reverse(substr($str,1));    echo substr($str,0,1);    return;  }}reverse("abcdefg");//gfedcbc

In the above example, I think it means a loop. for example, after the real parameter abcdefg is entered in the present era, the reverse function first calculates whether the value is equal to 0. if it is equal, it starts if it is large, the number of 7 digits is greater than 0, so what should we enter first? no, it's a bit dizzy. how can we understand this? it's a mess, and it's really incredible in logic?


Reply to discussion (solution)

Recursion is a loop.
Recursive code can all be converted into cyclic code.
The difference between recursive code and circular code is:
Recursion uses the system stack to save intermediate results
Cyclically uses a self-maintained data structure to save intermediate results

Therefore, recursive code is much simpler than circular code.
Some people say that recursion is inefficient, but he is unaware of it.

However, not all loops can be implemented recursively.

What is the function of not returning values?
Try static

Recursion is a loop.
Recursive code can all be converted into cyclic code.
The difference between recursive code and circular code is:
Recursion uses the system stack to save intermediate results
Cyclically uses a self-maintained data structure to save intermediate results

Therefore, recursive code is much simpler than circular code.
Some people say that recursion is inefficient, but he is unaware of it.

However, not all loops can be implemented recursively.



You are talking about the theory. now we are playing the truth:

My understanding of this instance is as follows: what do you think is wrong !!!!!!

First, the abcdefg parameter is a seven-bit program. if the program is greater than 0, it directly enters echo substr (abcdefg0, 1);, so the returned result is a. at this point, the program ends.

Recursion is a loop.
Recursive code can all be converted into cyclic code.
The difference between recursive code and circular code is:
Recursion uses the system stack to save intermediate results
Cyclically uses a self-maintained data structure to save intermediate results

Therefore, recursive code is much simpler than circular code.
Some people say that recursion is inefficient, but he is unaware of it.

However, not all loops can be implemented recursively.


The second floor is what I want to ask. let's talk about it in detail.
The key in this function is return;
But the question is, who is returned here: reverse (substr ($ str, 1); or reverse echo substr ($ str );
I think it is the former. if it is the former, then the problem arises again. for example, if bcdefg is reversed, the program will not proceed any more. Why can we still loop? why?
Another problem is distressing. when we echo, we first came out of haha. Why did we come out from the back?

I understand a bit. In fact, many functions imply the rules for re-assigning values,
For example, substr ($ str, 0, 1 );
In fact, this is a bit of computing
$ Str is assigned to bcdefg from the original abcdefg.
This is also the knot that many beginners have never been able to solve, and sometimes it is suddenly understood by inspiration. However, there are very few tutorials here. they just talk about Loop and loop. In fact, I think they do not understand the truth, and I am further aware that, learning programming really requires a good understanding of the principles of heap, stack, and variables, which is a good condition for understanding many potential rules.

Hands-on is the first condition for learning computer technology
Change the function.

Function reverse ($ str) {echo "incoming $ str \ n"; // here if (strlen ($ str)> 0) {reverse (substr ($ str, 1); echo "\ n $ str \ n"; // here echo substr ($ str,); return ;}} reverse ("abcdefg "); // gfedcbc
Input abcdefg, input bcdefg, input cdefg, input defg, input efg, input fg, input fg, input g, input, input g, input, return gg, return fg, return efge, return defgd, return cdefgc, and return bcdefgb

Easy to understand!
This is something you cannot understand over the past few days or nights.

The above loop is like this
Reverse (substr ($ str, 1 ));
Return returns its reverse (substr ($ str, 1 ));
Simultaneously output echo substr ($ str,); it


Hands-on is the first condition for learning computer technology
Change the function.

Function reverse ($ str) {echo "incoming $ str \ n"; // here if (strlen ($ str)> 0) {reverse (substr ($ str, 1); echo "\ n $ str \ n"; // here echo substr ($ str,); return ;}} reverse ("abcdefg "); // gfedcbc
Input abcdefg, input bcdefg, input cdefg, input defg, input efg, input fg, input fg, input g, input, input g, input, return gg, return fg, return efge, return defgd, return cdefgc, and return bcdefgb

Easy to understand!
This is something you cannot understand over the past few days or nights.



Well, it's clear!

This is actually the case here,
Take two steps first. one step is to reverse the string and the other is to print the string.
Step 1: reverse (substr ($ str, 1); take abcde as an example. for the first time, abcde is substituted into the reverse (substr ($ str, 1) in the function ));, so it must be bcde, and then it is returned to the memory by return, and then the reverse function takes over the new variable value, that is, after bcde, reverse (substr ($ str, (1), calculate and obtain the cde.
And so on!
At the same time, the echo in the second step is also synchronized with the row output, so this is recursive usage.
It is estimated that this is the only example of in-depth analysis of recursion across the network.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.