1function1.1the prototype and invocation of the function
You must define or declare a function before using a function
Double Circle (double R); int main () { Double length = circle (10); printf ("Length =%f\n", length); return 0; } Double Circle (Double R) { Return 2 * 3.14 * r; } |
1.2formal participation arguments for functions
When calling a function, most of the functions have parameters, and the key function and the called function need to pass data between them.
When you define a function, the name of the variable in parentheses after the function name is "formal parameter", or simply a formal parameter. When a function is called, the variable or expression in parentheses after the function name is called an "actual parameter", or argument.
1 parameters when no function calls occur, they do not occupy memory cells, only the parameters are allocated memory when the function call occurs, and the memory occupied by the parameters is freed when the function call is completed.
2 arguments can be variables, constants, or expressions
3 when defining a function, be sure to specify the data type of the parameter
The data type of the 4-shape participation argument must be compatible
5 in C, the data passing of a real-participation parameter is a "value pass", that is, a one-way pass, which is passed only to the parameter by the argument and not to the argument by the parameter.
If the parameter of a function is an array, then the value of the argument can be modified by a formal parameter.
1.3return type of function and return value
The return value of the 1 function is obtained through return in the function, and if the return value of the function is void the return statement is not required.
The return value data type in the 2 function return statement should be the same as the function definition.
3 If there is no return statement in the function, then the function returns an indeterminate value.
1.4Main function and exit functions and functions return statement
int Test1 () { printf ("111111\n"); return 0; Exit (0);//Call exit in a child function also represents the termination of the program, but calling return in a child function only terminates the child function, and the program executes normally printf ("222222\n"); } int main () { Test1 (); printf ("aaaaaa\n"); Exit (//exit) is a C-language library function, and the result of calling exit is that the program terminates Return 100;//calling exit in the main function is the same as calling return printf ("ccccccc\n"); Return 0;//main function return represents program termination printf ("bbbbbb\n"); } |
1.5compilation of multiple source code file programs1.5. Use of 1 header files
If you put the main function in the first file and put the custom function in the second file, you need to declare the function prototype in the first file.
If you include a function prototype in a header file, you do not have to declare its prototype every time you use a function. It is good practice to put function declarations into the header file.
The significance of 1.5.2 #include与 # define
#include就是简单的文件内容替换
#define就是简单的文本替换而已
1.5.3 #ifndef与 #endif
#ifndef的意思就是条件预编译, if the conditions behind the #ifndef are true, then precompile the code from #ifndef to #endif, otherwise you will not precompile the code
1.6recursion of Functions
Functions can call themselves, which is called recursive functions.
void recurse (int i) { if (i > 0) { Recurse (i-1); } printf ("i =%d\n", i); } int main () { Recurse (10); return 0; } |
The process analysis of 1.6.1 recursion
void Up_down (int n) { printf ("In%d, location%p\n", N, &n); if (n < 4) Up_down ((n + 1)); printf ("Out of%d, location%p\n", N, &n); } int main () { Up_down (1); return 0; } |
There were n individuals lined up to ask the nth man how old he was, and he answered 2 years older than the previous one, and then asked how old the previous one was, and he answered a 2-year-old man, who had asked the last question to ask the first person, and he answered the 10-year-old
int age (int n) { int i; if (n = = 1) i = 10; Else i = Age (n-1) + 2; return i; } |
will be Ten example of converting a binary number into a binary number
234 in decimal 2 * 10 of 2 times Square + 3 * 10 1 Times Square + 4*10 0.
Odd binary the last one must be 1, even the last digit of binary must be 0.
You can get the last one in binary form by number% 2, if you want to convert a complete integer into binary, you need to use a recursive function.
Before a recursive call, the value of number% 2 is computed, and then the output is made after the recursive invocation of the statement, so that the first calculated value is instead the last output.
To get the next number, divide the original number by 2, which is equivalent to the decimal point to the left one bit, if the number is even, then the next binary value is 0, if the result is odd, then the next binary number is 1.
Until the result of 2 is less than 2, the recursion is stopped.
void to_binary (unsigned int n) { unsigned int i = n% 2; if (n >= 2) To_binary (N/2); printf ("%c", i + 0x30); } |
Fibonacci Sequence Examples
The Fibonacci sequence refers to a sequence of 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, ...
The No. 0 item is 0, and the 1th item is the first 1.
This sequence starts with the 2nd item and each item is equal to the sum of the first two items.
int fib (int n) { if (n = = 0) return 0; if (n = = 1) return 1; if (n > 1) return fib (n-1) + fib (n-2); } |
Advantages of 1.6.2 Recursion
Recursion provides the simplest method for some programming problems
Disadvantages of 1.6.3 recursion
A defective recursion can quickly deplete the computer's resources, and the recursive program is difficult to understand and maintain.
C-Language Basics-Fifth lesson-functions