Indispensable Windows Native (6)-C language: Functions

Source: Internet
Author: User
Tags function prototype

[SOURCE DOWNLOAD]


Indispensable Windows Native (6)-C language: Functions



Webabcd


Introduced
Essential C language of Windows Native

    • Function



Example
CFunction.h

#ifndef _myhead_function_#define_myhead_function_#ifdef __cplusplusextern "C"#endif  //function Declaration//like this function declared in. h, if you want to be called by an external file, the external file can be called without any further declaration.Char*demo_cfunction ();LongRecursion (intn);voidUpdateintAry[],intarylength);//the above function declaration can also be written in this form, like this only parameter type, no parameter name function declaration called function prototype//void Update (int[], int);voiddemo_variable1 ();voiddemo_variable2 ();voiddemo_params ();//plus the static keyword is an intrinsic function that can only be used in the current file, and if there are intrinsic functions of the same name in different files, they don't interfere with each other .Static voidcfunction_demo1 ();//plus the extern keyword is an external function that can be called by another fileextern voidCfunction_demo2 ();//If there is no static or extern, the default is externvoidCfunction_demo3 ();#endif


Cfunction.c

/** Function * * * 1, from the scope of the variable (scope) Point of view, can be divided into global variables (external variables) and local variables * 2, from the variable lifetime angle to divide, can be divided into static storage mode and dynamic storage method * * * argument-argument * parameter -Formal parameters*/#include"pch.h"#include"CFunction.h"#include"CHelper.h"//Global Variables (also known as external variables), scope (scope) is the entire source programintGLOBAL01 = -;//functions like this defined in. c (no function declarations in. c files), if you want to be called by an external file, the external file needs to be declared before it is called.voidFunction0 () {;}Char*demo_cfunction () {//before a function is defined, it can be called without a declaration (the same file); After a function definition, it must be declared before it can be calledFunction0 (); //The following is a function declaration (declare) with no parameter and no return value, which is the same as void Demo_cfunction1 ();    voidFunction1 (void); //The following is a function declaration with a parameter with a return value    intFunction2 (intAintb); //the above function declaration can also be written in this form, like this only parameter type, no parameter name function declaration called function prototype//int demo_cfunction2 (int, int); //no parameter, no return value function demofunction1 (); //There are parameters, there is a return value function demo    intA =1, B =1;//the A and B here are actual arguments.    intx =function2 (A, b); //function Recursion    Longy = recursion ( -);//Results: 5050//a demonstration of passing an array type to a function//As I said before, Ary is the first address of an array, that is, passing pointers, meaning that arguments and formal parameters point to a place, which is equivalent to a reference type    intAry[] = {0,1,2,3,4 }; Update (ary,5);//the array ary is updated//demonstration of local variables and global variablesDemo_variable1 (); //a demonstration of how variables are storedDemo_variable2 (); //variable parameter function (mutable parameter)Demo_params (); return "look at the code and the comments.";}//no parameter, no return value function demovoidfunction1 () {//null argument, the difference between write void and not write void (C language suggests using void when no parameters are used)    /*void Fun ();//Represents the ability to pass arbitrary arguments (c + + means that no parameters can be passed) fun (1);//Normal compilation (in this case, it is actually compiled incorrectly, because the C + + compiler) Fun (1, 2, 3); Normal compilation (in this case, this is actually a compile error, because the C + + compiler is used)*/    /*void Fun (void);//delegate cannot pass any parameters fun (1);//Compilation Error Fun (1, 2, 3);//Compile Error*/}//There are parameters, there is a return value function demointFunction2 (intAintb) {//the A and B here are formal parameters, and the parameter is a copy of the argument.//The parametric only allocates the memory unit during the call, and the call ends immediately, that is, the shape parametric is valid only within the function, and leaving the function is no longer used, that is, the action of the formal parameter is within the function .    returnA +b;}//recursive invocation of the demo function//return:0 + 1 + 2 + 3 + ... + nLongRecursion (intN) {    Longresult =0 ; if(N >0) {result+=N; Result+ = recursion (N-1); }    returnresult;}//The argument is a demonstration of the array type (the reason it is necessary to pass the length of the arylength arrays is because the passed ARY is a pointer and cannot be evaluated for the length of the array it points to. Just like the main function, you need to pass the number of arguments)voidUpdateintAry[],intarylength) {    //the arguments and parameters here actually point to a place     for(inti =0; i < arylength; i++) {Ary[i]=0; }}//demonstration of local variables and global variablesvoidDemo_variable1 () {//I'm a local variable, scoped within a function    inti =1; {        //I am a local variable within a compound statement, scoped within a compound statement, and cannot be the same name as a local variable outside a compound statement        intj =1; }    //I am a local variable, and if the global variable has the same name as me, then the "mask" global variable    intGLOBAL01 = $; //The above "block" the global variable, then how do I refer to the global variable? Use "::" as follows    intGlobal01outside =:: global01;}//a demonstration of how variables are storedvoidDemo_variable2 () {//It can be divided into static storage mode and dynamic storage mode from the perspective of the lifetime of variables.//1. Static storage: means to allocate a fixed amount of storage space during program operation//2. Dynamic storage: A way to dynamically allocate storage space as needed during program operation//Global variables are allocated a fixed amount of storage space (static storage) during program run//local variables, by default, are auto, which is the dynamic allocation of storage space. The following sentence is full of auto int i = 0; (Dynamic storage mode)    inti =0; //If you want the value of a local variable in a function to not disappear after the function call ends, you should specify the local variable as static local variable (static storage mode) with static    Static intj =0; //Register variable is specified with register, which is stored in registers in the CPU. One of the best scenarios is to use it as a loop-controlled variable//The register variable has no address, so it cannot be addressed with "&" (c + + can take its address, but the compiler automatically turns it into a memory variable). In addition, if the register is not enough, it will automatically become a memory variableRegisterintx =0; //use volatile to guarantee that the variable must be in memory and not be put into the CPU register (if the compiler thinks there is no external modification to the variable, it will be placed in the Register for optimization)    volatile inty =0; //A global variable is defined outside a function and is scoped to start at the definition of the variable and at the end of the program file//if the function before the definition point wants to refer to the global variable, you should use extern to indicate that the variable has already been defined elsewhere, and that the variable is used here    extern intGlobal02, GLOBAL03;//If this is not the case, the compilation will report an "undeclared identifier" error    intP02 =global02; intP03 =global03;}intGLOBAL02 = $, global03 = -;//The following shows how to define and use a variable parameter function (variadic)#include<stdarg.h>//it defines va_start, Va_arg, Va_end (VA should be shorthand for variable argument (variable parameters))voidDemo_params () {//calculates the average of n integers, and the first parameter represents the number of subsequent arguments    floatAverageintParamCount, ...); floatresult = Average (4,1,2,3,4);//2.5}floatAverageintParamCount, ...) {    //define a list of parametersva_list Va_params; Longsum =0; //tells the number of indeterminate arguments and prepares to read parametersVa_start (Va_params, ParamCount);  for(inti =0; i < ParamCount; i++)    {        //Reads the value of the parameter one at a specified type, and after each reading, the parameter pointer moves backwards to continue reading the next        intValue = Va_arg (Va_params,int); Sum+=value; }    //Stop reading Parametersva_end (Va_params); returnSum *1.0f/ParamCount;}



Ok
[SOURCE DOWNLOAD]

Indispensable Windows Native (6)-C language: Functions

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.