Article From:https://www.cnblogs.com/Yuuki-/p/9967518.html

function
printf Output Function
Scnf Input Function

You can call a function simply by knowing the name of the function and the contents to be filled in parentheses.
1. If there are repetitive sentences in the code – & gt; writing functions can shorten the amount of code and improve efficiency.
2. When you use the function name + parameter to call a problem directly, you only need to check a single function.

int Function Type/Return Value Type
Main Function Name
() to write parameters if it does not write that there are no parameters or write void that there are no parameters
{} is a function body.
If the return value type void return is followed by no content, return can also be omitted.
void main ()
return; // / Need no added value or can be omitted later
void means empty 1, put in front of the function name (return value type) means that the function has no return value.
2, put function () in the expression function without parameters
are commonly used (in general, function bodies are not visible)
void out textxy (int x, int y, LPCTSTR str);
1. Know how to call function function with function + parameter
2. The Function of Return Value
Malloc Application Memory Returns the First Address of Memory Applied

defines function – & gt; function () contains parameters int a, int b

argument refers to the parameter used by the actual parameter when calling the function, which enables the variable to make constant action pass a value to the parameter.
Actual Participation in Formal Parameters is only the Relation of Appointment
modifies the formal parameters, it will not affect the actual parameters.
Returns to the location of the call function after it is called by assigning parameters with arguments.
Continue to Execute

formal parameters will not affect the actual parameters.
modifies the variables and arrays of the main function (one of the functions of the pointer) by passing the address.

If a function is defined after the call, it must brush the sense of existence in front of it (that is, when the defined function is placed after the main function, it must be declared in advance at the time of the call).

Return Value+Function Name+Parameter

1. Head Documents
2. Type Definition, Global Variables
3. Functional Statement
4. Main Function
5. Definition of Function

The names of parameters in the definition of function declaration function (if any) must be consistent
Recursion and Functional Parameters

Variable Scope Survival Cycle
{} if for function {}”
in the variable defined by {} out of {} can not use {} scope

