ARM architecture and C language (Wei Dongshan) study notes (4) - function

1. What is a function? What is the difference between the main function and other functions?

1. In the C language, a function is a reusable block of code used to complete a specific task or calculation, and can accept parameters and return values. A function usually has a name and a set of parameters, and can be called multiple times in the program to achieve code reuse and modularization.

Functions have the following characteristics in C language:
functions can accept parameters, which are used to transfer data to functions.
A function can return a value, which is used to return the processing result to the caller.
Functions can be called multiple times in a program to achieve code reuse and modularization.
Functions can be used in combination with other functions to implement complex calculations and processing logic.
Functions can use local and global variables for storing and manipulating data.
In the C language, the main() function is the entry point of the program and the place where the program begins to execute. From the start of the program to the end of the program, all functions are called in the call chain of the main() function.

2. Compared with other functions, the main() function has the following characteristics:

The main() function is the entry point of the program, where the execution of the program begins.
The main() function has no parameters, or has two parameters, argc and argv, which are used to receive command line parameters.
The return value type of the main() function is int, which represents the execution result of the program. Normally, returning 0 indicates that the program is executed successfully, and returning other values ​​indicates that the program fails to execute.
There can only be one main() function, and it must be defined in the top-level scope of the program.
Other functions can be called in the function body of the main() function to implement complex calculation and processing logic.
Compared with the main() function, other functions are usually used to implement specific functions or calculation tasks, which can accept parameters and return values, and can also call other functions to implement complex calculation and processing logic. But different from the main() function, other functions are not the entry point of the program, and can only be executed if they are called in the call chain of the main() function.

2. What are the formal parameters and actual parameters of the function?

A formal parameter is a variable in a function definition, which is used to receive the parameters passed in when the function is called .
The actual parameter is the parameter passed to the function when the function is called, which can be a constant, variable, expression or return value of other functions .
When the function is called, the value of the actual parameter is copied to the corresponding formal parameter , and the value of the formal parameter is used inside the function.
Modifications to the formal parameters do not affect the value of the actual parameter, because the formal parameter is just a copy of the actual parameter.
A function can accept multiple parameters, separated by commas.
For example: insert image description here
u32 bound is the formal parameter of the function uart_init, when the main function calls this function, use: uart_init(115200), then 115200 is the actual parameter.

3. Function recursion

In C, recursion refers to the act of a function calling itself. Recursive functions are a powerful programming technique that can be used to solve many complex problems, but they need to be used carefully to avoid infinite recursion.

The implementation of a recursive function usually includes the following two parts:
Recursive end condition: Define the end condition of the recursive function. When a certain condition is met, the recursive function no longer calls itself, but returns the result.
Recursive call: Call itself in the recursive function body to decompose and process the problem. Recursive calls typically use the formal parameters and local variables of the recursive function to store and process data.

The implementation of the recursive function needs to pay attention to the following issues:
the recursive function needs to define the end condition, otherwise it will cause infinite recursion, resulting in stack overflow and other problems.
The calling process of recursive functions will occupy a large amount of stack space. If there are too many recursive layers, problems such as stack overflow may occur.
Recursive functions are usually less efficient than loop implementations, because recursive calls require additional function call overhead and stack space overhead.
Here is an example of a simple recursive function to calculate the factorial of n:

int factorial(int n) {
    
    
    if (n == 0) {
    
    
        return 1;
    } else {
    
    
        return n * factorial(n - 1);
    }
}

In the above example, the factorial() function is a recursive function that calculates the factorial of n. The recursive end condition is that n is equal to 0, and 1 is returned at this time. Otherwise, the factorial() function is called recursively, and the value of n minus 1 is passed to the recursive function. Ultimately, the recursive function returns the product of n and factorial(n-1).

For example, here is an example call to the factorial() function:

int result = factorial(5);

In the above example, the factorial() function is called and passed the integer parameter 5 as an argument. The function uses a recursive call inside, passes 5, 4, 3, 2, 1 as actual parameters to the recursive function, and calculates their product. Finally, the result 120 returned by the function is assigned to the variable result.

4. Commonly used library functions

The C language provides a large number of standard library functions for realizing various functions. These functions can be called directly without writing implementation code yourself. The following are some standard library functions commonly used in C language:

String processing functions:

1.strlen(): Calculate the length of the string

Function prototype: size_t strlen(const char *s);
Function function: Calculate the length of the string, excluding the null character '\0'.
Function parameters: s is the pointer of the string whose length is to be calculated.
Function return value: returns the length of the string.
Sample code:

char str[] = "hello world";
int len = strlen(str); // len的值为11

2.strcpy(): Copy a string to another string

Function prototype: char *strcpy(char *dest, const char *src);
Function function: copy a string to another string.
Function parameters: dest is the pointer of the target string, and src is the pointer of the source string.
Function return value: return the pointer of the target string.
Sample code:

char src[] = "hello world";
char dest[20];
strcpy(dest, src); // dest的值为"hello world"

3.strcat(): Append a string to another string

Function prototype: char *strcat(char *dest, const char *src);
Function function: append a string to another string.
Function parameters: dest is the pointer of the target string, and src is the pointer of the source string.
Function return value: return the pointer of the target string.
Sample code:

char str1[20] = "hello ";
char str2[] = "world";
strcat(str1, str2); // str1的值为"hello world"

4.strcmp(): Compare the size relationship of two strings

Function prototype: int strcmp(const char *s1, const char *s2);
Function function: compare the size relationship of two strings.
Function parameters: s1 and s2 are pointers to the strings to be compared.
Function return value: if s1 is greater than s2, return a positive integer; if s1 is less than s2, return a negative integer; if s1 is equal to s2, return 0.
Sample code:

char str1[] = "hello";
char str2[] = "world";
int result = strcmp(str1, str2); // result的值为-15

Mathematical functions:

abs(): calculates the absolute value of an integer

int x = -10;
int result = abs(x); // result的值为10

sqrt(): Calculates the square root of a floating-point number

double x = 16.0;
double result = sqrt(x); // result的值为4.0

pow(): calculates the power of a number

double x = 2.0;
double y = 3.0;
double result = pow(x, y); // result的值为8.0

rand(): generate a random number

int result = rand(); // 生成一个随机数

Input and output functions:

printf(): output formatted text
scanf(): read formatted input

int x;
scanf("%d", &x); // 从用户输入中读取一个整数并存储在变量x中
float x;
scanf("%f", &x); // 从用户输入中读取一个浮点数并存储在变量x中
char c;
scanf("%c", &c); // 从用户输入中读取一个字符并存储在变量c中
char str[20];
scanf("%s", str); // 从用户输入中读取一个字符串并存储在数组str中

Memory manipulation function:

malloc(): dynamically allocate memory space
free(): release dynamically allocated memory space
I have learned the above two functions in the second and third sections.
memcpy(): Copy a section of memory to another memory
memset(): Set a section of memory to a specified byte value
The memcpy() function is used to copy a section of memory to another memory, and requires three parameters: target memory address, The source memory address and the number of bytes to copy. The syntax is as follows:

void *memcpy(void *dest, const void *src, size_t n);

The memset() function is used to set a piece of memory to a specified byte value and requires three parameters: the target memory address, the byte value to be set, and the number of bytes to be set. The syntax is as follows:

void *memset(void *s, int c, size_t n);

Guess you like

Origin blog.csdn.net/qq_53092944/article/details/131112072