Thread: New progragrammer needs help!

  1. #1
    Registered User
    Join Date
    Jun 2003
    Posts
    4

    New progragrammer needs help!

    This is my second day programming and I stumbled upon a fairly hard to understand code. Here it is

    Code:
    #include <iostream.h>
    
    int add (int x, int y)
    {
    
    	cout << "In Add(), recieved " << x << "and" << y << "\n";
    	return (x*y);
    }
    
    int main()
    {
    	cout << "Im in main()!\n";
    	int a, b, c;
    	cout << "Enter two numbers: \n";
    	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\n\n";
    	return 0;
    }
    Can someone try to explain what this code does in steps? and what is int a, b, c; is that a function or a variable?

    Thanks

    *edit* BY the way whats the difference between a variable and a function?
    Last edited by Shadowlink; 06-27-2003 at 08:22 PM.

  2. #2
    Registered User slaveofthenet's Avatar
    Join Date
    Apr 2003
    Posts
    80
    add is a function which takes two intergers and returns their sum. a, b, and c are all interger values. a, and b get their values from the user via cin, c is assigned to the value of add(a, b). A variable simply stores data. A function can return a value, in the type of a variable, but it can take arguments and manipulate data to get said value. A function does not need to return anything, it can be void.
    Last edited by slaveofthenet; 06-27-2003 at 08:26 PM.
    Detailed understanding of language features - even of all features of a language - cannot compensate for lack of an overall view of the language and the fundamental techniques for using it. - Bjarne Stroustrup

  3. #3
    Pursuing knowledge confuted's Avatar
    Join Date
    Jun 2002
    Posts
    1,916
    The code starts off by using cout to display "I'm in main()!". It then declares three int variables - a, b, and c. It displays a prompt to enter two numbers, and then inputs two numbers into the variables a and b. It then displays that it is calling the add() function. It calls add() with the parameters a and b. Add outputs that it is "in add()," and what parameters it received. It then MULTIPLIES the numbers together and returns that value (God, I hope that's a typo). This value is stored in the variable c back in main(). It then outputs "back in main", the value of c, and that it is exiting. main() returns 0 to the OS.
    Away.

  4. #4
    Registered User
    Join Date
    Jun 2003
    Posts
    4
    Yah I kinda am understanding it now, but Im still kinda unclear what the differnces between Variables and Functions are. Do they both produce values? And is a value data, like numbers?
    And what are int, void, main, char, float etc? And what stores the value?

    Thanks a lot.

    PS were any of you confused about these kinda stuff when starting out?

  5. #5
    Registered User
    Join Date
    Jan 2003
    Posts
    648
    Well, functions and variables are taught in algebra math classes, at least starting in 7-8th grade.

    Anyways, a variable is something that holds a value. A variable has a data type, a name and a value. For example:
    Code:
    int x = 3;
    This variable is called x, it has the value of 3, and its an int (an integer -- a positive/negative whole number).

    A function is a block of code that returns something. For example:
    Code:
    int square(int x)
    {
        return x * x;
    }
    This function takes in a parameter (x) and squares it, returning the value. Only void functions don't return anything. Here are some data types that you will most often see:

    int = positive/negative whole number
    float = a real number with decimals (if any)
    double = a more precise float
    char = a single ASCII character
    void = nothing, its not a data type, hence void lol

    Was I confused? No. Programming is just like math. If you know algebra, you're at home in programming, at least functional programming like VB/C/C++.

  6. #6
    Registered User
    Join Date
    Jun 2003
    Posts
    4
    hey thanks for all your help, I understand it now. I even made my self a addition program as proof, thanks a lot.

    Code:
    //My First Real program, email me: [email protected] or on aim: shadowlinksa
    #include <iostream.h>
    int main()
    {
    	cout << "Very Basic AddingProgram!\n"
    		 << "Adds 2 numbers togethor.\n"
    		 << "You wanna see? Good lets get started.\n\n\n"
    		 << "Type 2 numbers seperating them with a space,\n"
    		 << "then press enter...\n\n\n";
    	int a,b,c;
    	cin >> a;
    	cin >> b;
    	c=(a+b);
    	cout << "\nYour numbers were: " << a << " and " << b << "\n\n";
    	cout << "The sum of the number is:\t" << c << "\n";
    	cout << "\n\nThis proram was made by Shadowlinksa\nEmail: [email protected]\nAIM: shadowlinksa\n\n";
    		return 0;
    }
    What do you think? Not for a begginer uh? Only 3 days programming experience lol

  7. #7
    Registered User
    Join Date
    Nov 2002
    Posts
    1,109
    not bad. it may get frustrating at times, but remember we're here to help, and remember to have fun! good luck!

  8. #8
    Registered User
    Join Date
    Jun 2003
    Posts
    6
    parenthesis aren't necessaries

    you can use : c=a+b;

    and if you want your code to look like c++, you can put endl in place of '\n' like that :

    cout<<"hey"<<endl;

Popular pages Recent additions subscribe to a feed