Thread: Understanding C++?

  1. #1
    Novice.
    Join Date
    Oct 2005
    Posts
    88

    Understanding C++?

    I'm just new to programming in general and I've just started the C++ tutorial but I'm not really sure if I understand what the code does..

    Code:
    #include <iostream>
    
    using namespace std;
    
    float function ( float x, float y );
    
    int main()
    {
      float x;
      float y;
      
      cout<<"Please input two numbers to be multiplied: ";
      cin>> x >> y;
      cin.ignore();
      cout<<"The product of your two numbers is "<< function ( x, y ) <<"\n";
      cin.get();
    }
    
    float function ( float x, float y )
    
    {
        return x * y;
    
      
    }
    Does
    Code:
     #include <iostream>
    acces a libary? And if so what/what kind of libary?

    And what does
    Code:
    using namespace std;
    do? Does it acces a certain part of the libary iostream?

    Code:
     int main()
    sends a 0 (succes) or a 1 (failure) to the operating system. But what does () do?

    And I dont really understand the chapter about Functions (I'm 15 still getting English at school) located here: http://www.cprogramming.com/tutorial/lesson4.html.

    Can someone explain this part to me in a bit more understandeble English together with the programming terms so that i know what they mean?

    Thank you.

    (PS: I hope this post complies to the general forum rules.)

  2. #2
    C++ Enthusiast jmd15's Avatar
    Join Date
    Mar 2005
    Location
    MI
    Posts
    532
    First off, welcome to the forums, there is a big age range in here but don't worry about it, I'm 15 as well. Now for your questions. When you include iostream you are accessing a that library which defines many functions, types, etc. Using namespace std means you are using the objects or functions of std. It's a little difficult to explain but if you didn't include that line you would have to use the cout function(and others) like this:
    Code:
    std::cout<<"Hi";
    Since you are using the std namespace it knows where to look to find the definition of that function. If you didn't use that namespace then it would come up with an error because it wouldn't know where to look to find that functions definition. int main() does mean it will return a value on exit and the () means it has no parameters. You can use int main with parameters to accept command line arguments, search google for tutorials on that. Functions are just sets of code that are executed when called. Like that function you posted above, it will take in 2 parameters(the numbers to be multiplied) and it returns the value of those two being multiplied together. I think I covered everything. Good luck.

    EDIT:
    Oh yeah, that part:
    Code:
    float function ( float x, float y );
    Is a prototype, meaning that it is just letting the compiler know that the function will be defined later on in the code. The float in front of it, means it will return a float value(it also takes in 2 float values to multiply together).
    Last edited by jmd15; 10-31-2005 at 02:56 PM. Reason: Additional Information
    Trinity: "Neo... nobody has ever done this before."
    Neo: "That's why it's going to work."
    c9915ec6c1f3b876ddf38514adbb94f0

  3. #3
    Registered User Daniel Primed's Avatar
    Join Date
    Jun 2005
    Posts
    13
    Just to add my little bit in here:

    Code:
    #include<iostream>
    iostream itself allows the program to iput and output. It stands for in and out stream. So that allows you to use cin and cout. That is c in and c out.

    You'll also probably start to see other headers such as:

    Code:
    #include<cstring>
    Which allows you to use string functions such as strlen, ctrcmp etc.

    The tutorials should talk about the header files, I have done the beginner ones and they have mentioned it before.

  4. #4
    C++ Enthusiast jmd15's Avatar
    Join Date
    Mar 2005
    Location
    MI
    Posts
    532
    Yes, and that brings me to another point. You notice how in #include <iostream> the iostream doesn't have a .h after it while others do? Well the ones without the .h mean they are standard C++ headers, the ones without aren't standard but that doesn't mean there is anything wrong with them.
    Trinity: "Neo... nobody has ever done this before."
    Neo: "That's why it's going to work."
    c9915ec6c1f3b876ddf38514adbb94f0

  5. #5
    Registered User
    Join Date
    Apr 2003
    Posts
    2,663
    If you are just beginning c++, don't worry about what these lines mean:
    Code:
    #include <iostream>
    
    using namespace std;
    Just put them at the top of every program.

    Briefly, <iostream> is a library of functions your program needs and the library will be inserted into your program at the spot of the #include statement. If you make a simple Hello World program, and then check the file size, you will see that the file size is fairly large. That's because the standard library which is inserted in your program is big.

    A namespace prevents name clashes. If two variables have the same name in your program, you will get an error. Since you are including a standard library, the variable names you choose for your program could be the same as the variable names in the standard library. In order to prevent that, the standard libraries are enclosed in their own "namespace". That means all the variables in the standard libraries have an additional name tacked onto the front of them, e.g.

    std::someVariableName

    So, as long as you don't choose variable names that start with "std::", you won't have a name clash.

    Now, to use the variables and functions in the standard libraries, you have to use their full name, e.g.

    std::variableName

    But having to type "std::" in front of the names when you use them in your program can be a chore, so you can employ a using declaration instead:

    using namespace std;

    That says that from now on, all the names in the standard library no longer have to be preceded by "std::". If you leave the using declaration out of your program, you'll see you have to do this:

    std::cout<<"hello world";

    to make your program work. Of course, when you employ a using declaration in your program, the name clash problem presents itself again. However, when you are beginning don't worry about those potential name clashes. Just don't name any of your variables "cout" or "cin" and you'll be fine.

    (....well, not so brief.)
    Last edited by 7stud; 10-31-2005 at 09:27 PM.

  6. #6
    Registered User
    Join Date
    Apr 2003
    Posts
    2,663
    int main()

    sends a 0 (succes) or a 1 (failure) to the operating system. But what does () do?
    No, it does not send a 0 or a 1 to the operating system. All you can tell from that statement is that an int type will be returned to the operating system. Whatever is in the return statement is what gets returned to the operating system.

    Between the "()" is where you declare the parameters of a function--in this case there are none.


    Here is a very simple function:
    Code:
    #include <iostream>
    using namespace std;
    
    void showNumber()
    {
       cout<<100<<endl;
    }
    
    int main ()
    {
       showNumber();
    
       return 0;
    }
    Functions must specify a 'return type', and the return type precedes the function name. In the function above, there is no return value, and that is designated by 'void'. The function is 'called' in main() by this line:

    showNumber();

    That says, "please go find a function named showNumber, and execute the lines of code contained therein."

    Here is another example:
    Code:
    #include <iostream>
    using namespace std;
    
    void showNumber(int a)
    {
       cout<<a<<endl;
    }
    
    int main ()
    {
       showNumber(100);
       showNumber(25);
    
       return 0;
    }
    Now, instead of always displaying the same number, you can send the function a specific number, and the function will display whatever number you send it. Once again, the function does not return a value, so its return type is 'void'. You should note that the type of the number you send to the function must match the function 'parameter'. The function parameter above is "int a", which means the function is expecting an int type to be 'sent' to it. You send a value to a function like this:

    showNumber(100);

    To send a value to a function, you just put the value between the parentheses after the function name. The function parameter "int a" creates an integer variable named "a" which stores the value you send to the function--that's why the type of the value you send has to match the function parameter. Then, inside the function you can use the variable "a" by referring to it by name:

    cout<<a<<endl;

    Here is another example:
    Code:
    #include <iostream>
    using namespace std;
    
    int addTen(int a)
    {
       int answer = a + 10;
       return answer;
    }
    
    int main ()
    {
       int result1 = addTen(2);
       int result2 = addTen(5);
    
       cout<<"Here are the results: "<<result1<<" "<<result2<<endl;
       
       return 0;
    }
    This time the function returns a value. The return type preceding the function name must match the type of the value in the 'return statement'. The return statement replaces the function call in main(), e.g. addTen(2), with the return value. So, result1 is assigned 12, and result2 is assigned 15.

    To summarize:
    1) A function must list a return type before its name.
    2) The return type must match the type of the value in the return statement--if there is no return statement, then the type is 'void'.
    3) When you call a function, the value you send it must match the function parameter.
    4) The function call in main() is replaced by the function return value.
    5) Sometimes if a function doesn't have any parameters, like in the first example, "void" will be used for the parameter. For instance you might see the function in the first example written like this:
    Code:
    void showNumber(void)
    {
       cout<<100<<endl;
    }
    (void) is equivalent to ( )
    Last edited by 7stud; 10-31-2005 at 09:18 PM.

  7. #7
    Novice.
    Join Date
    Oct 2005
    Posts
    88
    Ok so if i understand correctly a function:

    1. Is a predefined piece of code which you can put into your program.

    2. Can be called from a libary which is already included in the compiler. (like cout and cin)

    3. You can "call" a function which means that is is activated/accesed

    4. You can create functions yourself in your program which you can call as well.

    5. A function should return something unless there is void in front of it.
    Only why does it has to return something? And what does it return to?
    Is it returned to be called later?

    What does the
    Code:
    return 0;
    do? does it return information of the function main?

    And what is exactly meant by declaring? Saying that something exists?

    And a statement would that be: If this = something do that?

    e.g
    Code:
    for ( variable initialization; condition; variable update )
    Thanks
    Last edited by omnificient; 11-01-2005 at 02:23 PM.

  8. #8
    C++ Enthusiast jmd15's Avatar
    Join Date
    Mar 2005
    Location
    MI
    Posts
    532
    When you call a function that doesn't return anything you don't put a void in front of it when calling it, only when defining/prototyping it. return 0; means for your main function(which is int main()) to return a 0 which when main returns it terminates the program. It returns 0 just like a function you define would return a value, so think of int main as one big function and if that function returns, it stops execution.
    Trinity: "Neo... nobody has ever done this before."
    Neo: "That's why it's going to work."
    c9915ec6c1f3b876ddf38514adbb94f0

  9. #9
    Registered User
    Join Date
    Apr 2003
    Posts
    2,663
    5. A function should return something unless there is void in front of it.
    Only why does it has to return something?
    It doesn't. It depends on what you want the function to do. Here are some examples:
    Code:
    #include <iostream>
    
    using namespace std;
    
    void greeting()
    {
    	cout<<"Hello."<<endl;
    }
    
    double calculateAverage(int anArray[], int arraySize)
    {
    	int total = 0;
    
    	for(int i = 0; i < arraySize; i++)
    	{
    		total = total + anArray[i];
    	}
    
    	double avg = total/arraySize;
    
    	return avg;
    }
    
    int main()
    {
    
    	greeting();
    
    	int myArray[3] = {1, 2, 3};
    	
    	double twoTimesAverage = 2 * calculateAverage(myArray, 3);
    	
    	cout<<twoTimesAverage<<endl;
    
    	return 0;
    }
    5. A function should return something unless there is void in front of it...And what does it return to? Is it returned to be called later?
    From my earlier post:
    This time the function returns a value. The return type preceding the function name must match the type of the value in the 'return statement'. The return statement replaces the function call in main(), e.g. addTen(2), with the return value. So, result1 is assigned 12, and result2 is assigned 15.
    For example, in this line:
    Code:
    double twoTimesAverage = 2 * calculateAverage(myArray, 3);
    The term calculateAverage(myArray, 3) is replaced by the return value of the function, which in the example code is 2. So, the statement becomes:

    double twoTimesAverage = 2 * 2;

    and therefore 4 is assigned to the variable twoTimesAverage.
    Last edited by 7stud; 11-05-2005 at 04:20 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need help understanding a problem
    By dnguyen1022 in forum C++ Programming
    Replies: 2
    Last Post: 04-29-2009, 04:21 PM
  2. Understanding Headers
    By AeonMoth in forum C++ Programming
    Replies: 2
    Last Post: 06-27-2007, 05:53 AM
  3. trouble understanding the source file structure
    By Mario F. in forum C++ Programming
    Replies: 5
    Last Post: 05-26-2006, 06:46 PM
  4. Replies: 8
    Last Post: 10-24-2005, 11:26 AM
  5. understanding recursive functions
    By houler in forum C Programming
    Replies: 7
    Last Post: 12-09-2004, 12:56 PM