函数不返回值,但是cout显示它 [英] Function not returning value, but cout displays it

查看:106
本文介绍了函数不返回值,但是cout显示它的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经学习C ++了一段时间,并尝试制作一个简单的函数来返回房间的面积。 return语句不会输出该值,但是使用cout可以看到结果。我在这里缺少什么吗?

I've been learning C++ for a bit and tried my hand at making a simple function that returns the area of a room. The return statement doesn't output the value, however using cout I can see the result. Am I missing something here?

#include <iostream>
using namespace std;

int Area(int x, int y);

int main()
{
  int len;
  int wid;
  int area;
  cout << "Hello, enter the length and width of your room." << endl;
  cin >> len >> wid;
  cout << "The area of your room is: ";
  Area(len, wid);
  return 0;
}

int Area(int len, int wid)
{
  int answer = ( len * wid );
  return answer;
}


推荐答案

std :: cout 用于在屏幕上打印数据。函数仅返回值,因此 Area 函数将返回要在 std :: ostream中传递的值: :operator<< 函数进行打印。您需要编写:

std::cout is used to print data on screen. Functions only return values, so the Area function will return the value which is to be passed in std::ostream::operator<< function to print it. You need to write:

std::cout << Area(len, wid) << "\n";

这篇关于函数不返回值,但是cout显示它的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