C语言中,自定义函数,如何将变量放进去
时间: 2024-10-01 22:08:47 浏览: 32
在C语言中,要将变量放入自定义函数,首先需要声明函数并指定它接受哪些参数。例如,如果你想让一个名为`addNumbers`的函数接收两个整数并返回它们的和,你可以这样做:
```c
#include <stdio.h>
// 定义一个接受两个int类型的参数的函数
int addNumbers(int num1, int num2) {
// 函数体内对num1和num2进行操作
int sum = num1 + num2;
return sum;
}
int main() {
// 在main函数中,我们定义了一个变量并把它传给addNumbers函数
int a = 5;
int b = 7;
int result = addNumbers(a, b);
printf("The sum is: %d\n", result); // 输出: The sum is: 12
return 0;
}
```
在这个例子中,`a`和`b`是变量,当你调用`addNumbers(a, b)`时,它们的值会被传递给函数。
阅读全文
相关推荐


















