自定义函数,求3个整数的和。如:输入100 200 50,输出350
时间: 2024-03-14 08:47:58 浏览: 28
你可以按照以下步骤来自定义函数,以求出3个整数的和:
1. 定义一个函数,函数名可以为sumOfThreeIntegers,形式参数列表为三个整型变量。函数定义的形式如下:
```
int sumOfThreeIntegers(int a, int b, int c) {
// 函数体
}
```
2. 在函数体中,编写代码来计算这三个整数的和,可以使用加法运算符(+)。
```
int sumOfThreeIntegers(int a, int b, int c) {
int sum = a + b + c;
return sum;
}
```
3. 在主函数中,调用定义好的函数并传入三个整数作为实际参数,用一个变量来接收函数返回的结果,并将这个变量的值打印出来。
```
int main() {
int a, b, c;
scanf("%d%d%d", &a, &b, &c);
int result = sumOfThreeIntegers(a, b, c);
printf("%d", result);
return 0;
}
```
完整的程序代码如下:
```
#include <stdio.h>
int sumOfThreeIntegers(int a, int b, int c) {
int sum = a + b + c;
return sum;
}
int main() {
int a, b, c;
scanf("%d%d%d", &a, &b, &c);
int result = sumOfThreeIntegers(a, b, c);
printf("%d", result);
return 0;
}
```
这样,当你输入100、200、50时,程序会输出350,即这三个数的和。
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![c](https://img-home.csdnimg.cn/images/20210720083646.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_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)