First, the function
C language programs are composed of functions, each function is responsible for the completion of a part of the function, the function will be the work of the package, for the program to call.
Second, function definition
Purpose: To encapsulate some common functions for later invocation.
Step: Determine the function name, determine the function body, call
Format: Return value type function name (formal parameter list)
{function Body}
Third, function call
Define what the function needs to be definite:
①. Take a meaningful function name
②. Determining the formal parameters of a function
③. Writing function bodies
④. Return value
Example:
int average (int num1,int num2)
{
Return (NUM1+NUM2)/2;
}
int main ()
{
Int a=1;
Int b=3;
Int C=average (A, b); Calling functions
Return 0;
}
Iv. parameters
①. Parameters: When defining a function, the arguments following the function name, such as NUM1 and num2
②. Arguments: When a function is called, the specific data of the descendant, such as A and b
③. The number of formal parameters and arguments must be equal
④. A variable with the same name as the parameter cannot be defined inside the function body
⑤. If the basic data type is a formal parameter, it is purely a value pass, and modifying the value of a function's internal parameter does not affect the value of the argument
⑥. A function can have no formal parameter, or it can have an infinite number of parameters
V. Return value of a function
(a) The function of Return:
①. Exit function
②. Returns a specific value to the caller of the function
(b) Return value use Note:
①. C is a weakly grammatical, weak type of language, very not strict
②. If the type of the return value is not written clearly, the default is int type
③. void represents no return value
④. Even if the return value type is explicitly declared, no value can be returned
⑤. C language By default, two functions with the same name are not allowed
Vi. function Use note
①. Duplicate function name not allowed by default
②. Functions are equal and cannot be nested defined
③. A function cannot be defined repeatedly, but can be declared repeatedly, as long as it is declared before the call, where it can be
④. function if the declaration is not defined, then the compilation succeeds, but the link is invalidated
Seven, multi-person collaboration
#include <.stdio.h> is a system header file.
#include <abc.txt> is equivalent to copying the content used in the Abc.txt file to the current location in plain text.
"" indicates that the file is in the same path as the. c file, and you can use a relative path or an absolute path. The left side has an absolute path, and no/is a relative path.
Note: Custom files use "" and system files use <>.
If the function definition is called after the call and no corresponding declaration is made, the compiler will make an error.
Call function, no function declaration, compiler will not error, link times wrong, do not define the function instead of error.
Link: Combine the relevant. o Target files in the project with the C language libraries to generate the executable file.
Usually:
①. The definition of a function is placed in a. c file, and the declaration of the function is placed in the. h file
②. If you have a function defined in a. c file, you only need to include the. h file that corresponds to the. c file.
③. h file is born soy sauce is copied by others to contain, compile the link without writing
Viii. Supplementary
(1) The return value of the main function is returned to the operating system, 0 is normal exit, and the other value is abnormal exit. In order to make the system log, it actually returns any value that has no effect on the program itself, just the markup
(2) The return value of the printf function is the number of characters. such as printf ("abc"), The return value is 3 if printf ("ABC man \ n"), the return value is 7, because one Chinese is 3 characters.
C Language Functions