Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > some questions on C++

Reply
Thread Tools

some questions on C++

 
 
rudra
Guest
Posts: n/a
 
      04-14-2008
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
 
Reply With Quote
 
 
 
 
Fei Liu
Guest
Posts: n/a
 
      04-14-2008
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
 
Reply With Quote
 
 
 
 
Stefan Ram
Guest
Posts: n/a
 
      04-14-2008
rudra <(E-Mail Removed)> 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;«.

 
Reply With Quote
 
Ben Bacarisse
Guest
Posts: n/a
 
      04-14-2008
"Alf P. Steinbach" <(E-Mail Removed)> 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.
 
Reply With Quote
 
Juha Nieminen
Guest
Posts: n/a
 
      04-14-2008
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.
 
Reply With Quote
 
James Kanze
Guest
Posts: n/a
 
      04-15-2008
On Apr 14, 4:10 pm, "Victor Bazarov" <(E-Mail Removed)> 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:(E-Mail Removed)
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
 
Reply With Quote
 
James Kanze
Guest
Posts: n/a
 
      04-15-2008
On Apr 14, 5:21 pm, Ben Bacarisse <(E-Mail Removed)> wrote:
> "Alf P. Steinbach" <(E-Mail Removed)> 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:(E-Mail Removed)
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
 
Reply With Quote
 
James Kanze
Guest
Posts: n/a
 
      04-15-2008
On Apr 14, 5:29 pm, "Alf P. Steinbach" <(E-Mail Removed)> 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:(E-Mail Removed)
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
 
Reply With Quote
 
James Kanze
Guest
Posts: n/a
 
      04-15-2008
On Apr 14, 11:55 pm, Juha Nieminen <(E-Mail Removed)> 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:(E-Mail Removed)
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
 
Reply With Quote
 
Stefan Ram
Guest
Posts: n/a
 
      04-15-2008
James Kanze <(E-Mail Removed)> 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«.

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
How can I restrict that the some ID can only login once in the some time ad ASP .Net 2 08-12-2005 09:14 PM
Re: Questions....questions....questions Patrick Michael A+ Certification 0 06-16-2004 04:53 PM
Need some help for some perl homework.... Perl 0 02-25-2004 01:45 AM
Forms Authentication question: How to have some pages open and some requiring forms authentication Eric ASP .Net 2 02-13-2004 02:14 PM
Some questions regarding 070-305 and hopefully some right answers. Needs correction... wink, wink ;-) Daniel Walzenbach MCSD 1 11-10-2003 12:25 AM



Advertisments