编写函数fun,其功能是从三个形参abc找出最大数,并作为函数值返回,Python语言编程
时间: 2023-12-29 09:01:55 浏览: 57
以下是函数fun的Python代码实现:
```python
def fun(a, b, c):
max_num = a
if b > max_num:
max_num = b
if c > max_num:
max_num = c
return max_num
```
函数接受三个参数a、b、c,首先将a赋值给max_num。然后依次判断b和c是否大于max_num,如果是,则将其赋值给max_num。最后返回max_num作为函数值,即为三个数的最大值。
相关问题
编写函数fun,函数的功能是:根据以下公式计算s,计算结果作为函数值返回;n通过形参传入。
我可以回答这个问题。函数fun的公式如下:s = 1 + 1/2 + 1/3 + ... + 1/n。函数fun的实现如下:
double fun(int n) {
double s = .;
for (int i = 1; i <= n; i++) {
s += 1. / i;
}
return s;
}
编写函数float fun(int n),功能是通过形参n计算1+1/2+1/3加加加加到1/n并作为函数值返回
以下是函数float fun(int n)的完整代码:
```
float fun(int n) {
float sum = 0.0;
for (int i = 1; i <= n; i++) {
sum += 1.0 / i;
}
return sum;
}
```
这个函数的功能是通过形参n计算1 1/2 1/3加加加加到1/n并作为函数值返回。
相关推荐
![c](https://img-home.csdnimg.cn/images/20210720083646.png)
![c](https://img-home.csdnimg.cn/images/20210720083646.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)