Thread: Lesson 4: Functions noob question

  1. #1
    Registered User
    Join Date
    Sep 2004
    Posts
    28

    Lesson 4: Functions noob question

    I dont understand this can somone explane it for me?
    Code:
    #include <iostream>
    
    using namespace std;
    
    int mult ( int x, int y );
    
    int main()
    {
      int x;                 
      int y;
      
      cout<<"Please input two numbers to be multiplied: ";
      cin>> x >> y;
      cin.ignore();
      cout<<"The product of your two numbers is "<< mult ( x, y ) <<"\n";
      cin.get();
    }
    
    int mult ( int x, int y )
    {
      return x * y;
    }
    the tutorial sort of lost me .


    plus i dont get the whole return thing....
    and what is cin exactly? the name seems to have nothing to do with what it does! cin.get? cin.ignore? and then they used cin>> x >> y!
    Last edited by NiVaG; 09-24-2004 at 03:51 PM.

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Sep 2004
    Posts
    28

    .

    ok ill do that next time.

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    Well see if you can do it this time by pressing the "edit" button at the bottom of your original post
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  5. #5
    Registered User
    Join Date
    Mar 2002
    Posts
    1,595
    Code:
    #include <iostream>  //use standard file
    
    using namespace std;  //use all of namespace std, not just parts of it
    
    int mult ( int x, int y );  //user declared function.  takes two ints and will return an int when called.  The return value may be ignored, or may be used.  The compiler must see the user declaration before there is a call to the function.  That means the declaration must come before main().  The definition of the function may come before or after main().  In this case it's after.
    
    int main()
    {
      int x; 
      int y;
    
      //request user input two integers
      cout<<"Please input two numbers to be	multiplied: ";  
     
      //store one in x and one in y
      cin>> x >> y;
     
      //ignore the newline char left in the input buffer after the values of x and y were extracted
      cin.ignore();
     
      //call the function, passing x and y
      //store the return value in a temp variable
      //and print the return value on the screen
      cout<<"The product of your two numbers is "<< mult ( x, y ) <<"\n";
     
      //don't close the program until I enter a char
      //of some sort.  This way I can see the outcome of my efforts.
      cin.get();
     
      //the standard says you don't have to explicitly indicate a return value from main(), but it's not a bad idea.
    }
    
    //define the function you declared earlier and called in main()
    int mult ( int x, int y )
    {
       //return the product of x times y
       return x * y;
    }

  6. #6
    Registered User
    Join Date
    Sep 2004
    Posts
    28
    awsome! that helped alot but i have a little bit more

    like when you said "ignore the newline char left in the input buffer after the values of x and y were extracted"

    why is there a newline i never saw /n? whats a char? whats a input buffer? and when/why/how were they extracted?



    and how can it tell me what the product of my two numbers is if its only returned after the whole things finished?

    and whats defined, and how would it help after its basicly over ?

    i know im hopeless...

  7. #7
    Hardware Engineer
    Join Date
    Sep 2001
    Posts
    1,398
    The structure & syntax...

    This is the function prototype. It tells the compiler the name of the function, the type of return value, and the types & quantity of values passed-in.
    Code:
    int mult ( int x, int y );

    This is the function definition. It's the "real code" for the function.
    Code:
    int mult ( int x, int y )
    {
    return x * y;
    }

    mult(x,y) is the function call. It "runs" the function.
    Code:
    cout<<"The product of your two numbers is "<< mult ( x, y ) <<"\n";
    Code:
    Z = mult((x, y);  // Another way to call the function

    A function is...
    Something like a mini-program...
    Here, you are running main(), and you get to the function-call, and the program-flow jumps to the mult() function. When the function ends, it returns back to main, and it brings-back a value... an int which is x * y.

    You can call a function as many times as you want. In fact, this is (mostly) why they are used. You only have to include the code for your function once, but you can call it many times. No one would actually write a function like the example. There is no need for a multiply function, since multiplication is built-into C++.

    cin is for "C Input" (from keyboard, etc)
    cout is for "C Output" (to display, etc)

    The extra cin.get() is there to make the program work on a Windows system. It just keeps your program from ending and closing the window before you can read the output.


    A few more notes & comments...
    You can pass as many values into a function as you wish, but a function can only return one value. (There are ways using pointers and references to affect more than one variable.)

    Make sure you understand functions. Everything in C++ is done with functions!

    The variable names are optional in the function prototype and are ignored by the compiler. Sometimes the names are helpful to us humans if they are meaningful... "Width", "Height", etc.
    Code:
    int mult( int , int );//  This is OK
    int mult(int Number1, int Number2); // This is OK too

    The variable names passed-into the function can be different than the names inside the function. Only the value is passed-in, not the actual variable.
    Code:
    cout<<"The product of your two numbers is "<< mult ( A, B ) <<"\n";  // This will work if A & B are defined ints
    cout<<"The product of your two numbers is "<< mult ( 2, 4 ) <<"\n"; // This will work too

  8. #8
    Hardware Engineer
    Join Date
    Sep 2001
    Posts
    1,398
    why is there a newline i never saw /n?
    When you hit [ENTER] you get a newline, but you don't automatically see it. (You see the results of it.) You can make your program see it!



    whats a char?
    It's a variable type that can represent an ASCII character.



    whats a input buffer? and when/why/how were they extracted?
    It's a little secret place in memory where the keystrokes go! It's a FIFO (First-In-First-Out). cin reads/extracts the input buffer.



    and how can it tell me what the product of my two numbers is if its only returned after the whole things finished?
    Only the function is "over". The product is returned to main where it's displayed via cout.



    and whats defined, and how would it help after its basicly over ?
    The compiler takes care of that. It has already compiled the function. So when you run the program, it knows what to do when the function is called. The placement/sequence of the function definition in the source code isn't important. Was that the question?

  9. #9
    Registered User
    Join Date
    Sep 2004
    Posts
    28

    ok.ok

    yeah! awsome, i think i understand now.

    last thing is if the compiler figures it all out before and it dosn't need to go in order why is return always at the end? and why does return 0 mean success?
    Last edited by NiVaG; 09-24-2004 at 05:50 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. pointer functions - nebie question
    By bbresc512 in forum C Programming
    Replies: 3
    Last Post: 03-24-2006, 01:36 PM
  2. Question about the introduction to pointers lesson
    By Freemind11 in forum C++ Programming
    Replies: 11
    Last Post: 02-15-2006, 11:45 PM
  3. Question about functions
    By richdb in forum C Programming
    Replies: 2
    Last Post: 01-22-2006, 01:18 AM
  4. question about c functions
    By LowLife in forum C Programming
    Replies: 1
    Last Post: 11-16-2005, 12:20 PM
  5. .h and functions question
    By Mick D in forum C++ Programming
    Replies: 5
    Last Post: 10-21-2005, 12:53 AM