Functions, Recursion, and Recursion

1. Arrays can be used as formal parameters.

example:

 

operation result:

 

 When an array is used as a formal parameter, the real formal parameter is not an array, but a variable that can be used as an array.

None of the memory areas contained in the array formal parameters are provided by the called function.

 It can be omitted when declaring an array formal parameter, and the number of storage areas contained in it can be omitted.

Example: When an external function calls an array, the number of storage areas of the array can be omitted.

 operation result:

 The formal parameter of the array needs to cooperate with a formal parameter of integer type to indicate the number of storage areas contained in the formal parameter of the array. 

example:

Array formal parameters allow the called function to use storage provided by other functions. 

These parameters are called input and output parameters.

practise:

#include<stdio.h>
int print(int array[],int size){
    for(int i=0;i<size;i++){
        array[i]=0-array[i];
    }
}

int main(){
    int array[]={1,2,3,4,5};
    print(array,5);
    for(int i=0;i<5;i++){
        printf("%d ",array[i]);
    }
    return 0;
}

Output result:

 

 practise:

#include<stdio.h>
#include<stdlib.h>
#include<time.h>
int print(int array[],int size){    
    for(int i=0;i<size;i++){
       array[i]=rand()%36+1;
    }
}

int main(){
    srand(time(0));
    int array[7]={0};
    print(array,7);
    for(int i=0;i<7;i++){
        printf("%d ",array[i]);
    }
    return 0;
}

Output result:

 

The number of function parameters in C language may not be fixed. 

Such parameters are called variable-length parameters.

Variable-length parameters cannot be named when writing a function, and special methods must be used in the called function to obtain the contents of these parameters.

example:

 In the above program, the guess result ret is called the implicit declaration of the function.

The type of the parameter in the function implicit declaration can only be int type or double type 

 If the actual format of the function is inconsistent with the implicit declaration, the compilation will report an error.

example:

Running result: the program reports an error

The part before the curly braces of the function can be used as a statement alone, and this kind of statement is called a function declaration statement.

The names of the formal parameters can be omitted in the function declaration statement. 

Writing the function declaration statement at the beginning of the file is called the explicit declaration of the function.

Explicit declarations avoid implicit declarations.

Output result:

 

 All functions except the main function should be declared explicitly.

2. The exit standard function can immediately end the execution of the program.

 In order to use this standard function you need to include the stblib.h header file

This function requires an integer type parameter, which has the same function as the return value of the main function.

example:

 Output result: return; the function exits the current function, exit(0); the function exits the entire program.

 3. Functions in C language can call themselves.

Such functions are called recursive functions.

example:

Output result: output countless 1

 

 If a problem can be decomposed into several small problems, at least one of which is essentially the same as the original problem (just a little simpler), this kind of problem is suitable for recursive function to solve.

Steps to write a recursive function:

(1) Write statements to solve each small problem after decomposition. (Assuming that the recursive function has been written and can be used directly)

(2) Write a branch at the beginning of the recursive function to handle undecomposable situations (this branch must ensure that the function can end)

example:

 operation result:

 practise:

#include<stdio.h>
int add(int num){
    if(num==1){
        return 1;
    }
    return add(num-1)+num;

}
int main(){
    int num=0;
    printf("请输入一个数字:");
    scanf("%d",&num);
    printf("求和结果为:%d\n",add(num));

    return 0;
}

operation result:

The idea of ​​using recursive functions to solve problems is called recursion.

The idea of ​​using loops to solve the same problem is called recursion.

 When testing recursive functions, first test with the simplest parameters, and then gradually change the parameters to

 practise:

 

#include<stdio.h>
int func(int num1,int num2){
    //取余函数%
    //当A>B时,A%B的余数正常
    //当A<B时,A%B=A
    if(!(num2%num1)){
        return num1;
    }
   return func(num2%num1,num1);

}
int main(){
    int num1=0,num2=0;
    printf("请输入两个数字:");
    scanf("%d%d",&num1,&num2);
    printf("最大公约数是%d\n", func(num1,num2));
    return 0;
}

 Output result:

 Note: //Remainder function%
           //When A>B, the remainder of A%B is normal
           //When A<B, A%B=A

