Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What is the recursive function?

2025-03-26 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article mainly explains "what is a recursive function". The content of the explanation is simple and clear, and it is easy to learn and understand. Please follow the editor's train of thought to study and learn "what is a recursive function".

When it comes to recursive functions, we have to think of a story we heard when we were young:

Once upon a time there was a mountain, there was a temple in the mountain, there was an old monk in the temple, the old monk was telling stories to the young monks, the story was: once upon a time there was a mountain, there was a temple in the mountain, there was an old monk in the temple, and the old monk was telling stories to the young monks.

Strictly speaking, this story is not recursive, because it will be repeated all the time, there is no termination condition, we call it an endless loop, so what exactly is recursion?

Tell me another story:

A child is sitting in the 10th row. His exercise book is thrown to the first row by the group leader. What can the child do if he wants to get his exercise book back?

He can pat the children in row 9 and say, "help me with the book in row 1", while the children in row 9 can pat the children in row 8 and say "help me with the book in row 1." As this went on, the news finally reached the children in the first row, so he handed the book to the second row, and the second row to the third row. Finally, I got the book!

This is recursion. Patting children on the back can be compared to function calls, while children remember to send messages and send books because they have a memory, which can be compared to a stack.

The recursive process:

Recursion: you call yourself while you are running.

Conditions of establishment:

1. The sub-problem must be the same thing as the original problem.

two。 Each recursion is to make the problem smaller and simpler.

3. You can't call yourself indefinitely, you must have a clear termination condition.

Recursive function: if a function calls the function itself within it and satisfies the above conditions, then this function is a recursive function.

What is the function of recursion? Why do we use recursion? )

Function: recursive algorithms can solve some problems defined by recursion-generally speaking, there are some small problems in a big problem, and these small problems have the same structure as the big problem, but on a smaller scale.

The core idea of recursion is mathematical induction: in order to solve the problem p (n), we must first solve the basic case p (1), and then assume that p (nmur1) has been solved, on this basis, if p (n) can be solved, then all problems can be solved.

The following is understood more clearly by mathematical substitution. We use recursion to calculate the factorial of 6 (first forward, then backward):

F (6) = > 6 * f (5) = > 6 * (5 * f (4)) = > 6 * (5 * (4 * f (3) = > 6 * (5 * (4 * (3 * f (2) = > 6 * (5 * (3 * f (1) = > 6 * (5 * (4 * (3 * (2 * 1) = > 6 * (5 *) (4 * (3 * 2) = > 6 * (5 * (4 * 6)) = > 6 * (5 * 24) = > 6 * 120 = > 720 Thank you for your reading The above is the content of "what is a recursive function". After the study of this article, I believe you have a deeper understanding of the problem of what is a recursive function, and the specific use needs to be verified by practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report