some questions on C++

R

rudra

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
 
F

Fei Liu

rudra said:
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
 
S

Stefan Ram

rudra said:
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;«.
 
B

Ben Bacarisse

Alf P. Steinbach said:
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).
 
J

Juha Nieminen

rudra said:
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.
 
J

James Kanze

rudra wrote:
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.)
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.
 
J

James Kanze

"Alf P. Steinbach" <[email protected]> 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.)
 
J

James Kanze

[...]
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.)
 
J

James Kanze

[...]
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.
 
S

Stefan Ram

James Kanze said:
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«.
 
S

Stefan Ram

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 said:
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.
 
J

James Kanze

* James Kanze:
[...]
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.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top