Archived

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

im a beginner

This topic is 5305 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
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
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
quote:
Original post by cowsarenotevil
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...


24 hours is almost the same book, I own it and it's pretty good. It DOES explain the example code. I think the people who say this book is the worst you can buy, have never read it. (Or the Dutch translation is different from the original book )
And sure you can't learn C++ in that amount of time, but you can learn as slow/fast as you want. That the title says 24 hours, doesn't mean you must finish it in 24 hours...

I thought the starter of this topic meant to say: I coded with Teach yourself C++ in 24 hours, and I found this tutorial code and it's syntaxis is quite a bit different from the code I've learned from the book, so can someone explain it to me.



[edited by - Marjolein on June 3, 2003 3:11:23 PM]

Share this post


Link to post
Share on other sites