当前位置:网站首页>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%】
List of articles
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 :
- Define a function , The last parameter is the ellipsis , The argument before the ellipsis is always int, Indicates the number of parameters .
- Create a... In the function definition va_list Type variable , This type is in stdarg.h As defined in the header file .
- 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 .
- Use va_arg The macro and va_list Variable to access each item in the parameter list .
- 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
边栏推荐
- 010 C language foundation: C function
- Argo Workflows —— Kubernetes的工作流引擎入门
- math_ Number set (number set symbol) and set theory
- LDR6028 手机设备一边充电一边OTG传输数据方案
- Mysql database foundation: DQL data query language
- Description of replacement with STM32 or gd32
- Games101 job 7 improvement - implementation process of micro surface material
- 微服务系统设计——API 网关服务设计
- 006 C语言基础:C存储类
- golang hello 安装环境异常【已解决】
猜你喜欢

如何让 EF Core 6 支持 DateOnly 类型

微服务系统设计——微服务调用设计

How to systematically learn LabVIEW?

Kotlin compose implicitly passes the parameter compositionlocalprovider

2022-06-26:以下golang代码输出什么?A:true;B:false;C:编译错误。 package main import “fmt“ func main() { type

Microservice system design -- distributed transaction service design

微服务系统设计——微服务监控与系统资源监控设计

微服务系统设计——服务熔断和降级设计

如何系统学习LabVIEW?

清华大学开源软件镜像站网址
随机推荐
缓存综合项目--秒杀架构
Microservice system design -- distributed cache service design
静态时序分析-OCV和time derate
Microservice system design -- microservice invocation design
USB DRIVER
fplan-Powerplan实例
Learn crypto from Buu (Zhou Geng)
Cache comprehensive project - seckill architecture
真xx相来了?测试/开发程序员为什么不愿意加班,这是个疯狂的状态......
FastDDS的服务器记录-译-
Almost because of json Stringify lost his bonus
为什么 C# 访问 null 字段会抛异常?
008 C语言基础:C判断
Microservice system design -- Distributed timing service design
【Unity】UI交互组件之按钮Button&可选基类总结
百度飞桨“万有引力”2022首站落地苏州,全面启动中小企业赋能计划
A^2=E | 方程的解 | 这个方程究竟能告诉我们什么
第2章 关键技术介绍
基于MobileNet-Yolov4搭建轻量化目标检测
014 C语言基础:C字符串