Thread: problem with function

  1. #1
    Registered User
    Join Date
    Dec 2013
    Posts
    18

    problem with function

    Code:
    int print(void);
    
    
    int main(void)
    {
        cout << "The Lucky " << print() << endl;     //This line
        return 0;
    }
    
    
    int print(void)
    {
        cout << "No : ";
        return 3;
    }

    expected output is THE LUCKY 3


    but what i got is NO: THE LUCKY 3


    please help me.Thank you

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    The order of evaluation is unspecified, hence it is possible for "The Lucky" to be printed before or after print() is called. To fix this, break the chain:
    Code:
    cout << "The Lucky ";
    cout << print() << endl;
    This should do what you want, but I am still uncomfortable with it. Why does it return 3? Why not just write:
    Code:
    void print()
    {
        cout << "No : 3";
    }
    Then call it as:
    Code:
    print();
    ? Or you could simply not have this print function, but I presume you simplified your example from something less trivial.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Dec 2013
    Posts
    18
    what is mean the order of evaluation.detial please

  4. #4
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    There can be no detail about the order of evaluation, because the order of evaluation is not specified. As long as all the things that are supposed to happen do happen, they can happen in whatever order the compiler finds convenient. (The only thing that must happen is that the 3 must be somewhere after the "no:", but it need not be immediately after.)

  5. #5
    Registered User Joelito's Avatar
    Join Date
    Mar 2005
    Location
    Tijuana, BC, México
    Posts
    310
    I'll go with the simplest:
    Code:
    #include <iostream>
    
    int myprint() {
        return 3; // return this to the stream
    }
    
    int main() {
        std::cout // opens the stream
        << "Lucky number: " // print some nice text
        << myprint() // print the returning value
        << std::endl; // line jump
        return 0;
    }
    * PC: Intel Core 2 DUO E6550 @ 2.33 GHz with 2 GB RAM: Archlinux-i686 with xfce4.
    * Laptop: Intel Core 2 DUO T6600 @ 2.20 GHz with 4 GB RAM: Archlinux-x86-64 with xfce4.

  6. #6
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by bhuvaneshnick View Post
    what is mean the order of evaluation.detial please
    If you look at this statement:

    int x = a() + b() + c();

    You cannot tell which of the function a, b and c the compiler will invoke in what order. You probably assume that the functions will be called like

    a
    b
    c

    But in reality, the compiler is allowed to call them in any order, so

    c
    b
    a

    or even

    c
    a
    b

    is allowed. This is called the order of evaluation.
    Because of this, it is either recommended that you

    Call the function sequentially and cache their return values and then do stuff with them, or
    Make sure the functions have no side effects (that means they must only return a value and may read variables, but may never write to any variables but local variables).

    Code:
    int foo(int& x)
    {
        std::cout << "Meh."; // <--- This has side effects since std::cout is a global object which you are writing to.
        x = 10; // <--- This has side effects since you are writing to a non-local variable.
        int y = 10;
        y = 20; // <--- OK, writing to a local variable
        return y; // <--- OK, returning a local variable
    }
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problem passing argument into function, basic problem
    By tsdad in forum C++ Programming
    Replies: 7
    Last Post: 05-22-2013, 12:09 PM
  2. Replies: 2
    Last Post: 11-14-2011, 08:08 AM
  3. function inside function (most likely) problem
    By vlaskiz in forum C++ Programming
    Replies: 2
    Last Post: 10-16-2011, 05:10 AM
  4. Replies: 14
    Last Post: 03-02-2008, 01:27 PM
  5. Replies: 5
    Last Post: 10-17-2006, 08:54 AM