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 use of C language functions?

2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

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

In this article, the editor introduces in detail "what is the use of C language function". The content is detailed, the steps are clear, and the details are handled properly. I hope this article "what is the use of C language function" can help you solve your doubts. Let's follow the editor's ideas to learn new knowledge.

1. Nested calls to functions

When defining a function, another function cannot be defined within a function, that is, it cannot be nested, but it can be nested to call a function, that is, in the process of calling a function, another function is called.

⚠️ Note:

Functions can be nested calls but not nested definitions.

Each function should exist independently outside the curly braces.

Code example:

From this diagram, you can clearly see that there is a new_line () function nested inside the three_line () function.

The current code prints three .

Second, the chain access of the function

Chained access to a function takes the return value of one function as an argument to another function.

In the current code, the strlen () function is used as an argument to the printf () print function and returns its return value of 3 to the printf () function, so the final print result is: 3.

What is the print result of the following code?

# includeint main () {printf ("% d", 43)); return 0;}

Output result:

Code analysis:

⚠️ Note: the return value of the printf () function is the number of printed characters.

When you know that the return value of the printf () function is the number of printed characters, you can make an analysis.

Third, the advantages and disadvantages of function recursion

The programming skill in which a program calls itself is called recursion. Can be called directly or indirectly, the essence is to turn a complex problem into a small-scale problem. Recursion usually requires only a small amount of code to depict multiple repeated calculations. The main way of thinking of recursion: making big things small.

necessary condition

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

It gets closer and closer to this restriction bar after each recursive call.

Working with scen

1. It can be transformed into a new problem, and the two solutions are the same, and there are regular changes in the objects to be dealt with.

two。 Non-recursion is troublesome, while recursion is simple.

3. There is a template or formula can be directly applied, there will be no obvious problems.

Details of function recursion

1. Each level of recursion has its own variable, which may have the same name, but a different value.

When called recursively, the system automatically retains the parameter variables of the current function.

Each call to the system opens up a corresponding space for the function.

two。 Each call returns a value, and at the end of the recursive execution, control is returned to the function at the next level.

After the call ends, the system releases the space opened up by the call

The program returns to the last call point and gets the parameters of the call at that level.

Each level of recursion must be returned step by step and cannot be skipped or interrupted.

3. The code before the recursive statement in the function, executed in the order of the called function

The code after recursion is executed in the opposite order to the called function.

Give examples to illustrate

Accept an integer value (unsigned) and print each bit in order. For example, input: 1234, output: 1234

Code example:

# includevoid print (unsigned int n) {if (n > 9) {print (n / 10);} printf ("% d", n% 10);} int main () {unsigned int num = 0; scanf ("% u", & num); / / print function can print every bit of the parameter number print (num); / / function recursion-call its own return 0;}

Output result:

Code analysis:

Understanding of two necessary conditions

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.

If it is recursive, two restrictions must be met.

Fourth, recursive practice

Writing functions does not allow you to create temporary variables and find the length of the string:

The title request does not allow the creation of temporary variables, the current code does not meet the requirements.

# include#includeint my_strlen (char* str) {int count = 0; / temporary variable / / printf ("% c\ n", * s); while (* str! ='\ 0') {count++; str++;} return count } int main () {/ / find the length of the string char arr [] = ""; int len = strlen (arr); printf ("% d\ n", len); return 0;}

A way of writing that meets the requirements:

# include#includeint my_strlen (char* str) {if (* str = ='\ 0') {return 0;} else {return 1 + my_strlen (str + 1);}} int main () {/ / find the length of the string char arr [] = "" / / arr is the array name, which is the address of the first element of the array int len = my_strlen (arr); printf ("% d\ n", len); return 0;}

Code analysis:

Running result:

After reading this, the article "what is the use of C language function" has been introduced. If you want to master the knowledge of this article, you still need to practice and use it yourself. If you want to know more about the article, you are welcome to follow the industry information channel.

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