Jump to content
  • Advertisement

Archived

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

spitfirz

im a beginner

This topic is 5591 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

hi everyone im jsut beginning to learn C++ and im confused about this tutorial code i coded with sams teachyourself c++ in 24 hours boook heres the code: #include <iostream.h> #include <stdlib.h> int Add (int x, int y) { std::cout << "In Add (), received " << x << " and " << y << "\n"; return (x+y); } int main() { std::cout << "I''m in main()!\n"; std::cout << "\nCalling Add() \n"; std::cout << "The value returned is: " << Add (3,4); std::cout << "\nBack in main ().\n"; return 0; system("PAUSE"); } can someone here do an analysis of how this code works like explain to me what each line does. thanks

Share this post


Link to post
Share on other sites
Advertisement
I would suggest you continue reading the book. It would take a poor beginner''s book indeed to fail to adequately explain its own example code. If you are still confused, please come back with more specific questions (what, exactly, do you not understand?).

Share this post


Link to post
Share on other sites
Hey man, I had the same problem when i started learning C++ with that book.

First of all, Add() is a function.

In int main(), Add(3,4) is call which is saying to pass the int value 3 and 4 into the function Add(). We pass 3,4 because Add() has to int parameters. We can replace 3 and 4 with other integers.

Once 3 and 4 has been pass, x becomes 3 and y becomes 4.

In the function Add(), It first display the value x and y which is 3 and 4.

Last it did a return(x+y);

As you see, Add returns int because it is

int Add(int x, int y)

so if it is int, it can return int. if it is float, it can return float.

float Add(float x, float y)

if it is void, then

void Add(int x, int y) , you cannot return anything since it is expecting void.

So by having return(x+y); it is return the sum of x and y which is 7.

Hopefully this helps.

Peace out

Share this post


Link to post
Share on other sites
Sams teach yourself is almost the worst book you can buy.
You can NOT possibly learn all their is to c++ in that amount of time. The book also doesn''t explain things well.
This code is so hairy that I can hardly make sense of it. It jumps around all over the place. I would just get a new book, or check out some tutorials. Sigh...I guess I need to start putting up a tutorial on c++ on my site. Check out http://www.demonrealmstudios.com/tut/vc.php in three days and I''ll have a real tutorial started.

Share this post


Link to post
Share on other sites
quote:
Original post by demonrealms
Sams teach yourself is almost the worst book you can buy.
You can NOT possibly learn all their is to c++ in that amount of time. The book also doesn''t explain things well.
This code is so hairy that I can hardly make sense of it. It jumps around all over the place. I would just get a new book, or check out some tutorials. Sigh...I guess I need to start putting up a tutorial on c++ on my site. Check out http://www.demonrealmstudios.com/tut/vc.php in three days and I''ll have a real tutorial started.


I dunno about 24 hours, but C++ in 21 days is one of the best books I''ve seen...

Share this post


Link to post
Share on other sites
Guest Anonymous Poster

...
return 0;
system("PAUSE";
}


What the heck is that? I wouldn''t trust this book.

Share this post


Link to post
Share on other sites
It pauses the program ("Press any key to continue. . .") before exiting. I don''t remember that in 21 days though...




-~-The Cow of Darkness-~-

If you see the image I am online

Share this post


Link to post
Share on other sites
I think AP was refering to the fact that the system function was being called after the return (ie. not at all).

Share this post


Link to post
Share on other sites
wow this forums answers fast. and after thinking about it hardi get it i was just wondering how the 3 n 4 got ther if it called it without having any intergers in the x n y. well i duno if ill go n buy more books but if anyone here has any good links to some free ebooks id appreciate it.

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.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!