C Language Learning (2) function review

Source: Internet
Author: User

Topic 1: Function 1. Why use function 1.1. Avoid the main function becomes complex, unclear, easy to read and maintain the program. 1.2. Avoid a feature to write code 2 times. Modular Programming 1.1 Objective: To simplify the process of programming. 1.2 Benefits: Once written well, later with Tiaogan. 3. Functions are functions. Each function is used to implement a specific function. The name of the function should reflect the function it represents. 4. The function declaration is to inform the compilation system of information about functions (function names, functions, number and types of function arguments) so that when the compilation system compiles the program, it is known that the function is a function rather than a variable or other object when it is called by the main function. The correctness of the calling function is also checked (such as the correct type, function name, number of arguments, parameter type, etc.). 5. A source program file is a compilation unit that is compiled at the time of program compilation in the source program file, rather than in a function unit. 6. Definition of function 7. Function of NULL function: In order to extend the function of the program. At the same time, the structure of the program is clear, readable, and now occupies a position in the structure 8. The purpose of defining a function is to call this function to get the expected result. 9. Formal parameter: Is the argument that is defined in the function parenthesis: is the value that comes from 10. Data transfer between formal parameters and arguments in the process of calling a function, the system passes the value of the argument to the parameter of the called function. Or, the formal parameter gets a value. This value is valid during a function call and can participate in the operation of the function. 11. Function definition: No semicolon, function body function declaration: There is a semicolon, no function body 12. An argument can be a constant, a variable, or an expression. The type of the real-participation parameter should be the same or copy-compatible. 13. The parameters specified in the definition function do not account for memory storage units when function calls are not present. When a function call occurs, the function's formal parameter is temporarily allocated a memory unit, which differs from the memory unit of the argument. Because the value memory cells are different, the change of all parameter values does not affect the actual parameter values. 14. The first line of a function is called a function prototype. 20115. The parameter name in a function declaration can be written in the form of a write-only parameter type. 16. If the function called in this file has been declared at the beginning of the file (before all functions), the function called in the function is not declared again. 17. Functions cannot be nested defined, but can be nested call 18. In the process of invoking a function, the function itself is called directly or indirectly, called a recursive call to a function. This is a major feature of C language. 19. A recursive problem can be divided into two stages: "Backtracking" and "recursion". 20920.n-1,1,3,2 1,3 n-1,2,1,3 theme 2: Arrays as function arguments The 1.1 array element is a function parameter, and the value of the array element is passed to the parameter. One-way delivery. Array elements can only be used as arguments. 1.2 The array name is a function parameter, and the address of the first element of the array is passed to the parameter. The array name can be either an argument or a formal parameter. 1.3 Example 1.3.1. Declaring a function in the main function 1:float average (float array[10]);                  &N BSP; Call statement: average (score);//array name of the defined array                     function 1 definition: float Average (float array[10]);//You can also do not specify an array size, write only one square bracket 13.2. In fact, the parameter group name is processed as a pointer variable at compile time to receive an address. 13.3. When the array length is required to control the number of loops, the actual length of the array can be passed to a parameter in the parameter list. 14. Multidimensional array famous function parameters, the size of one dimension of formal parameters can not be qualified, but the size of the second dimension to be specified, and the real parameter group of the 2nd dimension. An argument is an array name. 15. Local variables: Compound statement (sub-program) within the variable, outside the invalid, out of this range, the system will take its occupied memory unit released. 16. The valid range of all variables starts from the position where the variable is defined to the end of the source file. The function is to increase the channel of data connection between functions. The 1th letter of the global variable name in 17.C programming is capitalized, but not specified. 18. Disadvantages of global variables 18.1. Consumes storage unit 18.2 during all execution. Reduces the reliability and versatility of the function, because when the function is porting another file, all the variables are moved, and the problem occurs when the global variable has the same name as the variable of the other file. 18.3. In the program design, the module is required to be cohesive when the module is divided, and the coupling of other modules is weak. That is, do not put many unrelated functions into one module, and the interaction with other modules as little as possible. 18.4. Using too many global variables makes it impossible for people to clearly determine the value of the variable at each point in time. Reduce the clarity of the program. 19. In the C language, each variable and function has two properties: the data type and the storage class of the data. Storage categories refer to how data is stored in memory (such as static storage and dynamic storage). 20. Storage category for local variables 20.1. Auto variable auto does not write is the default auto dynamic store 20.2. Static variables, after the function call ends, it does not release and retains the current value. Static storage 21. Explanation 21.1. Static local variables are assigned the initial value at compile time, that is, the initial value is assigned only once, and it has an initial value when the program is running. If the definition is not assigned an initial value, the system assigns 0 or '. 21.2 for automatic variables, the system automatically assigns values that are indeterminate. Because the content of the storage unit that is actually assigned to it is not known. For example, the previous variable was used but the content was not emptied. 21.3. Static local variables can only be referenced by this function and cannot be referenced by other functions. 21.4. Static local variables can consume memory for a long time. 22. Register variable (register variable) the value of the local variable can be stored in the register in the CPU, improve the efficiency of execution, applicable to the case of frequent application of the variable. 23. Storage type for global variables    topic 3: Storage type for global variables. 1.1. The function is used to extend the scope of the external variable 2. Global variables are stored in the static storage area. So their lifetimes are fixed and exist throughout the process. 3. Extend the scope of external variables within a file. 3.1. Situation: If an external variable is not defined at the beginning of the file, its valid scope is limited to the end of the file. A function that precedes the definition point cannot reference the external variable. 3.2. Resolution: Use the keyword extern3.3. Example: Write the extern int a,b,c before the point is defined, and you can use it before defining the point. 4. Extend the scope of an external variable to another file 4.1. Scenario: If a program consists of multiple source program files, you want to refer to an external variable 4.2 defined in another file in one file. Workaround: Define an external variable a in either file, and in another file declare the variable with extern: extern A; the scope of a is then extended to this file. 4.3. Question: How to compile and run a program that includes multiple files. 5. How the system distinguishes between 3.1 and 3.2 a: when encountering extern at compile time, the definition of external variables is found in this file, and if found, the scope is extended in this file, and if not found, the definition of external variables is found in other files at the time of connection. If you find it from another file, extend the scope to this file; If you can't find it again, press error 6. Limit the scope of external variables to this file. 6.1. Situation: In the program design, you want some external variables to be referenced only by this file, not by other files. 6.2. Workaround: Add a static declaration when defining an external variable. 6.3. Use: Declared in this document: static int A; then another file is in extern A; An error will occur. This is called: static external variables. 6.4. Meaning: 1) Avoid being misused by other documents 2) this is equivalent to putting the external variables of this file on the outside "screen"Up, from the perspective of other files, this static external variable is" invisible, cannot be referenced. " 6.5. Note: Storage category keywords cannot be used alone 7. Why are local variables and global variables discussed? 7.1. The nature of the problem is the scope of the variable, then why it leads to scope 7.2. Because of the question: Can a variable defined in a function be referenced in another function? What is the range of variables defined in different locations??? And this involves the scope of the variable. 8. Storage Category Summary 8.1. The definition of a data: To use the relevant keywords to specify the data type and storage type of the two properties. Declares an external variable that has been defined with extern. 9. Three angles of the variable: 1) the scope [space]2] lifetime [Time]3] The location of the storage. where 1 and 2 are the two attributes of the variable, there is a connection, but the meaning is different. 10. Declaration and definition of variables 10.1. The essence is the declaration, the definition is a special declaration, special in the establishment of storage space. 10.2. Only for the sake of narrative: There are statements and definitions that seem different. 1) The Declaration of the establishment of the storage space is defined, not established, called the Declaration. 2) A definition declaration and a citation statement; 10.3. A declaration, whose function is to declare that the variable is an external variable already defined elsewhere, is simply a "declaration" that extends the scope of the variable.  Topic 4: Intrinsic and external functions1.1. Introduction: Out of the same analysis as the variable. The function is distinguished as an intrinsic and an external function, depending on whether the function can be called by another source file. 2. Intrinsic function This file 2.1. Definition: Static type name Function name (formal parameter list); only 3. External functions other files 3.1. First part of the function: extern int fun (int A, INTB) represents an external function defined in other files 3.2.C language provisions, do not write extern default is an external function 233Topic 5: Declaring a function with extern allows you to invoke functions defined in other files in this file, or extend the scope of the function to this file. 2. The first line of the function is called the prototype of the function. The form of the extern declaration is to add the keyword EXTERN3 on the basis of the function prototype. The nature of the function is external, so extern can be omitted. 4. From 2, 3 know that with a function prototype you can extend the scope of a function beyond the file that defines the function (not using extern). The function prototype informs the compilation system that the function is defined later in this file, or in another file. 5. #include指令的应用就是利用函数原型扩展函数作用域. Because it specifies a header file that contains prototypes of related functions such as input and output, and other related information.

C Language Learning (2) function review

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.