How to incorporate a function.

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • abrown07
    New Member
    • Jan 2010
    • 27

    How to incorporate a function.

    Hi,

    I have been doing more reading than necessary on functions and I still cannot figure out how to create the one I want to. and even if I could I do not know how to call upon it. SO I'm hoping i can get a little assistance on here. I need to create a function that will calculate the gravity factor for various planets. where the gravity factor = the mass of a planet/ mass of the earth divided by the radius of a planet/the radius of the earth. After i can create this function I dont understand how to call it when needed in my program. Please help!
  • whodgson
    Contributor
    • Jan 2007
    • 542

    #2
    You call a function by writing its name and identifying the value/s of its argument/s.
    For example:
    Code:
    double circle (double r);//declaration of function 'circle'
    const pi=3.4151;
    int main()
    {
       cout<<""enter circle radius";
       cin>>r; //input value for r
       cout<<"The area of circle of radius "<<r<<"is"<< circle(r)
               <<endl;//call function //circle(r).........also could have been written as circle (10)
    }//end main
    double circle(double r)//definition of function 'circle'
     {
        a=pi*r*r;
        return a;
    }

    Comment

    • abrown07
      New Member
      • Jan 2010
      • 27

      #3
      that seems simple enough but how do i do this for the function i need to create?

      Comment

      • whodgson
        Contributor
        • Jan 2007
        • 542

        #4
        We created the circle function above. Now its time for you to do something which is no more difficult. When you have succeeded post your code for our interest

        Comment

        • jkmyoung
          Recognized Expert Top Contributor
          • Mar 2006
          • 2057

          #5
          1. Figure out how you're going to calculate the answer, what equation you're using.
          2. Figure out all the constants, and make them constants in your program.
          3. Figure out all the unknowns you need to calculate the answer. These become the arguments in the function.
          4. Solve the equation for your specified variable, then program this equation in and plug in the arguments.

          Comment

          Working...