当前位置:网站首页>Programming learning records - Lesson 7 [functions]
Programming learning records - Lesson 7 [functions]
2022-07-27 06:23:00 【Autumn mountain chariot God AE】
Function declaration
Function definition
Recursion of a function
A procedure or function has a method that directly or indirectly calls itself in its definition or description , Usually, a large and complex problem can be transformed layer by layer into a smaller problem similar to the original problem to solve , Recursion can simplify and reduce the number of program code , But it may also increase the amount of computation .
Two necessary conditions for recursion
1. Recursion requires restrictions , When the conditions are met , Recursion doesn't continue , Recursion cannot go on indefinitely , Otherwise, it will cause dead loop and stack overflow .
2. With recursive execution , Getting closer to the constraints .
recursive thinking
Break down the problem , From big to small .
Example 1: Enter an integer , Print each digit of the integer in turn .
The idea is , Divide the integer by an integer , For example, the input 1234, hold 1234 delimit 123, Print 4, And then 123 delimit 12, Print 3, hold 12 delimit 1 and 2, Print 2, Re print 1.
Realization :
void print(int n)
{
if (n > 9)
{
print(n / 10);
}
printf("%d ", n % 10);
}Example 2: Without creating temporary variables , Find the length of a string array .
First , The parameter passed to the function by string array is the pointer address of the first element of an array ,
We can control the pointer to move to the right , Until the element pointed to by the pointer is the termination symbol of the string array '\0', It means to the far right of the array .
Realization :
int strlen(char* sz)
{
if (*sz != '\0')
{
return 1 + strlen(sz + 1);
}
else return 0;
}Example 3: Reverse a string array .
To reverse the order , We can start from both sides of the array , Swap the first and last elements of the array . Then swap the rest of the string array , Until the remaining array length is less than 2 Stop when .
Realization :
void reverse_string(char* string)// Pass pointer to function parameter , The first thing passed is the pointer to the first element of the array .
{
int n = strlen(string);// According to the example 2, Find the length of the current array
char frist = *string; // Assign the first element of the current array to frist
*string = *(string + n - 1); // Put the last element of the current array first .
*(string + n - 1) = '\0'; // Temporarily reduce the length of the array
if(strlen(string)>2) // When the array length is greater than 2 when Recursion .
{
reverse_string(string + 1);
}
*(string + n - 1) = frist; Place the first element at the end of the current array .
}Example 4:
When we use recursive method to calculate Fibonacci sequence , You will find that when the number of sequences is large , The calculation will be slow or even crash , This is because when calculating Fibonacci sequence with recursive method , There are many repeated calculations , Wasted time .
Compare :
// Fibonacci sequence - recursive
int function1(int n)
{
if (n <= 2) return 1;
else return function1(n - 1) + function1(n - 2);
}
// Fibonacci sequence - Not a recursive
int function2(int n)
{
int f1 = 1;int f2 = 1;int f3 = 0;
if (n <= 2) return 1;
else
{
for (int i = 2;i < n;i++)
{
f3 = f1 + f2;
f1 = f2;
f2 = f3;
}
return f3;
}
}You can see , When using recursion , The code is more concise , But the corresponding , Will increase repeated calculations .
边栏推荐
猜你喜欢

Li Kou 236. the nearest common ancestor of binary tree

Knowledge supplement of multithreading

5g's past and present life -- a brief introduction to the development of mobile communication

Sexy prime number (acwing daily question)

Simple understanding of network principle

TF coordinate transformation

Pzk's first understanding of pointer in learning C language

Reading and writing of file content - data flow

How to choose the correct server backup method

wireshark数据包修改--IP地址修改(一)
随机推荐
Simple understanding of network principle
Automatic tracking
The problem that tqdm cannot display in a single line
C language minesweeping latest recursive expansion super detailed explanation (with source code)
wireshark数据包修改--添加或修改消息字段(二)
ROS运行管理之launch文件
遥感影像识别进展2022/5/5
wireshark图形界面抓包
数据库的索引和事务(重点)
无法启动程序,拒绝访问?
Li Kou daily question (linked list simulation)
Unity 菜单界面的简单介绍
数据库的联合查询
Linear progression for face recognition
Strategies for common locks in multithreading
通信机制比较
哈夫曼树的求法,代码实现及证明,图文解释
ULCL功能--5GC
How to choose the correct server backup method
数据库在终端的基础操作