<> function

c Language is a process oriented high-level language

open VS You can see the header file of the encapsulated library function in the installation path .h And its implementation .c file c, In fact, some library functions encapsulate system call functions , Call the library function to call the system call indirectly

Naku function strlen Let's have a chestnut ,strlen Is a function that calculates the number of characters in a string , be located string.h Header file
#include <stdio.h> #include <string.h> int main() { char str[] = "producing!";
printf("%d", strlen(str)); return 0; }
Let's simulate it strlen

Recursive implementation strlen
// Recursive implementation strlen #include <stdio.h> int strlen_self(char* ch) { if (*ch == '\0')
return 0; return 1 + strlen_self(ch + 1); } int main() { char st[10] =
"ASDFGHJKL"; printf("%d\n", strlen_self(st)); return 0; }
Non recursive implementation strlen
// Non recursive implementation strlen #include <stdio.h> int strlen_self(char* ch) { int sum = 0; while
(*ch++ != '\0') { sum++; } return sum; } int main() { char st[10] = "ASDFGHJKL";
printf("%d\n", strlen_self(st)); return 0; }
Library function is a mature and safe function which is packaged by human and used by program developers , The time cost of repeated wheel building is avoided , It improves the efficiency of program development and shortens the time cycle

<> recursion

The programming skill of program call itself is called recursion ( recursion). Recursion is an idea of divide and conquer , A large-scale series of jobs with the same operation are divided into several small jobs of the same operation to simplify the steps .
<> Two necessary conditions of recursion :
*
There are limitations , When this constraint is met , Recursion does not continue .

*
After each recursive call, it gets closer to this constraint .

Recursive simulation from above strlen Function starting of function , We analyze the process of recursion

In order to facilitate observation and tracking, understand the recursive process , We will st Initialize to string "ASD"
// Recursive implementation strlen #include <stdio.h> int strlen_self(char* ch) { if (*ch == '\0')
return 0; return 1 + strlen_self(ch + 1); } int main() { char st[4] = "ASD";
printf("%d\n", strlen_self(st)); return 0; }
We can see from the function stack frame ,strlen_self Function called in total 4 second , The process of stack frame out of stack is the process of recursive return

Keep coming out of the stack …

Until there is only one left in the stack main The frame of the function

Will be in main Function strlen_self Return value of

Technology
©2019-2020 Toolsou All rights reserved,
vue Changes in data Interface not updated Python realization js And Django Front and back interaction in the background JavaSwing To achieve a simple Lianliankan games C# Making a simplified version of calculator elementui Shuttle box el-transfer Display list content text too long JVM summary Regression of dependent variable order categories (R language )【C#】 The realization of student achievement information management system use css Design a simple style html login interface C language --------- Tetris ( source code )