当前位置:网站首页>021 basics of C language: recursion, variable parameters

021 basics of C language: recursion, variable parameters

2022-06-27 04:25:00 Prison plan progress 50%

One : summary

Recursion is the process of repeating an item in a similar way to itself . alike , In programming languages , Call the function itself inside the function , It's called a recursive call .

void recursion(){
    
	recursion(); 	//  Function call itself 
}
int main(){
    
	recursion();
}

C Language supports recursion , namely , A function can call itself . But when using recursion , Programmers need to pay attention to defining a condition to exit from a function , Otherwise, it will enter into an infinite cycle .

Recursive functions play an important role in solving many mathematical problems , For example, calculate the factorial of a number 、 Generate the Fibonacci sequence , wait .

 

Two : Factorial of numbers

#include <stdio.h>
int jiecheng(unsigned int i){
    
	if(i <= 1){
    
		return 1;
	}
	return i * jiecheng(i -1);
}
int main(){
    
	printf("input jiecheng number: ");
	int n;
	scanf("%d", &n);
	printf("%d  The factorial is : %d", n, jiecheng(n));
}
 result :
	┌──(rootkali)-[~/Desktop/c_test]
	└─# ./jiecheng
	input jiecheng number: 5
	5  The factorial is : 120   

 

3、 ... and : Variable parameters

Sometimes , It could happen , You want the function to have a variable number of arguments , Instead of predefined numbers of parameters .C Language provides a solution for this situation , It allows you to define a function , Can accept variable number of parameters according to specific needs .

The following example demonstrates the definition of this function .

int func(int, ...){
    
	.
	.
	.
}
int main(){
    
	func(1, 2, 3);
	func(1, 2, 3, 4);
}

function func() The last parameter is written as an ellipsis , That's three point numbers , The argument before the ellipsis is always int, Represents the total number of variable parameters to pass .

To use this function , Need to use stdarg.h The header file , This file provides functions and macros for variable parameter functions .

The specific steps are as follows :

  1. Define a function , The last parameter is the ellipsis , The argument before the ellipsis is always int, Indicates the number of parameters .
  2. Create a... In the function definition va_list Type variable , This type is in stdarg.h As defined in the header file .
  3. Use int Parameters and va_start Macro to initialize va_list Variable is a list of parameters . macro va_start Is in stdarg.h As defined in the header file .
  4. Use va_arg The macro and va_list Variable to access each item in the parameter list .
  5. Use macros va_end To clean up the endowments va_list Variable memory .

Now let's follow the steps above , To write a function with a variable number of arguments , And return their average :

#include <stdio.h>
#include <stdarg.h>
double average(int num,...){
    
	va_list valist;
	double sum = 0.0;
	int i;
	/*  by  num  Initialization of parameters  valist */
	va_start(valist, num);

	/*  Visit all assigned to  valist  Parameters of  */
	for (i = 0; i < num; i++){
    
		sum += va_arg(valist, int);
	}     /*  Clean as  valist  Reserved memory  */
	va_end(valist);
	return sum/num;
}
int main() {
    
	printf("Average of 2, 3, 4, 5 = %f\n", average(4, 2,3,4,5));
	printf("Average of 5, 10, 15 = %f\n", average(3, 5,10,15));
} 

When the above code is compiled and executed , It will produce the following results . It should be pointed out that , function average() Called twice , Each time, the first parameter represents the total number of variable parameters passed . Ellipsis is used to pass a variable number of parameters .

Average of 2, 3, 4, 5 = 3.500000
Average of 5, 10, 15 = 10.000000
原网站

版权声明
本文为[Prison plan progress 50%]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/178/202206270404362765.html