View Full Version : I need help understanding....

10-03-2007, 11:42 PM
I got the book "Sam's Teach Yourself C++ in 21 days" and im near the end of the 2nd chapter and its talking about functions. I am having trouble understanding like here is the code it shows.
#include <iostream>

int Add (int x, int y)
cout << "In Add(), received" << x << " and " << y << "\n";
return (x+y);

int main()
cout << "Im in main()!\n";
int a,b,c;
cout << "enter 2 numbers: ";
cin >> a;
cin >> b;
cout << "\nCalling Add()\n";
cout << "\nBack in main().\n";
cout << "c was set to " << c;
cout << "\nExiting...\n\n";
return 0;

10-04-2007, 12:03 AM
I'll try to explain it as best I can:

#include <iostream> // This is needed for input and output, cin and cout

int Add (int x, int y) // This is a function declaration, int x and int y are two variables (integers) that are passed when calling the function
{ // Open function block
cout << "In Add(), received" << x << " and " << y << "\n"; // Outputs the text and the variables
return (x+y); // this returns x+y, i'll explain it in the call (it has to return an integer because the function type is int [ int Add ]
} // end function block

int main() // This is another function declaration, it has no paramenters and it is the one that the program starts in
cout << "Im in main()!\n"; // outputs I'm in main()!(newline)
int a,b,c; // declares the variables a b and c, needed to use variables
cout << "enter 2 numbers: "; // outputs enter 2 numbers:
cin >> a; // this lets the user input info, in this case the value for the integer a, and for this example it is set to 5
cin >> b;// this lets the user input info, in this case the value for the integer b, and for this example it is set to 4
cout << "\nCalling Add()\n"; // Outputs (newline)Calling Add()(newline)
c=Add(a,b); // This calls the function, a and b are the parameters for the function, this allows you to create dynamice functions, a and b need to be integers.
// a here = x in the function and b here = y in the function
// At the start c is set to the function. What this does is sets it to the returned value from the function, which in this case is 9.
// In Add(), received5 and 4(newline) will be outputted by the function
cout << "\nBack in main().\n"; // outputs stuff
cout << "c was set to " << c; // outputs stuff
cout << "\nExiting...\n\n"; // outputs stuff
return 0; // Even the main() function has to return a value (integer)

Sorry if that's a bit confusing...

10-04-2007, 02:31 AM
That was awesome I understood that. Just some you said was confusing like the c=Add(a,b) and what you said for it. Hard part I am getting is memorizing that when putting it on the ide.

10-04-2007, 03:25 PM
I am having hardtime understanding this codes and what they mean like c=Add(x,y) and so on....I looked at the websites on here and hey don't have ones where what the codes mean in definition..Anyone know any good ones?

This is more in reference to your previous topic. Appending it to that thread.
- Antoniohawk

10-04-2007, 05:20 PM
I'll try to reword it:

The value of Add(a,b) is what is returned by the function, a and b are the parameters and match up with Add (int x, int y) from when the function is created (a = x, b = y), and they have to match up unless a default is given (like Add (int x, int y = 2), if the second parameter isn't set it is automatically set to 2). Functions essentially execute a block of code so before the value of a+b is returned, the line cout << "In Add(), received" << x << " and " << y << "\n"; is run by the program. All functions end as soon as you return something so the function ends on the next line when it returns a+b. and c is set to a+b.

EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum