some questions on C++

Discussion in 'C++' started by rudra, Apr 14, 2008.

  1. rudra

    rudra Guest

    dear friends,
    i am a novice C++ user; all my previous experience is with fortran.
    newly i started learnning C and C++. and there is few question in my
    mind. I consider to post my queries here, so that experienced ppl may
    help me.
    1. in every function that C uses including main(), we use "return
    0" (stroustroup prefers to use the variable directly in case of
    functions). whta is the significance of this "0"? i checked with
    return 1 and still its working fine. SO WHAT 0 MEANS IN 'RETURN 0'
    STATEMENT?
    2. As my foundation in fortran,(and i guess a valid understanding in C+
    + as well), function do some work of the the code; say when you need
    to calculate something again and again or a seperate calculation whose
    result is to be used by the main program. keeping this in mind, i am
    not getting any logic of void function, which by efination, **does
    not** return any value to the main(). So why on earth the VOID
    FUNCTION NEEDED?

    this may be very primitive question, but i ll be really greatful if
    you ans them.
    regards
    rudra, Apr 14, 2008
    #1
    1. Advertising

  2. rudra

    Fei Liu Guest

    rudra wrote:
    > dear friends,
    > i am a novice C++ user; all my previous experience is with fortran.
    > newly i started learnning C and C++. and there is few question in my
    > mind. I consider to post my queries here, so that experienced ppl may
    > help me.
    > 1. in every function that C uses including main(), we use "return
    > 0" (stroustroup prefers to use the variable directly in case of
    > functions). whta is the significance of this "0"? i checked with
    > return 1 and still its working fine. SO WHAT 0 MEANS IN 'RETURN 0'
    > STATEMENT?

    0 means no error returned from main, anything else is traditionally
    interpreted as some kind of error condition. The only requirement is
    'int' value returned from main.
    > 2. As my foundation in fortran,(and i guess a valid understanding in C+
    > + as well), function do some work of the the code; say when you need
    > to calculate something again and again or a seperate calculation whose
    > result is to be used by the main program. keeping this in mind, i am
    > not getting any logic of void function, which by efination, **does
    > not** return any value to the main(). So why on earth the VOID
    > FUNCTION NEEDED?

    think of void function as 'subroutine' in Fortran.
    >
    > this may be very primitive question, but i ll be really greatful if
    > you ans them.
    > regards


    Fei
    Fei Liu, Apr 14, 2008
    #2
    1. Advertising

  3. rudra

    Stefan Ram Guest

    rudra <> writes:
    >1. in every function that C uses including main(), we use "return 0"


    One may also use:

    #include <cstdlib>
    int main(){ return EXIT_SUCCESS; }

    or

    int main(){}

    In the second case, reaching the final »}« of main
    has the effect of »return 0;«.
    Stefan Ram, Apr 14, 2008
    #3
  4. "Alf P. Steinbach" <> writes:

    > * rudra:
    >> dear friends,
    >> i am a novice C++ user; all my previous experience is with fortran.
    >> newly i started learnning C and C++.

    <snip>
    > In C it's an ordinary function (although called automatically when the
    > program starts), whereas in C++ it's a special function that (1)
    > cannot be called by your code, and (2) doesn't have to explicitly
    > return a result value.


    Tiny point: as of C99 an explicit return from main is no longer
    required (though it is considered bad style to omit it).

    --
    Ben.
    Ben Bacarisse, Apr 14, 2008
    #4
  5. rudra wrote:
    > 1. in every function that C uses including main(), we use "return
    > 0" (stroustroup prefers to use the variable directly in case of
    > functions). whta is the significance of this "0"? i checked with
    > return 1 and still its working fine. SO WHAT 0 MEANS IN 'RETURN 0'
    > STATEMENT?


    As people have already told you, not every function must return a value.

    main() should return a value because the environment which launched
    the program may use that value for something (by far the most usual use
    for that return value is to indicate success or error).

    One application where the return value of programs is completely
    crucial is this: http://en.wikipedia.org/wiki/Make_(software)

    (It's crucial because if a program launched by 'make' erroneously
    returns an error code when it shouldn't, it will cause the build to
    fail, erroneously. Also if the program fails but still returns the
    success code, the building process will not notice the failure, continue
    building, and all kinds of misbehavior is likely to happen.)

    This is the reason why programmers not caring what their main()
    returns is a really bad thing.
    Juha Nieminen, Apr 14, 2008
    #5
  6. rudra

    James Kanze Guest

    On Apr 14, 4:10 pm, "Victor Bazarov" <> wrote:
    > rudra wrote:


    > > 1. in every function that C uses including main(), we use "return
    > > 0" (stroustroup prefers to use the variable directly in case of
    > > functions). whta is the significance of this "0"? i checked with
    > > return 1 and still its working fine. SO WHAT 0 MEANS IN 'RETURN 0'
    > > STATEMENT?


    > It depends on the environment that runs your program. The value zero
    > in many environments means "everything went OK" (you can think of it
    > as "the number of errors that has occurred"). Of course, just as many
    > environments _ignore_ the return value from 'main'.


    The standard requires 0 to mean success; if the system considers
    0 failure, then the runtime library must map it to something
    that would be considered success. The two other values for
    which the standard makes guarantees are EXIT_FAILURE and
    EXIT_SUCCESS. Anything but those three values is implementation
    defined.

    If you're willing to restrict yourself to Unix and Windows, 0
    means success, and a small positive value means failure. (Note
    that at least under Unix, only the low order bits are
    propagated, so something like 0x8000 will mean success as well.)

    If you want to actually see the effect, something like:
    myProgram && echo "success"
    in any of the usual shells will show it clearly. (I'd suggest
    that the original poster actually try this. Once returning 0,
    and once returning 1.)

    > > 2. As my foundation in fortran,(and i guess a valid understanding in
    > > C+ + as well), function do some work of the the code; say when you
    > > need to calculate something again and again or a seperate calculation
    > > whose result is to be used by the main program. keeping this in mind,
    > > i am not getting any logic of void function, which by efination,
    > > **does not** return any value to the main(). So why on earth the VOID
    > > FUNCTION NEEDED?


    > It's an abomination, I'll give you that. But think of a function
    > that returns 'void' (Not VOID, C++ is case-sensitive) as having the
    > same significance as "SUBROUTINE" in Fortran. C++ does not make any
    > distinction between "FUNCTION" and "SUBROUTINE", except by means of
    > the return value type.


    It's a question of terminology. Neither in Fortran nor in C++
    are functions necessarily something that a mathematician would
    consider a function. In both languages, they can have side
    effects, and modify global state. The fact that some also have
    return values, and others don't, is largely incidental.
    Personally, I'd prefer a more neutral name, like procedure or
    subroutine. But the usage of "function" in C++ is so
    established that it's not likely to change.

    --
    James Kanze (GABI Software) email:
    Conseils en informatique orientée objet/
    Beratung in objektorientierter Datenverarbeitung
    9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
    James Kanze, Apr 15, 2008
    #6
  7. rudra

    James Kanze Guest

    On Apr 14, 5:21 pm, Ben Bacarisse <> wrote:
    > "Alf P. Steinbach" <> writes:


    > Tiny point: as of C99 an explicit return from main is no longer
    > required (though it is considered bad style to omit it).


    It's also considered bad style to omit it in C++. If you return
    from main. (Generally, in C++, it's considered bad style, and
    can also cause problems, to call exit() directly, so well
    written programs which terminate normally usually do return from
    main.)

    --
    James Kanze (GABI Software) email:
    Conseils en informatique orientée objet/
    Beratung in objektorientierter Datenverarbeitung
    9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
    James Kanze, Apr 15, 2008
    #7
  8. rudra

    James Kanze Guest

    On Apr 14, 5:29 pm, "Alf P. Steinbach" <> wrote:

    [...]
    > But, who cares about C99?


    The C++ standards committee. C compatiblity is still considered
    de rigor for some aspects (preprocessor, basic types, etc.), and
    that means standard C. In other words, C99. (Thus, we will be
    getting extended integral types, vararg macros, etc., in the
    next version of the standard.)

    --
    James Kanze (GABI Software) email:
    Conseils en informatique orientée objet/
    Beratung in objektorientierter Datenverarbeitung
    9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
    James Kanze, Apr 15, 2008
    #8
  9. rudra

    James Kanze Guest

    On Apr 14, 11:55 pm, Juha Nieminen <> wrote:

    [...]
    > main() should return a value because the environment which launched
    > the program may use that value for something (by far the most usual use
    > for that return value is to indicate success or error).


    > One application where the return value of programs is completely
    > crucial is this:http://en.wikipedia.org/wiki/Make_(software)


    Are there any applications where it isn't? Any program which
    can be invoked from the command line will sooner or later end up
    being invoked by something like:
    prog < important > temp && mv temp important
    to make modifications in an existing file. And critical servers
    are generally started by shell scripts, which restart them
    automatically if they terminate abnormally.

    --
    James Kanze (GABI Software) email:
    Conseils en informatique orientée objet/
    Beratung in objektorientierter Datenverarbeitung
    9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
    James Kanze, Apr 15, 2008
    #9
  10. rudra

    Stefan Ram Guest

    James Kanze <> writes:
    >It's a question of terminology. Neither in Fortran nor in C++
    >are functions necessarily something that a mathematician would
    >consider a function. In both languages, they can have side
    >effects, and modify global state.


    The /evaluation/ of function calls has side effects in C++.

    In mathematics, the evaluation of a function term also /might/
    have side effects (for example, the mathematician calculating
    its value might break his pencil).

    The difference is that for C++ functions, some side-effects
    of the evaluation are be /specified/, while for mathematical
    functions they are never specified and only pure coincidence.

    Another less-known difference is the number of arguments:
    A mathematical function always needs at least one argument.

    Expressions, like »f()«, in C++ corresponds to entities called
    »operations« in a mathematical field called »universal algebra«.
    Stefan Ram, Apr 15, 2008
    #10
  11. rudra

    Stefan Ram Guest

    James Kanze <> quotes:
    >>Tiny point: as of C99 an explicit return from main is no longer
    >>required (though it is considered bad style to omit it).


    C++ predates this, so it is as of C++89:

    »If control reaches the end of main without encountering a
    return statement, the effect is that of executing return 0;«

    ISO/IEC 14882:1998(E), 3.6.1

    James Kanze <> writes:
    >It's also considered bad style to omit it in C++.


    I do not know, what could be harmful about it, so
    I do not consider it bad style.
    Stefan Ram, Apr 15, 2008
    #11
  12. rudra

    James Kanze Guest

    On 15 avr, 12:06, "Alf P. Steinbach" <> wrote:
    > * James Kanze:


    > > On Apr 14, 5:29 pm, "Alf P. Steinbach" <> wrote:


    > > [...]
    > >> But, who cares about C99?


    > > The C++ standards committee. C compatiblity is still considered
    > > de rigor for some aspects (preprocessor, basic types, etc.), and
    > > that means standard C. In other words, C99. (Thus, we will be
    > > getting extended integral types, vararg macros, etc., in the
    > > next version of the standard.)


    > Naturally, but in the (snipped) context of using a default
    > 'main' result value?


    > Relying on C99 rules in ordinary C programming is (still) just
    > a recipe for disaster, except for things where C99 codified
    > existing widespread practice.


    > At least for portable code.


    The same thing could be said about C++98. The "standard" way of
    handling templates would involve export.

    In practice, there are parts that have been widely adopted in
    both cases (e.g. long long, or member templates), and others
    which have been mainly ignored. Sometimes, it makes me wonder
    why bother having a standard at all.

    --
    James Kanze (GABI Software) email:
    Conseils en informatique orientée objet/
    Beratung in objektorientierter Datenverarbeitung
    9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
    James Kanze, Apr 15, 2008
    #12
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Guest
    Replies:
    0
    Views:
    670
    Guest
    Feb 25, 2004
  2. Eric
    Replies:
    2
    Views:
    1,405
    Tommy
    Feb 13, 2004
  3. ad
    Replies:
    2
    Views:
    711
  4. =?Utf-8?B?bWF2cmlja18xMDE=?=

    SetAuthCookie works some times and fails some times?

    =?Utf-8?B?bWF2cmlja18xMDE=?=, Mar 23, 2006, in forum: ASP .Net
    Replies:
    0
    Views:
    501
    =?Utf-8?B?bWF2cmlja18xMDE=?=
    Mar 23, 2006
  5. =?Utf-8?B?bWF2cmlja18xMDE=?=

    Forms Authentication Fails some times and not some times???

    =?Utf-8?B?bWF2cmlja18xMDE=?=, Mar 28, 2006, in forum: ASP .Net
    Replies:
    0
    Views:
    484
    =?Utf-8?B?bWF2cmlja18xMDE=?=
    Mar 28, 2006
Loading...

Share This Page