C language function (Part 1)


function(1)

1. What is a function?

  • Definition of function from Wikipedia: Subroutine

  • In computer science, a portion of the code of a large program , consisting of one or more statement blocks. It is responsible for completing a specific task
    and is relatively independent from other codes.

  • Generally, there are input parameters and return values, providing encapsulation and hiding of details of the process. These codes are usually integrated into software libraries.

  • Classification of functions:
    1. Library functions
    2. Custom functions

  • The location of the main function: C language stipulates that in a source program, the location of the main function can be arbitrary. When executing a program written in C language, the main function is equivalent to the entrance to the executed program. As long as there are no grammatical and logical errors, the main function can be placed anywhere. Regardless of the position of the main function in the entire process, a C program always starts execution from the mam function. The main function, also known as the main function, is the entry function of the C program, that is, the execution of the program starts from the main function, and the mobilization of other functions is also called directly or indirectly in the main function. A C program has one and only one main function. In addition to the main function, there can be several other functions. Each function implements a specific operation. Program execution always starts from the main function. If there are other functions, it will return to the main function after completing the calls to other functions. Finally, the main function ends the entire program. When the program is executed, the main function is called by the system.

2.Library functions

  • In the C language, commonly used functions are encapsulated into functions, which are provided for everyone to use.
    For example: scanf printf strlen strcmp rand srand time

  • C language does not directly implement library functions, but provides C language standards and library function conventions. The implementation of library functions is generally implemented by the compiler.

  • Note: When using library functions, you must include the corresponding header file #include.
  • strcpy
  • memset

3. Custom functions

Custom functions are the same as library functions. They have function names, return value types and function parameters

//Find the maximum value of two integers

//Function that can exchange the contents of two integer variables

- //Actual parameters - Actual parameters - //When the actual parameters are passed to the formal parameters - //The formal parameters are a temporary copy of the actual parameters - //So modifications to the formal parameters will not affect the actual parameters
  • The exchange of x and y will not affect a and b at all

Improvement:
First, the value of a is placed in tmp; second step, *pb is assigned to pa; then the value of pb is placed in tmp; in fact, the values ​​of the three variables are exchanged; what is exchanged is the value pointed to by the address. Content; addresses cannot be exchanged.

The final effect was achieved.

4. Function parameters (formal parameter names and actual parameter names can be the same)

  • 4.1
    The actual parameters (actual parameters)
    are really the parameters passed to the function, called actual parameters.
    Actual parameters can be: constants, variables, expressions, functions, etc.
    No matter what type of quantities the actual parameters are, they must have certain values ​​when making function calls so that these values ​​can be transferred to the actual parameters.
  • 4.2
    Formal parameters (formal parameters)
    Formal parameters refer to the variables in parentheses after the function name. Because the formal parameters are only instantiated (memory units allocated) during the function being called, they are called formal parameters. The formal parameters are called formal parameters when the function call is completed. It will be automatically destroyed after that. So formal parameters are only valid within functions.

5. Function call

- 5.1 The formal parameters and actual parameters of a function called by value occupy different memory blocks respectively, and modifications to the formal parameters will not affect the actual parameters. (In this design, the formal parameter is a temporary copy of the actual parameter, and modifications to the formal parameter will not affect the actual parameter.) - 5.2 Call by address - Call by address is to pass the memory address of the variable created outside the function to Function parameters are a way to call a function. - This method of passing parameters can establish a real connection between the function and the variables outside the function, that is, the variables outside the function can be directly manipulated inside the function.

(Variables outside the function can be accessed and operated through the pointers of the formal parameters.)

6. Nested calls and chained access of functions

  • 6.1 Nested calls
    Nested calls are possible; however, there is no nested definition.
  • 6.2 Chain access
    Use the return value of one function as a parameter of another function.

7. Declaration and definition of functions

  • 7.1 Function declaration:
    1. Tell the compiler what the function is called, what the parameters are, and what the return type is. But whether it exists or not cannot be determined by the function declaration.
    2. The declaration of a function generally appears before the use of the function, and it must be declared first and then used .
    3. The function declaration is generally placed in the header file.

  • 7.2 Definition of function:
    The definition of function refers to the specific implementation of the function and explains the functional implementation of the function.

Guess you like

Origin blog.csdn.net/Ghr_C99/article/details/130693702