Skip to main content
gamedev.net
🔒 Locked

im a beginner

Started by spitfirz Jun 2, 2003 at 7:08 PM 11 replies 1.1k views
Original Post
spitfirz
spitfirz
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
Miserable
Miserable
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?).
dangerduo
dangerduo
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
demonrealms
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.
cowsarenotevil
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...
-~-The Cow of Darkness-~-
cowsarenotevil
cowsarenotevil
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
-~-The Cow of Darkness-~-
ISOPimp
ISOPimp
I think AP was refering to the fact that the system function was being called after the return (ie. not at all).
spitfirz
spitfirz
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.
Marjolein
Marjolein
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]
spitfirz
spitfirz
its from the book but the explanation they gave i didnt quite understand.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.