This article for the author's original, reproduced please indicate the source, thank you for your reading and sharing, I hope this article can let you have some gains.
Preface
This blog is the C language function part of the focus of content and details of the experience gained through actual combat summary refining, does not cover all the contents of the C language function, all refined content from refining and combat, reading needs to the function part of a certain basis, can be used to understand the C function of the promotion and preparation review, Understanding this article will help you solve the C function part of the topic, get only through the actual combat to deepen understanding of the experience.
We all know a little bit about the concept of "function" in mathematical sense, such as "Y=f (x)", and regardless of the specific form of F, its basic feature is "to an X, there is a Y value corresponding to". In C language, "function" is an important concept, and is the basis of modular programming.
What is a function? -The return output is processed according to the input.
function Essentials and trivia
1. Function concept
A function is a method, a function is a module that completes a particular function, the main function is the entry point of a C program, and there is only one main function.
2. Execution flow of functions
The function call must wait for the function execution to complete before performing the next step.
3. Library functions and custom functions
Library functions:
Provided by the C language system;
The user does not need to define, do not have to make type description in the program;
Simply include the header file with the function definition in front of the program;
Custom functions:
A function written by the user in the program as required;
4. Features of the function in the VS compiler
Function name in VS can be mixed in Chinese
C language parameter too many will be warned, many will ignore, the result is not guaranteed correct, C language guarantee as far as possible type consistent, the number is consistent.
The C language function does not return the returned can compile, but the result is self-esteem.
C language functions must add a declaration, add a declaration will not be wrong, without declaring there may be errors, declarations can have more than one, the definition must be declared.
The VS2013 compiler, which configures the path of the static library lib by default.
C language compilation is relatively broad, there is a library path, can be automatically positioned, do not need function declaration; C + + must have function declaration, header file, library file.
STD standard library, C language standard cross-platform.
You can use the Abort () function to handle program exceptions.
5. The return value of the function is the output of the function, the result of the function
The main function can have no return value, regardless of whether the type is int or void.
C Non-main function if it is not void, there will be a warning that if it is CPP it will be an error.
6.return later statements are no longer executed.
The return statement of the main function exits with no return,main function executing all statements.
7. Formal participation arguments for functions
When a function is called, the parameter allocates memory, creates a new variable, and stores the arguments passed. Before the function call, the formal parameter, which is the parameter in the function timing (), the value is indeterminate, the value is indeterminate, the memory is not allocated, only when the function is called, the memory is allocated to create a new variable, the value of the actual parameter is accepted, and when the function call ends, the memory occupied by the formal parameter is reclaimed. When the actual argument is a function call, the exact value passed by the function is the actual parameter, and the actual argument can be a constant, a variable, or an expression. The form participation argument memory address is not the same, occupying different memory space.
form parameters and the actual parameters of the type, will automatically complete the conversion of the data type, call the function, as far as possible type to match, otherwise there will be errors or errors.
8. Local variables and global variables
Local variables: Local variables are reclaimed after the call is completed, local variables are served for block statements, variables defined inside functions, and parameters of functions, all local variables
Global variables: Global variables do not belong to any function, can be called by any function, the lifetime of the global variable is the life of the program, the global variables will always account for the memory, and local variables are thrown out.
Global variables can be used for function communication, and local variables will mask global variables in cases where the same name exists.
C + + can be used:: Access to global variables, c. language can not. The same block statement under the variable can not be the same name, you can install a block statement.
int // global variable declaration, no initialization default is 0, if any, the default value int a=9// global variable definition, only one
Global variables, which can be called across files
If the name is the same, the local variable masks the global variable, and the inner block statement variable masks the external variable
Global variables, easily overwritten, easily read and written
The difference between a local variable without a declaration and a definition
int Add (int ,int// declare variable name can be omitted, to add a semicolon, declare to match the definition
9. Input and OUTPUT functions
// Print by character // wait for you to enter a character, the return value is the character you entered
10. function declaration and definition
The C language compiles from the top down, so if there is no definition of the function, or the declaration cannot find the function, it cannot be called.
The entity of a function can have only one, the declaration of a function, only the existence of a function, so there may be more than one.
C + + belongs to a strict programming language, and the declaration of a function must precede the call
11. function is the basis of modular programming, function solves the problem of code reuse
A function can abstract a function that is relatively independent, making it an independent entity in the program. Can be reused multiple times in the same program or other program.
12. Block statements for functions do not allow omitting
function body Internal variable cannot have the same name as parameter
Parameter passing is a one-way value pass
(void) parameter is empty
The default type of the function is int, which can be omitted
13. Copy mechanism of functions
The function is a copy in addition to the array, the copy mechanism is assigned, and the assignment is automatically type-converted. Return will also complete the type conversion
14. String input and output
%i equivalent to%d, prints signed decimal data
Gets (); // enter a string into a string variable // wait for you to enter the function of a character
The sleep () function is in the windows.h header file.
15. Order of execution of functions:
From the bottom to the upward stack, from the top down to execute.
16. Function parameter Operation Order:
Start with the argument on the right (int a,int b) to calculate the value of B before calculating the value of a
// Execution Results 6, 5
actual combat--Realization of variable parameter function
1.int type variable parameter function implementation:
#include <stdarg.h>//Standard Parameters intAddintNum,...)//... Represents a mutable parameter{ intres=0;//ResultsVa_list ARGP;//address where the store parameter startsVa_start (Argp,num);//from the first address, read the data behind Num for(intI=0; i<num;i++) {res+=va_arg (ARGP,int);//reads a data parsed by int type} va_end (ARGP); //End Read returnRes;}
2. String type variable parameter function implementation:
voidGointnum,...) {va_list argp;//address where the store parameter startsVa_start (Argp,num);//from the first address, read the data behind Num for(intI=0; i<num;i++) { Charstr[ -]; sprintf (str,"%s", Va_arg (ARGP,Char*)); System (STR); //reads a data according to char * parsing} va_end (ARGP); //End Read}
3. If the number of parameters is not known:
voidShowint (intstart,...) {va_list argp;//address where the store parameter startsVa_start (Argp,start);//from the first address, read the data behind Num intArgvalue=start;//The first step of initializing Do{printf ("\n%d", Argvalue); Argvalue=va_arg (ARGP,int);//constantly read} while(argvalue!=-1); Va_end (ARGP); //End Read}
Understanding the essentials of C language through actual combat--function Chapter