Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 5 of 5
  1. #1
    New Coder
    Join Date
    Sep 2007
    Posts
    20
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Question 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 <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";
    c=Add(a,b);
    cout << "\nBack in main().\n";
    cout << "c was set to " << c;
    cout << "\nExiting...\n\n";
    return 0;
    }

  • #2
    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 <iostream> // This is needed for input and output, cin and cout

    int Add (int xint 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" << << " and " << << "\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)

  • #3
    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.

  • #4
    New Coder
    Join Date
    Sep 2007
    Posts
    20
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Cool 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.

  • #5
    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.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •