Question:
Basic C++ Question...I'm an absolute beginner?
lemonsTasteGood
2010-10-25 21:47:07 UTC
I was looking at 2 different tutorials for functions....

Link A:
http://www.cplusplus.com/doc/tutorial/functions/

Link B:
http://www.devarticles.com/c/a/Cplusplus/Beginners-Guide-to-Functions-in-C-plus/1/

They carry out the same function
Int Add( int a, int b)

but there are several differences, including the header in Link B, as well as the fact that B has a prototype for the function, while A doesn't.

I was wondering which tutorial is better to follow, and also, which is more recent.

Thanks
Five answers:
Joe Savage
2010-10-26 02:32:50 UTC
fstream is for dealing with files..



Both tutorials are good, however I personally would use http://www.dev-hq.co.uk/1-C++



It has good text lessons, and video lessons to work with the text lessons (to make sure you understand it all).
?
2010-10-26 13:00:43 UTC
Both tutorials are bad and look (to me) as poorly ported C language tutorials, but the second one is much worse than the first.



http://www.cplusplus.com/doc/tutorial/functions/



bad things:

1. creates uninitialized objects ('r' and 'z') only to overwrite them with actual values in the following lines.

2. defines variables before use (at the beginning of the function, even!)

3. uses parentheses in a return statement (okay, not really bad, just meaningless)

good things:

1. uses '\n' instead of the unfortunately common endl



http://www.devarticles.com/c/a/Cplusplus/Beginners-Guide-to-Functions-in-C-plus/1/

bad things:

1. uses fstream.h (what is this, 1990? It's not even a part of C++. Even if this was a real header file, it serves no purpose in the examples)

2. creates uninitialized objects only to overwrite them with actual values in the following lines.

3. uses endl instead of '\n'

4. uses global variables indiscriminantly.
modulo_function
2010-10-26 05:07:18 UTC
About prototypes.



C++ (and C) require that functions be defined before they are used. You can define your functions in order in the file so that this condition is met or you can put prototypes at the top and then you are free to define the functions later in the file. If you look at the code that does not use prototypes I think that you'll find that the functions are defined before main(..) right?
BobberKnob
2010-10-26 04:59:35 UTC
fstream is for dealing with files, which is what link b must be doing.



As far as the prototypes, you only need a prototype if the function is called before the definition. If you put the function definition above your main function, you don't need a prototype.
Al Red
2010-10-26 04:59:03 UTC
I REALLY like cplusplus.com--it's got the easiest reference articles when you're trying to figure out this library or that, when you want to know what standard functions are available to you.



I'll even use cplusplus.com to show you why linkB uses fstream.h instead of iostream.h! Usually, unless you're doing FILE input/output you don't really need fstream.h. BUT it basically includes everything in iostream.h as showing in cplusplus.com's little inheritance graph at the top of their fstream page: http://www.cplusplus.com/reference/iostream/fstream/



So, if you need stuff in iostream.h (cout, cin etc to talk to the user) you can use either fstream or iostream. But if you need to do FILE I/O you need fstream.


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...