Indispensable Windows Native (6), windowsnative

Source: Internet
Author: User

Indispensable Windows Native (6), windowsnative

[Download source code]


Indispensable Windows Native (6)-C language: Function



Author: webabcd


Introduction
Indispensable C language for Windows Native

  • Function



Example
CFunction. h

# Ifndef _ MYHEAD_FUNCTION _ # define _ MYHEAD_FUNCTION _ # ifdef _ cplusplus extern "C" # endif // function declaration // like this in. if the function declared in h is to be called by an external file, the external file can call char * demo_cFunction (); long recursion (int n) without further declaration ); void update (int ary [], int aryLength); // You can also write the above function declaration in the following form, such as this only parameter type, function declaration without parameter names is called function prototype // void update (int [], int); void demo_variable1 (); void demo_variable2 (); void demo_params (); // The static keyword is an internal function, which can only be used in the current file. If there are internal functions with the same name in different files, they do not interfere with static void cFunction_demo1 (); // The extern keyword is an external function, which can be called by other files. extern void cFunction_demo2 (); // if there is neither static nor extern, the default value is externvoid cFunction_demo3 (); # endif


CFunction. c

/** Function ** 1. It can be divided into global variables (external variables) from the scope perspective) and local variables * 2. From the perspective of variable survival, they can be divided into static storage mode and dynamic storage mode * argument-real parameter * parameter-parameter */# include "pch. h "# include" cFunction. h "# include" cHelper. h "// global variable (also known as external variable), scope is the entire source code int global01 = 100; // such definition is in. functions in c. c file). If you want to be called by an external file, you must declare void function0 () {;} char * demo_cFunction () before calling the external file () {// before defining a function, you can call it without declaration (in the same file). After defining a function, you must declare it before calling the function. 0 (); // The following is a function declaration (declare) with no parameter and no return value, which is the same as void demo_cFunction1 (); void function1 (void ); // The following is the function declaration int function2 (int a, int B) with a return value. // You can also write the above function declaration as follows, for example, a function declaration with only the parameter type and no parameter name is called function prototype // int demo_cFunction2 (int, int); // No parameter, function1 () for a function with no return value; // for a function with a return value, int a = 1, B = 1; // here the real parameters a and B are int x = function2 (a, B); // function recursion long y = recursion (100); // result: 5050 // demonstration of passing the array type to the Function // Previously said that ary is the first address of the array, that is, the pointer passed in the past, that is, the real parameter and the form parameter actually point to a place, which is equivalent to the reference type int ary [] = {0, 1, 2, 3, 4}; update (ary, 5); // The array ary is updated // demo of local variables and global variables demo_variable1 (); // demo of variable storage: demo_variable2 (); // function with variable parameters (Variable Parameter) demo_params (); return "view code and comment";} // No parameter, the void function1 () {// when there is no return value function, the difference between void writing and void writing is not (void is recommended in C Language)/* void fun (); // indicates that any parameter can be passed (C ++ indicates that no parameter can be passed) fun (1 ); // normal compilation (in this example, the compilation is incorrect because The C ++ compiler is used.) fun (1, 2, 3); // normal compilation (in this example, the compilation is incorrect, because the C ++ compiler is used) * // * void fun (void); // It indicates that no parameter fun (1) can be passed; // The compilation error fun (1, 2, 3); // compilation Error */} // a parameter exists. a return value function is provided to demonstrate int function2 (int a, int B) {// here, a and B are the form parameters, and the form parameters are the copies of the real parameters. // The form parameters are allocated memory units only during the call, and are released immediately after the call ends, that is to say, the parameter variable is valid only in the function, and cannot be used without the function. That is, the effect of the parameter on return a + B in the function ;} // recursive call of the demo function // return: 0 + 1 + 2 + 3 +... + nlong recursion (int n) {long result = 0; if (n> 0) {result + = n; result + = recursion (n-1);} return result;} // The parameter is an array-type demonstration (the reason why the length of the aryLength array needs to be passed, it is because the passed ary is a pointer and the length of the array to which it points cannot be calculated. Like the main function, you need to pass the number of parameters) void update (int ary [], int aryLength) {// here the real parameters and form parameters actually point to a place for (int I = 0; I <aryLength; I ++) {ary [I] = 0 ;}} // demonstration of local variables and global variables void demo_variable1 () {// I am a local variable and the scope is int I = 1 in the function; {// I am a local variable in a composite statement, and the scope is within the composite statement. I cannot duplicate the name int j = 1;} // I am a local variable, if there is a global variable with the same name as me, the global variable int global01 = 200 is "blocked"; // The global variable above is "blocked". How can I reference the global variable? As shown in the following example, use ":" int global01Outside =: global01;} // variable storage mode to demonstrate void demo_variable2 () {// to score the lifetime of the variable, it can be divided into static storage mode and dynamic storage mode // 1. static storage mode: it refers to the way to allocate a fixed storage space while the program is running // 2. Dynamic Storage Mode: is the way to dynamically allocate storage space as needed during the program running // global variables will be allocated a fixed storage space (static storage) during the program running) // The local variable is auto by default, that is, the storage space is dynamically allocated. The following statement indicates auto int I = 0; (dynamic storage mode) int I = 0; // if you want the value of the local variable in the function to not disappear after the function call, you should use static to specify the local variable as "static local variable" (static storage mode) static int j = 0; // use register to specify "register variable ", it will be stored in the registers in the cpu. An Optimal scenario is to use it as a variable for loop control. // The register variable has no address, so you cannot use "&" to get its address (C ++ can get its address, but the compiler automatically changes it to a memory variable ). In addition, if the register is not enough, the memory variable register int x = 0 will be automatically changed; // use volatile to ensure that the variable must be in the memory, instead of being put into the cpu register (if the compiler considers that a variable has no external modification, it will be put into the register for optimization) volatile int y = 0; // global variables are defined outside the function. Their scope is from the definition of variables, to the end of the program file // if the function before the definition point wants to reference the global variable, use extern to indicate that the variable has been defined elsewhere, use the variable extern int global02, global03; // if there is no such statement, the compilation will report the error int p02 = global02 for the "undeclared identifier; int p03 = global03;} int global02 = 200, global03 = 300; // The following describes how to define and use functions with variable parameters (variable parameters) # include <stdarg. h> // It defines va_start, va_arg, va_end (va should be the abbreviation of variable argument (variable Parameter) void demo_params () {// calculate the average of n integers, the first parameter represents the number of subsequent parameters float average (int paramCount ,...); float result = average (4, 1, 2, 3, 4); // 2.5} float average (int paramCount ,...) {// define a parameter list va_list va_params; long sum = 0; // inform the number of indefinite parameters, and prepare to read the parameter va_start (va_params, paramCount ); for (int I = 0; I <paramCount; I ++) {// read the parameter values one by one based on the specified type, the parameter pointer moves backward to continue reading the next int value = va_arg (va_params, int); sum + = value ;}// stop reading the parameter va_end (va_params ); return sum * 1.0f/paramCount ;}



OK
[Download source code]

Related Article

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.