当前位置:网站首页>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 .
边栏推荐
- Unable to start program, access denied?
- 数据库在终端的增删改查
- C language minesweeping latest recursive expansion super detailed explanation (with source code)
- Robot navigation implementation
- wireshark IP地址域名解析
- Force deduction problem solving monotonous stack
- Solve binary tree (5)
- socket 长链接
- Pzk learns string function of C language (1)
- Wireshark function introduction
猜你喜欢

IP核之ROM

Tangent space and TBN matrix

Multi threaded CAS, synchronized lock principle, JUC and deadlock

5G网络身份识别---详解5G-GUTI

The principle of hash table and the solution of hash conflict

UnityShader-高斯模糊

多坐标变换

Non photorealistic rendering (NPR) paper understanding and reproduction (unity) - stylized highlights for cartoon rendering and animation

5g network identity - detailed explanation of 5g Guti

Remote sensing image recognition - making data sets
随机推荐
ULCL功能--5GC
socket 长链接
ROS distributed communication
自动追随跟踪
wireshark图形界面抓包
如何选择正确的服务器备份方法
5G网络身份识别---详解5G-GUTI
C thread lock
Brief introduction to unity menu interface
Robot navigation implementation
ROS node name duplicate
Robot navigation
Multi threaded CAS, synchronized lock principle, JUC and deadlock
yum获取rpm软件包的三种方法
UnityShader-LowPoly
Wireshark packet modification -- IP address modification (I)
内部类的相关知识
ROS topic name setting
Dynamic planning for solving problems (4)
Reading and writing of C # file