Results 1 to 3 of 3

Thread: Help In Understanding Functions

  1. #1

    Thread Starter
    New Member
    Join Date
    Nov 2004
    Location
    New York
    Posts
    14

    Help In Understanding Functions

    Ok I Recently got into my book and i am up to functions and how they work and i am a bit confused maybe someone can explain better then my book is or use a better example.....

    Code:
    //sqrt.cpp -- use a square root function
    #include <iostream>
    #include <cmath>
    using namespace std;
    int main()
    {
         double cover;   //use double for real numbers
    
         cout << "How many square feet of sheets do you have?" << endl;
         cin >> cover;
         double side;   //create another variable
         side = sqrt(cover); // call function, assign return value
         cout << "You can cover a square side of " << side;
         cout << " feet\nwith your sheets.\n";
         cin.get();
         cin.get();
         return 0;
    }
    im not grasping how this works exactly......

  2. #2
    Fanatic Member twanvl's Avatar
    Join Date
    Dec 2001
    Posts
    771
    Do you know how mathematical functions work? In programming functions work roughly in the same way.

  3. #3
    PowerPoster Arc's Avatar
    Join Date
    Sep 2000
    Location
    Under my rock
    Posts
    2,336
    A function merely does something in a seperate part of code.

    For example,

    Code:
    int add2(int num){
    return num + 2;
    }
    
    int main(){
    int amount=0; //decalre original amount var
    int newAmount=0; //declare  new amount var
    
    newAmount=add2(amount);
    }
    So the function add2 simply adds 2 to the amount that was entered into the function and returns it to the variable you asign to it.

    So amount was equal to 0, it was then input into the add2 function which made it equal to 2. Now newAmount = 2.
    -We have enough youth. How about a fountain of "Smart"?
    -If you can read this, thank a teacher....and since it's in English, thank a soldier.


Posting Permissions

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



Click Here to Expand Forum to Full Width