void fun(){return 1;}是否正确
时间: 2023-05-25 08:06:37 浏览: 35
这是错误的。
在 C/C++ 中,函数类型为 `void` 表示函数没有返回值,因此您不能在函数中使用 `return 值` 语句来返回值。
如果您想返回一个整数值,则需要将函数类型更改为 `int`,并在函数末尾使用 `return 值` 来返回该值。
相关问题
怎样将int main函数改为void fun
将原来的 `int main()` 改为 `void fun()`,并且去掉 `return` 语句即可。例如:
```c++
#include <iostream>
void fun() {
std::cout << "Hello, world!" << std::endl;
}
int main() {
fun();
return 0;
}
```
改为:
```c++
#include <iostream>
void fun() {
std::cout << "Hello, world!" << std::endl;
}
int main() {
fun();
}
```
注意,`main` 函数是程序的入口,它的返回值表示程序的结束状态,一般来说返回值为 0 表示程序正常结束,非 0 表示程序异常结束。因此在将 `main` 函数改为其他函数时,需要注意函数的返回值是否符合要求。在上面的例子中,我们去掉了 `return` 语句,表示程序正常结束。
void fun(int n)
void fun(int n) {
if (n <= 0) {
return;
}
for (int i = 0; i < n; i++) {
printf("这是一个函数,参数为%d\n", n);
}
}
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![docx](https://img-home.csdnimg.cn/images/20210720083331.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)