return-statement with a value
时间: 2023-04-28 18:06:28 浏览: 664
在函数中,使用 return 语句可以结束函数的执行并返回一个值。返回值可以是任何类型的数据,如数字、字符串、布尔值等。
例如:
```
def add(a, b):
return a + b
result = add(1, 2)
print(result) # 输出 3
```
在上面的例子中,函数 add 接受两个参数 a 和 b,并使用 return 语句返回它们的和。调用函数 add(1, 2) 后,函数返回 3,并将结果赋值给变量 result。
相关问题
Error :return-statement with a value,in function returning'void'.
这个错误是因为你在一个返回类型为 void 的函数中使用了带有返回值的 return 语句。在返回类型为 void 的函数中,应该使用不带返回值的 return 语句。
例如,如果你有一个返回类型为 void 的函数 foo,那么应该这样写:
```
void foo() {
// do something
return;
}
```
而不是这样:
```
void foo() {
// do something
return 1;
}
```
因为在返回类型为 void 的函数中,你不能返回任何值。
[Error] return-statement with a value, in function returning 'void' [-fpermissive]
这个错误提示意味着在一个返回类型为 `void` 的函数中使用了带有返回值的 `return` 语句,这样是不合法的。 `void` 类型的函数不应该返回任何值。
可以检查一下代码中是否有这样的情况出现,如果有的话需要修改成正确的写法,例如:
```c++
void myFunction() {
// some code here
return; // return statement without a value
}
```
而不是:
```c++
void myFunction() {
// some code here
return 0; // return statement with a value (not allowed)
}
```
相关推荐
![text/plain](https://img-home.csdnimg.cn/images/20210720083646.png)
![ini-development](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)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)