🔒 Locked
im a beginner
Advertisement
Original Post
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
You might want to post here.
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
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
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.
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.
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...
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-~-

If you see the image I am online
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.
quote:
original post by spitfirz
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.
try this first
w 3 schools
Cprogramming.com
Cplusplus.com
Regards.
EDIT: fixed a link
[edited by - pacman2003 on June 3, 2003 2:24:59 AM]
quote:
Original post by cowsarenotevilquote:
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]
Topic Locked
This topic has been locked by a moderator. New replies are not allowed.
Sign in to reply to this topic.
Advertisement