[C language] Summary of function knowledge (2)

1. Recursive function

In C language, in addition to calling other functions, a function can also call itself directly or indirectly. This form of a function calling itself is called a recursive call of the function. A function with a recursive call is also called a recursive function.

  • Direct recursive call: the function calls itself directly
  • Indirect recursive call: a function calls itself indirectly through other functions
  • Key: Find recursive algorithms and the termination conditions of recursion
  • The recursive function call itself just calls a function with the same name and the same function, or it can be understood as calling a copy of itself. Every time a function is called in C language, the system allocates new memory space for the function, stores various variables and parameters required for runtime, and generates a new function entity. The occupied memory will not be released until the function ends. memory space. During recursion, each call to itself will generate new formal parameter variables. Although they have the same name, they are not the same variable.

Example: Output the nth term of the Fibonacci sequence.
Insert image description here
Code:

#include<stdio.h>
long fibo(int);
int main()
{
    
    
	int n,f;
	scanf("%d",&n);
	f=fibo(n);
	printf("%d\n",f);
	return 0;
}
long fibo(int n)
{
    
    
	int f;
	if(n==1||n==2)
	   f=1;
	else
	   f=fino(n-1)+fibo(n-2);
	return f;
}

operation result:

10
55

2. Array as parameter of function

1. Use array elements as actual parameters
2. Use array names as formal parameters and actual parameters of functions

  • In C language, the array name represents the first address of the array
  • The array name is used as a parameter of the function. At this time, the formal parameter and the actual parameter point to the same storage unit, that is, the formal parameter group can access the storage unit where the actual parameter group is located, and can also change the contents of these units, so the actual parameter group The value of the element is changed

3. Things to note when using arrays as functions:

  • The names of the formal parameter group and the actual parameter group do not need to be the same but their types must be the same.
  • In the function parameter list, it is not allowed to give the length of the formal parameter group, and a variable parameter can be added to represent the number of array elements. 4. The
    difference between using array names as function parameters and using array elements as actual parameters:
  • When using array elements as actual parameters, as long as the array type is consistent with the type of the formal parameter variable of the function,
  • The array name is the first address of the array, so when the array name is used as a function parameter, the transfer is only the transfer of the address value, that is to say, the first address of the actual parameter group is assigned to the formal parameter group name. After the formal parameter group name obtains the first address, it also points to the array of actual parameters. In fact, the formal parameter group and the actual parameter group are the same array and share the same memory space.

3. Local variables and global variables

(1) The scope of a variable is the effective scope of the variable. C language only allows variables to be defined in 3 places:
1. The declaration part inside the function
2. Outside all functions
3. The declaration part in the compound statement
(2) Local variables : Only valid within the scope of this function
1. Variables with the same name can be used in different functions or compound statements, but they are not the same variables. They occupy different units in the memory.
2. Variables defined in the main function main are only in the main function. efficient. The main function cannot use variables defined in other functions.
3. Formal parameters are also local variables. Memory is allocated for the function when it is called, and the occupied memory will be released when the function exits.
4. Variables can be defined in a compound statement inside a function, and a single variable is only valid in this compound statement.
5. The compilation system does not check the function name and local Whether the variable has the same name
(3) Global variable: the valid range is from the location where the global variable is defined to the end of the source file
1. If the function before the global variable definition wants to reference the global variable, you need to use keywords in the function extern is used as an external variable declaration
. 2. It can only be defined once in the same scope, and the location of the definition is outside all functions.
3. If the global variable has the same name as the local variable, the global variable will not work within the scope of the local variable ( The program's reference to variables follows the principle of minimum scope)

4. Variable lifetime and storage type

1. Variables are allocated storage units from the time of definition to the storage units are recycled at the end of operation. The whole process is called variable life cycle 2.
Storage distribution of C program
Insert image description here
3. Based on the life cycle of variables, the storage categories of variables are divided into

  • Static storage method: refers to the method of allocating fixed storage space during program running.
  • Dynamic storage method: a method of dynamically allocating storage space as needed during program running

4. Storage type of variables

  • auto (automatic type) variables: When defining automatic variables, auto can be omitted, so previously used variables are automatic variables

  • Register (register type) variable: It is also an automatic variable. The difference from the auto variable is that the value of the register variable is stored in the register and not in the memory.

  • static (static type) variable: The storage unit of this variable is allocated in the static storage area of ​​​​the data area. If the value of the local variable in the function can still be retained after the function call is completed, so that it can be used the next time the function is called, the local variable can be defined as a static type.

  • extern (external type) variables: external variables, which are global variables declared in the program that have been defined outside the function.

  • Things to note about static local variables and automatic variables:
    1. Static local variables are assigned an initial value at compile time, that is, they are only assigned an initial value once; while automatic variables are assigned an initial value when the function is called, and the initial value is assigned again every time the function is called. Value
    2. If an automatic variable is not assigned an initial value, its initial value is uncertain; if no initial value is assigned when defining a static local variable, the system will automatically assign an initial value of 0 during compilation. And the initial value is only assigned for the first time in the function. It is performed when calling, and its value in subsequent calls will be the value retained in the previous call.
    3. Static local variables, like global variables, are special usages of variables. If there is no requirement for static storage, it is not recommended to use static variables.

5. Internal functions and external functions

1. Internal functions

  • This function can only be called by other functions in this file and is also called a static function. Define format:
static 类型名 函数名(形参表);
  • Benefits: The scope of the function is limited to the file in which it is located. When multiple people write different program modules, you don’t have to worry about whether the function name you use is the same as that used by others. Even if the function name is the same, there will be no interference.

2.External functions

  • This function is allowed to be called by functions in other files. Define format:
extern 类型名 函数名(形参表);
  • External functions are valid throughout the source program. C language stipulates that if extern is omitted when defining a function, it defaults to an external function.
  • After the external function is defined, the prototype of the called function must be declared with extern in the program file that needs to call this function, indicating that the function has been defined in other files.

Guess you like

Origin blog.csdn.net/m0_74102736/article/details/130670764