practise:

 

#include<stdio.h>
int func(int num){
      if(num<=1)
      {
          return 1;
      }
        return func(num-2)+func(num-1);
}
int main(){
    int num=0;
    printf("请输入数字:");
    scanf("%d",&num);
    printf("%d\n", func(num));
    return 0;
}

operation result:

 4. All statements that can use a variable are called the scope of the variable.

 A variable declared in a function is called a local variable , and its scope includes all statements in the function.

example:

Running result: compilation error.

Variables declared outside all functions are called global variables , and their scope includes all statements in the program.

example:

 Output result: the val variable can be called in both the main function main and the function func

 Global variables that are not initialized are automatically initialized to 0

Global variables and local variables can have the same name.

Variable names take precedence to represent local variables.

example:

 Output result:

If both global variables and local variables can solve the problem, local variables should be given priority.

 Storage is used without scope (storage can be used across functions).

Storage area usage is limited by lifetime.

The life cycle is a period of time. At the beginning of the life cycle, the computer allocates a storage area to the program, and at the end of the life cycle, the computer takes back the storage area allocated to the program.

The lifetime of a global variable is the execution time of the entire program.

The lifetime of a local variable is the time frame of a certain execution of the function.

When the function starts, the computer allocates storage for local variables, and when the function ends, the computer reclaims the storage for local variables.

So if the function is executed multiple times, the storage area corresponding to the local variable may be different each time it is executed.

example:

Running result: the first time the func function is called in the main function, the output num is a random number, and the second time the func function is called again through the func1 function, it is still a random number, indicating that the computer will immediately release its storage area when a function ends. Find a new random storage area, so when the function is executed multiple times, the storage area may be different.

 

 The life cycle and scope of static variables are different from ordinary variables.

5. The static keyword should be used when declaring static variables.

Both global and local variables can be declared static.

The lifetime of a static local variable is the execution time of the entire program.

example:

operation result: 

Static variables that are not initialized are automatically initialized to 0 

Storage for static local variables is always available.

The scope of static local variables is the same as that of ordinary local variables.

example:

 Running result: When the func function is run for the second time, the static function is not affected by the initialization and becomes 10, but 1000

 Note: The initialization of static variables is only performed once at the beginning of the program.

The life cycle of a static global variable is still the execution time of the entire program, but its scope only includes all statements in the file where it is declared (static global variables cannot be used across files)

The scope of variables is divided into three types: local variables, static global variables, and global variables.

The life cycle of variables is divided into two types: ordinary local variables and other variables.

Exercise: Optimized Fibonacci numbers:

The first:

#include<stdio.h>
int func(int num){
    static array[50]={0};
      if(num<=1)
      {
          return 1;
      }
      if(!array[num -2]){
          array[num-2] = func(num-2);
      }
      if(!array[num-1]){
          array[num-1] = func(num-1);
      }
      return array[num-2 ]+array[num-1];
}
int main(){
    int num=0;
    printf("请输入数字:");
    scanf("%d",&num);
    printf("%d\n", func(num));
    return 0;
}

 The second type:

#include<stdio.h>
int array[50]={0};
int func(int num){
      if(num<=1)
      {
          return 1;
      }
      if(!array[num -2]){
          array[num-2] = func(num-2);
      }
      if(!array[num-1]){
          array[num-1] = func(num-1);
      }
      return array[num-2 ]+array[num-1];
}
int main(){
    int num=0;
    printf("请输入数字:");
    scanf("%d",&num);
    printf("%d\n", func(num));
    return 0;
}

The third type:

#include<stdio.h>
int func(int num,int array[],int size){

      if(num<=1)
      {
          return 1;
      }
      if(!array[num -2]){
          array[num-2] = func(num-2,array,size);
      }
      if(!array[num-1]){
          array[num-1] = func(num-1,array,size);
      }
      return array[num-2 ]+array[num-1];
}
int main(){
    int num=0;
    int array[50]={0};
    int size=50;
    printf("请输入数字:");
    scanf("%d",&num);
    printf("%d\n", func(num,array,size));
    return 0;
}

 

Guess you like

Origin blog.csdn.net/weixin_44954230/article/details/127284280