1. Externally defined scopes of global variable functions affect the running time of the program throughout the file lifecycle
2, Local Variable {} Definition Scope only acts on {inside the life cycle out of {} automatic recovery
3. Variables Modified by Statie of Static Variables Live Cycle and Global Variables

stayic static variable only defines the value of the next time and then the value of the last time to continue to use

 

Test code:

  1 #include<stdio.h>
  2 #include<string.h>
  3 #if 0
  4 /*Design a function  5 1、Determining the function of a function, such as comparing three numbers to the maximum  6 2、Function names cannot be renamed with existing identifier keyword functions  7 is_Max(a,b,c) The order of parameter determination determines the number and type of parameters  8 3、The return value can write void first  9  //is_Max(a,b,c)
 10  The order of parameter determination determines the type of the number of parameters 11  When invoked (), the variable page can be a constant. 12 
 13 */
 14 int is_Max(int a,int b,int c)
 15 {
 16     //c = 22;
 17     if (a > b)
 18     {
 19         if (a > c)
 20         {
 21             //printf("aMaximum "";
 22             return a;//You can return a maximum value
 23         }
 24         else
 25         {
 26             return c;//The function type before the return value should correspond to int
 27         }
 28     }
 29     else
 30     {
 31         if (b > c)
 32         {
 33             return b;
 34         }
 35         else
 36         {
 37             return c;
 38         }
 39     }
 40 
 41     //return;
 42 }
 43  //Custom functions can be written before or after the body of the main function 44  //It can be called directly when it is called in the main function before it is written. 45  //Function declaration is required before the value of the main function is written and called in the main function after the main function.
 46 #endif
 47 
 48 int x;//The global variable scope acts on the running time of the entire file lifecycle program
 49 
 50 
 51 void swapint(int a, int b)
 52 {
 53     int temp = a;
 54     a = b;
 55     b = temp;
 56     printf("In the function a=% d, b=% d\ n",a,b);
 57 }
 58 void swapPoint(int *p, int *q)
 59 {
 60     int temp = *p;
 61     *p = *q;
 62     *q = temp;
 63     printf("In the main function a=% d, b=% d\ n",*p,*q);
 64 
 65 }
 66 
 67 void fun()
 68 {//Scope is in {} but will not be recovered by the system after {}
 69     static int x = 0;//staticModified variable definitions are defined only once
 70     x++;
 71     printf("%d",x);
 72 }
 73 
 74 
 75 
 76 
 77 int main()//Main function
 78 {
 79     ////Code
 80     //printf("hello");//Function with parameter
 81     ////1、int *p = (int*)malloc(sizeof(int));//The return value returns the called function return value assigned to p
 82     //int t=is_Max(1, 2, 3);//The return value of a function call is int, which defines an int variable to receive.
 83     ////is_Max Assign the value of 1 to a, assign the value of 2 to b, and assign the value of 3 to C
 84     //printf("%d",is_Max(1,2,3));
 85     //int x = 1, y = 2, z = 3;
 86     //is_Max(x, y, z);
 87     //printf("hello");//Function with parameter
 88 
 89     int a = 2, b = 3;
 90     swapint(a, b);//Exchange the value of AB by function
 91     printf("A=% d, b=% D in the main function",a,b);
 92     swapPoint(&a, &b);
 93     printf("In the main function a=% d, b=% d\ n",a,b);
 94     
 95     //if (1)
 96     //{
 97     //    int myint;
 98     //    myint = 1;
 99     //}
100     ////myint = 1;  Unavailable
101     fun();
102     fun();
103     fun();
104     
105     
106     getchar();//Functions without parameters
107     return 0;
108 }

 

 

Annex:

formal and practical parameters

function

//class 1————————————————————————————————————————————————————————————————————————————–//

  • function group to execute a task statement is the basic unit of the program
    • Function function
      • Users who use encapsulated code do not need to know how functions are implemented, they only need to know the functions to use them.
      • Save the number of lines of code for some code segments that need to be reused. Using functions can shorten the code and improve programming efficiency.
      •  
    • Function composition
      • Function name
        • Name of function (self-defined)
        • Call when you know the name and parameters of the function.
      • Function type (return value type)
        • When defined, the type written before the function is the same as the type written after the return of the function.
        • If the function does not return any value return type, write no value after void return (return can also be omitted)
      • parameter
        • The contents in function parentheses are called parameter calls and need to pass values in.
      • Function body
        • Statements that specify the content of a function’s execution
    • Definition and use of functions
      • Function definitions provide the actual body of a function (must have)
      • Function name (parameter) in the way that the function is called
      • Function declaration function needs to be added if it is defined after invocation
        • Function declarations are usually preceded by function type function names and parameter types (parameter names can be omitted)
        • Formal parameters and real parameters
          • The parameters in parentheses when defining parametric functions are called parametric functions.
          • The parameters in parentheses when an argument function is called are called arguments.
          • Parameters and parameters can have the same name, but not the same variable does not occupy the same block of memory to modify the parameter does not affect the parameter does not change the parameter.
          • The variable in the calling function can be modified directly by the address through the pointer.

//class 2———————————————————————————————————————————————————————————————————————————-//

  • recursion
    • A function that calls itself (itself directly or indirectly) when it is defined is called recursion.
    • Advantage
      • A small amount of code can describe a problem that requires repeated computation.
      • Recursion that can be achieved by loops can be achieved, but loops that can be achieved by recursion may not be achieved.
    • shortcoming
      • Equivalent to other commonly used methods (such as loops) is inefficient
      • Each call opens up a stack area for parameters and return values. Too many memory recursions in the stack area can easily cause stack overflow.
  • Notes
    • Scope of action
      • Variables defined internally in {} are local variable scopes only in this {} (e.g. in functions or in parentheses of if and for)
      • Externally defined in {} is a global variable. Global variables can be used in any function after definition.
      • Variables between different scopes allow the same name not to be redefined
      • Variables with the same name between different scopes use a recently defined variable within the scope (proximity principle)
    • Naming rules
      • Function Naming Rules and Variable Naming Rules
      • Only numeric letters can be underlined. No other characters can be used. No keywords can be used.
    • Function definitions and function declarations should maintain consistent return value type function name parameters (if the name of a tangible parameter in the declaration is consistent with the time of definition)
    • The formal parameters of a function cannot change the arguments.
  • Understanding
    • Pointer functions that return pointers, such as strcpy strcat, return pointers malloc
    • Function pointer to function pointer
      • Generally used to transfer function names as parameters
      • Writing such as int fun (int x) – & gt; the corresponding function pointer int (* pfun) (int) function name is replaced by (* pfun)
      • Function pointers are assigned in the same way as function names.

 

2018-11-1609:39:15

 

Leave a Reply

Your email address will not be published. Required fields are marked *