# ggggrrrr the functions

• 03-10-2005
cerin
ggggrrrr the functions
I gave up on functions before because I could never seem to get them to work so I figured its about time I learned. I'm making a calculator to solve all of the equations we are using in physics.
Code:

```#include <cstdlib> #include <iostream> using namespace std; float df(float appgrav=10, float time) { return ((.5*appgrav)*(time*time); } int main(int argc, char *argv[]) {                float acceleration;         float time;         float velocity;         float distance;         float speed;         float gravity=9.8;         float appgrav=10;         char begin;                 cout<<"What equation would you like to use? [d]istance fallen, [v]elocity,\n[h]horizontal distance, [a]verage velocity.";         cin>>begin;         if (begin=='d')         cout<<"Enter time:";         cin>>time;         df(appgrav,time);     system("PAUSE");     return EXIT_SUCCESS; }```
Really messed up.
• 03-10-2005
Zach L.
ii. You never assign the result of the computation to anything:
Code:

`float result = df(appgrav, time);`
• 03-10-2005
Brain Cell
- Don't get used to system(). Its a horrible function.

- you can save some typing by declaring your variables like this :
Code:

```float acceleration, time, velocity, distance; float speedm, gravity =9.8 , appgrav =10;```
they'd look prettier , too.
• 03-10-2005
hk_mp5kpdw
Code:

```float df(float appgrav=10, float time) { return (.5*appgrav)*(time*time); }```
If you are going to have a single default argument to a function taking two arguments like you are doing here, the default parameter must go with the second argument, like so:

Code:

```float df(float time,float appgrav=10) {     return (.5*appgrav)*(time*time); }```
• 03-10-2005
pianorain
Code:

```        if (begin=='d')         {             cout<<"Enter time:";             cin>>time;             cout << df(appgrav,time) << endl;         }```
• 03-10-2005
Krak
While you're at it, take away the useless params in main, and switch EXIT_SUCCESS to 0. You're cooler than that crap, aren't you?;)

And as was said earlier, don't use system() honey. As its name implies, it's system-dependent, and it might not run on someone else's machine. Something like 'cin.get()' might be a better choice, you see.
• 03-10-2005
cerin
The reason I used system was because my compiler automatically puts it there and I forgot to change it. I already understand why I shouldn't use system().

P.S. I got a compiler for my mac now yay!
• 03-10-2005
cerin
It doesn't do what I want it to if you work the formula d=.5gt^2 it is 125 with 5 as t and 10 as g, but my program returns 250.
• 03-11-2005
Dave Evans
Quote:

Originally Posted by cerin
It doesn't do what I want it to if you work the formula d=.5gt^2 it is 125 with 5 as t and 10 as g, but my program returns 250.

Here's how to debug your program yourself. If you get a wrong answer, make sure that the formula is working on whatever you think is. That is, put output statements at strategic places in the program that indicate the values that the formula is using.

So, for example, put the following statement in the beginning of your function:

Code:

`  cout << "In df: appgrav = " << appgrav << ", time = " << time << endl << endl;`
Here's another suggestion: since you indicate that you "could never seem to get functions to work", start with a simple case: no default arguments. Then make a simple program that shows whether the function can perform the calculation and return the correct value.

A first effort would be something like this:

Code:

```#include <iostream> using namespace std; float df(float appgrav, float time) {   cout << "In df: appgrav = " << appgrav << ", time = " << time << endl << endl;   return ((.5*appgrav)*(time*time)); } int main(int argc, char *argv[]) {   float time;   float gravity;   float distance;   gravity = 10;   time = 5;   distance = df(gravity, time);     cout << "distance = " << distance << endl;;   return 0; }```
Regards,

Dave