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

How to call recursive functions in C language

2025-02-27 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)05/31 Report--

This article mainly explains "how to call recursive functions in C language". The content of the explanation in this article is simple and clear, and it is easy to learn and understand. Let's follow the editor's train of thought to study and learn how to call recursive functions in C language.

What is recursion

The programming skill in which a program calls itself is called recursion. As an algorithm, recursion is widely used in programming languages. A process or function has a method of calling itself directly or indirectly in its definition or description, which usually transforms a large and complex problem into a smaller problem similar to the original problem. The recursive strategy requires only a small number of programs to describe the repeated calculations needed in the problem-solving process, which greatly reduces the amount of code of the program. The main way of thinking about recursion is to make things small.

Two necessary conditions for recursion:

There is a constraint, and when this constraint is met, recursion does not continue.

It gets closer and closer to this constraint after each recursive call.

Int main () {printf ("hehe\ n"); main (); return 0;}

The function calls itself, printing "hehe" all the time, but the program stops for a while. This is not really recursion, it is an endless loop (two conditions for dissatisfaction with living recursion)

Recursive implementation: receives an integer value (unsigned) and prints each of its bits sequentially.

For example:

Enter: 1234

Output: 4321

Void print (unsigned int n) {if (n > 9) {print (n / 10);} printf ("% d", n% 10);} int main () {unsigned int num = 0; scanf ("% u", & num); / / Recursive-the function calls its own print (num); return 0;}

The basic implementation logic is shown in the figure

Note when writing recursive code:

There is no dead recursion, there is a jump condition, and each recursion approaches the jump condition

The level of recursion cannot be too deep (may overflow the stack)

Recursion and iteration

Find the nth Fibonacci number (can be implemented recursively or iteratively) (without considering overflow)

We know things like: 1, 1, 1, 2, 3, 5, 8, 13, 13, 21, 34. So a sequence in which the nth number is equal to the n-1 number plus the sum of the nmer 2 numbers is the Fibonacci sequence.

Recursive implementation to find the Fibonacci number, look directly at the code:

Int Fib (int n) {if (n 2) {c = a + b; a = b; b = c; n if;} return c;} int main () {int n = 0; scanf ("% d", & n); int ret = Fib (n) Printf ("% d\ n", ret); return 0;}

The calculation of loop iteration is very fast.

Tip:

Many problems are explained in iterative form simply because it is clearer than the non-recursive form.

However, the iterative implementation of many problems is often less efficient than the recursive implementation. Although the code is slightly less readable.

When a problem is so complex that it is difficult to implement iteratively, the simplicity of recursive implementation can compensate for the running overhead.

Thank you for your reading. the above is the content of "how to call recursive functions in C language". After the study of this article, I believe you have a deeper understanding of how to call recursive functions in C language. the specific use of the situation also 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