Whether recursion hides the loop

Source: Internet
Author: User
Look at the code first
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 that understanding is a circular meaning, such as the contemporary into ABCDEFG this argument, the reverse function first calculates whether equal to 0, if so, if the big start, here 7 digits greater than 0, so the first input what, not, or a little dizzy, how to understand it, messy, Logically, it's unbelievable.


Reply to discussion (solution)

Recursion is a loop.
Recursive code can be converted into loop code
The difference between a recursive code and a loop code is that:
Recursive use of the system's stack to save intermediate results
The loop uses a data structure of its own to preserve intermediate results

So, recursive code is more concise than loop code
Some say that recursion is inefficient, that he is ignorant.

But not all loops can be implemented recursively.

What is the purpose of asking without a return value?
Try static.

Recursion is a loop.
Recursive code can be converted into loop code
The difference between a recursive code and a loop code is that:
Recursive use of the system's stack to save intermediate results
The loop uses a data structure of its own to preserve intermediate results

So, recursive code is more concise than loop code
Some say that recursion is inefficient, that he is ignorant.

But not all loops can be implemented recursively.



You're talking about theory, we're playing it now:

For this example, my understanding is this: you see where to understand the wrong HA!!!!!!

The first parameter ABCDEFG, is seven bits, so the program a look more than 0, directly into the Echo substr (abcdefg0,1), so the return is a, to this, the program ends

Recursion is a loop.
Recursive code can be converted into loop code
The difference between a recursive code and a loop code is that:
Recursive use of the system's stack to save intermediate results
The loop uses a data structure of its own to preserve intermediate results

So, recursive code is more concise than loop code
Some say that recursion is inefficient, that he is ignorant.

But not all loops can be implemented recursively.


The second floor is also what I want to ask, a piece of detail to say it.
The key in this function I think is return;
But the question comes, who is the return here: the reverse (substr ($STR, 1)), or the Echo substr ($str, 0, 1);
I think it is the former, if it is the former, then the problem comes again, such as back to the BCDEFG it, at this time the program will not go down, why can cycle it, the reason?
There is a problem, Echo, the first out is a ha, I came out of the back

I'm kind of getting it, actually, there are many functions that contain the re-assigned rules,
such as substr ($STR, 0, 1);
In fact, at this time, the calculation
$str re-assigned to BCDEFG by the original ABCDEFG
This is also a lot of beginners can not open the knot, sometimes by inspiration suddenly understand. However, there are very few tutorials to talk about, they just blindly say cycle ah cycle, inside the truth I think they also do not understand, and then deep one step of the sentiment is that learning programming really good understanding of the heap and stack and the principle of variables, which to understand a lot of unspoken rules to do well conditions.

Hands-on, is the first condition of learning computer technology
You change the function.

function reverse ($STR) {  echo "incoming $STR \ n";//Here  if (strlen ($STR) >0) {    reverse (substr ($STR, 1));    echo "\ n return $str \ n";; Here    Echo substr ($STR, 0,1);    return;}  } Reverse ("ABCDEFG");//GFEDCBC
Incoming ABCDEFG incoming BCDEFG incoming DEFG incoming EFG incoming CDEFG incoming g incoming return when GG returns when FGF returns when Efge returns DEFGD when CDEFGC returns BCDEFGB

Glance!
It's something you can't understand for days and nights.

The loop above is like this.
Reverse (substr ($STR, 1)) after the current argument, because it is greater than 0;
Then return it reverse (substr ($STR, 1));
Simultaneously outputs the Echo substr ($str, 0, 1);


Hands-on, is the first condition of learning computer technology
You change the function.

function reverse ($STR) {  echo "incoming $STR \ n";//Here  if (strlen ($STR) >0) {    reverse (substr ($STR, 1));    echo "\ n return $str \ n";; Here    Echo substr ($STR, 0,1);    return;}  } Reverse ("ABCDEFG");//GFEDCBC
Incoming ABCDEFG incoming BCDEFG incoming DEFG incoming EFG incoming CDEFG incoming g incoming return when GG returns when FGF returns when Efge returns DEFGD when CDEFGC returns BCDEFGB

Glance!
It's something you can't understand for days and nights.



Well, the enlightened!

Actually, here's the thing.
Take two steps first, one step is to reverse the string, one step is to print out the string
The first step: reverse ($str ($STR, 1)), take ABCDE as an example, the first time ABCDE generation into the function reverse (substr (substr, 1)), then it must be BCDE, then it is returned to memory by return, Then the reverse function takes over the new variable value, namely BCDE, then uses the reverse (substr ($STR, 1));
And so on
At the same time the echo of the second step also synchronizes the output, so this is the use of recursion
This is the only example of the entire network that is so deeply dissecting recursion.

  • 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.