preface
In mathematics, we have learned many kinds of functions, such as logarithmic function, exponential function and so on. These make my brain ache when I see them. Now I still remember that my high school mathematics teacher filled the whole blackboard in high spirits, wiped it and then wrote it. I can only stare at him like a silly x below, ha ha. However, the functions in C language are very friendly. They are not as complex as those in mathematics and are very easy to use. Next, let's learn the functions in C.
What is a function?
Wikipedia definition of function: subroutine
- In computer science, subroutine (English: subroutine, procedure, function, routine, method, subroutine, callable unit) is a part of code in a large program, which is composed of one or more statement blocks. It is responsible for completing a specific task and has relative independence compared with other codes.
- Generally, there are input parameters and return values, which provide encapsulation of the process and hiding of details. These codes are usually integrated into software libraries.
- Vernacular: before, we wrote an addition function in the main function. Now, take it out and write it outside the main function alone, and it becomes an addition function.
Why is there a function?
With the development of the program, more and more functions are needed, some of which need to be reused. If the code is placed in only one function, it will become more and more complex and inconvenient for maintenance and further development. Therefore, different functions are divided into various functions, which can reduce the complexity and call a single function repeatedly without repeated writing.
Classification of functions in C language:
- Library function
- Custom function
Why library functions?
At the beginning of C language design, only syntax is designed without function. With the application of C language in development, A programmer A needs to print something, so he writes A program himself. Programmer B also needs to print A sentence, and he also writes A program himself. Then, there are many programs printed in the same project, and the code written by everyone may be different, As A result, the project code is redundant, the development efficiency is low, and it is not standard enough, because everyone writes different printing programs. For the above reasons, C language realizes some commonly used functions into functions. If you integrate the library, there will be library functions.
To sum up, the library functions commonly used in C language are:
-
IO function
-
String operation function
-
Character manipulation function
-
Memory operation function
-
Time / date function
-
Mathematical function
-
Other library functions
Next, let's learn two library functions
#include <stdio.h> #include<string.h> //strcpy(); Use of functions int main() { char arr[] = "abcdefg";//abcdefg\0 char arr2[20] = { "xxxxxxxxx"}; //Copy the string from arr to arr2. The value of ascall of \ 0 is 0 strcpy(arr2, arr); printf("%s", arr2); return 0; } // memset(); int main() { char arr [] = { "hello bit"}; memset(arr,'X', 3);//Replace the first three strings of hello //1. The memory is set in bytes //2. The content of each byte is the same value printf("%s",arr); return 0; }
Note: however, a secret that library functions must know is that when using library functions, the header file corresponding to #include must be included. Here, learn the above library functions by referring to the documents in order to master the usage of library functions.
How to learn library functions
Need to learn how to use query tools / websites:
MSDN tools
Custom function
As the name suggests, programmers define a function according to their needs. The reason why there is a user-defined function is that the library function is omnipotent. It only has common functions and cannot contain all functions. Because the times are progressing, it cannot know the needs of our times from the beginning. Everything changes dynamically, so we need to learn to customize functions, To solve different needs, white point library functions are also user-defined, and all the more important is user-defined functions. Like library functions, custom functions have function names, return value types, and function parameters. But the difference is that these are designed by ourselves. This gives programmers a lot of room to play. Function composition:
ret_type fun_name(para1, * ) { statement;//Statement item } ret_type Return type fun_name Function name para1 Function parameters
Take an example of an addition function
//get_ Design of Max function int get_max(int x, int y) //xy is a formal parameter { return (x > y) ? (x) : (y);//The maximum value obtained from the return value } int main() { int num1 = 10; int num2 = 20; int max = get_max(num1, num2);//Num1 and num2 are arguments, declaring a variable of max to receive the return value printf("max = %d\n", max); return 0; }
As an example, writing a function can exchange the contents of two integer variables.
//Bad Swap1 void Swap1(int x, int y)//The formal parameter x opens up space in the external memory to store the transmitted num1 and num2 space 3 and space 4 respectively { int tmp = 0;//Open space 5 to store 0 tmp = x; x = y; y = tmp; //This code takes tmp as the intermediate variable to exchange the values of space 3 and space 4, and does not exchange the values of space 1 and space 2 } //Correct version void Swap2(int* px, int* py)//px and py are pointer variables, which store the address, and the pointer is the address { int tmp = 0;//Open space storage 0 tmp = *px; *px = *py; *py = tmp; } int main() { int num1 = 1;//Declare num1 open space storage 1 space 1 int num2 = 2;//Declare num1 open space storage 2 Space 2 Swap1(num1, num2); printf("Swap1::num1 = %d num2 = %d\n", num1, num2); Swap2(&num1, &num2);//To address printf("Swap2::num1 = %d num2 = %d\n", num1, num2); return 0;
First look at the beauty coming to study
Parameters of function
Actual parameter (actual parameter)
The parameters actually passed to the function are called arguments.
Arguments can be constants, variables, expressions, functions, etc.
No matter what type of arguments are, they must have definite values when making a function call in order to pass these values to the formal parameters.
Formal parameter (formal parameter)
Formal parameters refer to the variables in parentheses after the function name. They are called formal parameters because they are instantiated (memory units are allocated) only when the function is called. Formal parameters are automatically destroyed when the function call is completed. Therefore, formal parameters are only valid in functions.
The parameters x, y, px and py in the above Swap1 and Swap2 functions are formal parameters. Num1 and num2 passed to Swap1 in the main function and & num1 and & num2 passed to Swap2 function are actual parameters. Here we analyze the real and formal parameters of the function:
Memory parsing
Here we can see as like as two peas, Swap1 and x have their own space, and have the same content as the real reference, when the function of the y is called. So we can simply think that the instantiation of the formal parameter is actually equivalent to a temporary copy of the argument.
Function call:
- Value passing call
- Address calling
-
The formal parameters and arguments of the value passing calling function occupy different memory blocks respectively, and the modification of the formal parameters will not affect the arguments.
-
Address calling address calling is a way to call a function by passing the memory address of the variable created outside the function to the function parameters. This parameter transfer method can establish a real relationship between the function and the variables outside the function, that is, the variables outside the function can be directly operated inside the function. (example of exchanging two variables)
. nested calls and chained access to functions
Functions and functions can be combined according to actual needs, that is, they call each other.
Nested Call
#include <stdio.h> void new_line() { printf("hehe\n"); } void three_line() { int i = 0; for(i=0; i<3; i++) { new_line(); } } int main() { three_line(); return 0; }
Functions can be called nested, but cannot be defined nested.
Chain access
Chained access takes the return value of one function as an argument to another function
int main() { char arr[20] = "hello"; int ret = strlen(strcat(arr,"bit"));//Here is the strlen function printf("%d\n", ret); return 0; } #include <stdio.h> int main() { printf("%d", printf("%d", printf("%d", 43))); //What's the result? //Note: the return value of printf function is the number of characters printed on the screen return 0; }
Declaration and definition of functions
Function declaration:
- Tell the compiler what a function is called, what the parameters are and what the return type is. But whether it exists or not depends on the function declaration.
- The declaration of a function usually appears before the use of the function. To meet the requirements of declaration before use.
- The declaration of the function is usually placed in the header file.
Function definition:
The definition of function refers to the specific implementation of the function and explains the function implementation of the function.
test. Declaration of the content placement function of H
int Add(int x, int y);
test. Implementation of content placement function in C
int Add(int x, int y) { return x + y; }
Demo.c call function
#include "Add.h" / / reference header file int main() { int num1 = 0; int num2; scanf("%d %d", &num1, &num2); int sum = Add(num1, num2); printf("%d", sum); return 0; }
You can also write directly to a file
int Add(int x, int y) { return x + y; }//The Add function needs to be written in front, otherwise it must be declared first int main() { int num1 = 0; int num2; scanf("%d %d", &num1, &num2); int sum = Add(num1, num2); printf("%d", sum); return 0; }
It is suggested to write in separate documents
Function recursion
What is recursion? The programming skill of the program calling itself is called recursion. As an algorithm, recursion is widely used in programming languages. A process or function has a method of directly or indirectly calling itself in its definition or description. It usually transforms a large and complex problem layer by layer into a smaller problem similar to the original problem to solve. The recursive strategy can describe the multiple repeated calculations required in the problem-solving process with only a small number of programs, which greatly reduces the amount of code of the program. The main way of thinking about recursion is to make big things small
Two necessary conditions of recursion
-
There is a constraint. When this constraint is met, recursion will not continue.
-
After each recursive call, it gets closer and closer to this limit.
The simplest recursion in history
int main() { printf("You are a pig!\n"); main(); return 0; }//Dead recursion, dead loop, stack overflow
The recursive implementation accepts an integer value (unsigned) and prints each bit of it in order.
void print(int n) { if (n > 9) { print(n / 10);//Call yourself repeatedly } printf("%d ", n % 10); } int main() { int num = 0; scanf("%d", &num);//1234 print(num); return 0; }
graphic
Recursion: call yourself (recursion), call the function to return, and return to where you call it (regression)
Concluding remarks
Sometimes you may have to live for a specific thing in order to overcome everything else in your life Frederick Buckman Bear Town 2