Flash Website Builder- Trendy Site Builder is a Flash Site Building tool that helps users build stunning websites. Check Out Custom Custom Logo Design by LogoBee. Website Design and Free Logo Templates available.
 CodingForums.com I need help understanding....

Before you post, read our: Rules & Posting Guidelines

Enjoy an ad free experience by logging in. Not a member yet? Register.
 10-03-2007, 11:42 PM PM User | #1 xsubxwooferx New Coder   Join Date: Sep 2007 Posts: 20 Thanks: 1 Thanked 0 Times in 0 Posts I need help understanding.... 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 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"; c=Add(a,b); cout << "\nBack in main().\n"; cout << "c was set to " << c; cout << "\nExiting...\n\n"; return 0; }
 10-04-2007, 12:03 AM PM User | #2 Mwnciau Regular Coder   Join Date: May 2006 Location: Wales Posts: 820 Thanks: 1 Thanked 82 Times in 79 Posts I'll try to explain it as best I can: PHP Code: ``` #include  // 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...
 Users who have thanked Mwnciau for this post: xsubxwooferx (10-04-2007)
 10-04-2007, 02:31 AM PM User | #3 xsubxwooferx New Coder   Join Date: Sep 2007 Posts: 20 Thanks: 1 Thanked 0 Times in 0 Posts 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 PM User | #4 xsubxwooferx New Coder   Join Date: Sep 2007 Posts: 20 Thanks: 1 Thanked 0 Times in 0 Posts Websites for syntax! 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? Edit: This is more in reference to your previous topic. Appending it to that thread. - Antoniohawk Last edited by Antoniohawk; 10-04-2007 at 06:12 PM..
 10-04-2007, 05:20 PM PM User | #5 Mwnciau Regular Coder   Join Date: May 2006 Location: Wales Posts: 820 Thanks: 1 Thanked 82 Times in 79 Posts I'll try to reword it: Code: `c=Add(a,b);` 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.

 Bookmarks

 Thread Tools Rate This Thread Rate This Thread: 5 : Excellent 4 : Good 3 : Average 2 : Bad 1 : Terrible

 Posting Rules You may not post new threads You may not post replies You may not post attachments You may not edit your posts BB code is On Smilies are On [IMG] code is On HTML code is Off Forum Rules
 Forum Jump User Control Panel Private Messages Subscriptions Who's Online Search Forums Forums Home :: Client side development     JavaScript programming         DOM and JSON scripting         Ajax and Design         JavaScript frameworks         Post a JavaScript     HTML & CSS     XML     Flash & ActionScript         Adobe Flex     Graphics and Multimedia discussions     General web building         Site reviews         Building for mobile devices :: Server side development     Apache configuration     Perl/ CGI     PHP         Post a PHP snippet     MySQL         Other Databases     Ruby & Ruby On Rails     ASP     ASP.NET     Java and JSP     Other server side languages/ issues         ColdFusion         Python :: Computing & Sciences     Computer Programming     Computer/PC discussions     Geek News and Humour Web Projects and Services Marketplace     Web Projects         Small projects (quick fixes and changes)         Medium projects (new script, new features, etc)         Large Projects (new web application, complex features etc)         Unknown sized projects (request quote)         Vacant job positions         Looking for work/ for hire         Project collaboration/ partnership         Paid work offers and requests (Now CLOSED)     Career, job, and business ideas or advice     Domains, Sites, and Designs for sale         Domains for sale         Websites for sale         Design templates and graphics for sale :: Other forums     Member Offers     Forum feedback and announcements

All times are GMT +1. The time now is 11:54 PM.