Thread: cout<< ?

  1. #1
    Registered User tu_user's Avatar
    Join Date
    Jan 2004
    Posts
    36

    Question cout<< ?

    Is it necessary to use cout<< when we call a function in main() ?

    if so y? whats the logic?

    I tried to call a void function in main() like this

    cout<< function();

    it gives me a perse error before token')'.

    but if i call it like function(); without cout<< it gives me the right output. y is this so?

    do we have to use cout<< only when function is not void or when function doesn't take any aurguments?

    This is a very simple question with a very complex logic behind it. Can anyone clear this?

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Does function return anything useful (say a string or an int) which cout could actually cope with?

    Because if function() returns void, then yeah, it isn't going to be happy about it.

    Next time, post some actual code you tried to compile so we can see all the relevant context to the question.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User tu_user's Avatar
    Join Date
    Jan 2004
    Posts
    36
    thanku and i'll be keeping ur advice in mind.

  4. #4
    Hardware Engineer
    Join Date
    Sep 2001
    Posts
    1,398
    Is it necessary to use cout<< when we call a function in main() ?
    No.

    cout is only used to display stuff.

    Functions are used to "do something"... like to calculate something.

    So, if function() "returns" a value, then cout << function() will display the value.

    This:
    x = function();
    cout << x;

    Is the same as:
    cout << function();

    Are you working from a book or tutorial? Your book or tutorial should explain how functions and cout work. Most programmers (maybe all programmers) learn from books. (It's OK to start-out with a tutorial.)

    I assume that you have found the tutorials here at http://www.cprogramming.com/tutorial.html.

  5. #5
    Registered User tu_user's Avatar
    Join Date
    Jan 2004
    Posts
    36

    Thumbs up

    I meant the same. Thanku v m. So nice of u.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Cout<< Problems!
    By Nailogamer in forum C++ Programming
    Replies: 5
    Last Post: 08-13-2003, 11:54 PM
  2. Can you cout<< an array?
    By Inquirer in forum C++ Programming
    Replies: 6
    Last Post: 08-05-2002, 06:32 PM
  3. cout<< only in box in corner
    By CARBUNCLE in forum C++ Programming
    Replies: 2
    Last Post: 05-02-2002, 07:04 AM