Jump to content
  • Advertisement

Archived

This topic is now archived and is closed to further replies.

gamechampionx

Help with C++

This topic is 6139 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I''m new to C++, and I need some help. First, what''s the difference between int main, void mani, and int main(void)? Next, what the hell does return 0 or whatever mean? Lastly, how does passing stuff, and calling other procedures work. I do understand much of the syntax, however. BTW, I''m only 15, I''m not a complete idiot.

Share this post


Link to post
Share on other sites
Advertisement
Guest Anonymous Poster
When you make a function it''s put in the following format:


Return type, Function Name, Input Types and variable names.

So lets get into what a type is: int, char, short, and long are just a few type names. If your function doesn''t need to return a value then you use void. The void type pretty much means "nothing". So lets look at the functions you listed

int main(void) This means that the function called main returns an integer, but takes in no values.

void main(void) This means that the function called main returns nothing and takes in no values.

Return is a keyword. It ends the function and returns what ever value you give it. In the case you listed below "return 0;" it returns 0 from a function. However you can not use return if your return value on the function is void.

How do you pass stuff?

  

// Function prototypes NOTE: All functions need a prototype but main.


// This function will take in an integer value and square it.

int Square(int origionalvalue);

int main(void)
{
int test = 2;

cout << "The current value of our test variable is : " << test << endl;

cout << "Now squaring the test variables value." << endl;
test = Square(test);

cout << "The squared value is : << test << endl;

return 0;

}

int Square(int origionalvalue)
{
return ( origionalvalue * origionalvalue);
}


Share this post


Link to post
Share on other sites
1)
void main(void) - declares the fuction main with no return value,
and no parsed values to it.

int main - equals to "int main(int)"
returns an integer and gets one integer.

int main(void) - function returns int, no value parsed

2)
return 0;
passes the value ''0'' to and then returns to the code that called the function.

3)
lets say you wanna call "int add(int a, int b)"
The function returns a+b:

  
int add (int a, int b)
{
return a+b;
}

// this could be anywhere in your code:

void main(void)
{
printf("%d + %d = %d\n", 1, 2, add(1, 2));
}


HTH
/Fredrik Olsson

Share this post


Link to post
Share on other sites
quote:
Original post by Anonymous Poster
However you can not use return if your return value on the function is void.


Not quite. You can use return, just not return a value.

void foo(void)
{
return;
}


Gamechamp, invest in a C++ book. Check out the faq for comp.lang.c++ for good titles. C++ FAQ LITE

Share this post


Link to post
Share on other sites
int main()
int main( void )
void main()

First of all this are possible declerations of the
main function in C/C++. There are also other ones for
getting the args you pass with the commandline.

The first two of the function declarations listed above
are equal, just an alternative syntax, but I would
prefer writing () if you mean you don''t need any params,
however this is a matter of style.

The int means the function returns an int,
for the main function this would be treated as the error code.
Under MS-DOS for example you could check the error code in a batch file.

But as far as I can say that, I would suggest for your programs
void main() would be sufficiant.

Then you don''t need to write something like "return 0;" this
is often stupid because it always returns 0 so theres no fun,
except you return/exit before.

It only really makes sense to use a return value if this value is somehow variable and not always the same ...

So this should be enough for now.
But do yourself a favour and by a book on C/C++ or search for
some tutorials on the web or whatever ( no attack, just an advice ).

Share this post


Link to post
Share on other sites
AP:
// Function prototypes NOTE: All functions need a prototype but main.// This function will take in an integer value and square it.

Not really true at all.
If the function is defined before its call you wont need a
prototype for it ...

I just wanted to add that because for writing some really simple
programs at the beginning of your C++ career it doesn''t come in
handy to have a prototype for the only tiny function the proram is all about ...

Share this post


Link to post
Share on other sites
I do have a book, but my friend has it. Anyway, thanks for the help. BTW, I need help with something. I made a cool program, but when it finishes execution, it closes itself, and you can''t see the result. How do you make it stay open until the user closes it? BTW, it''s a Win-32 console application using void main().

Share this post


Link to post
Share on other sites
quote:
Original post by gamechampionx
I made a cool program, but when it finishes execution, it closes itself, and you can't see the result. How do you make it stay open until the user closes it?
You have to use something called a "loop." I seriosly suggest you pick up the book "Sams Teach Yourself C++ in 21 Days" and read away. I don't know how much you know, so I can't exactly give you an example.

If you cannot get that book, go here for a book/tutorial for free.

Good luck!

------------------------------
Simple DirectMedia Layer:

Main Site - (www.libsdl.org)
Cone3D Tutorials- (cone3D.gamedev.net)
GameDev.net's Tutorials - (Here)

OpenGL:

Main Site - (www.opengl.org)
NeHe Tutorials - (nehe.gamedev.net)
Online Books - (Red Book) (Blue Book)


Edited by - Drizzt DoUrden on February 23, 2002 9:39:05 PM

Share this post


Link to post
Share on other sites
According to ANSI, main should always take the form: int main(int argc, char* argv[]) (or, equivalently int main(int argc, char* argv[])). Most compilers will let you get away with having a void return type on it or taking no arguments, but will at least give you a warning. And you should make sure you return EXIT_SUCCESS or EXIT_FAILURE (defined in stdlib.h) from main, suppressing all inclinations to return hard-coded values like, say, 0.

Share this post


Link to post
Share on other sites
For example you could use getch() ( the name should be similar to that ) declared in the nonstandart header conio.h .
This tries to read in a character and if there''s no character
waiting in the standart input stream it waits until you typed some usually. But be warned the behaviour is not defined by the standart ''cause it is no standart function.

But if the one implmented in your library acts like the one I
described above, this is an easy way to accomplish the task you
want to do.

An other way for instance is to start the console app in a DOS-box.

Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!