Function not returning value, but cout displays it

  • A+
Category:Languages

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 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"; 

Comment

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: