1 / 27

Computer Science 1620

Computer Science 1620. Default Parameter Values. Default Parameters If a projectile is launched vertically with velocity v 0 , the maximum height it will reach is given by: g is the downward gravitational acceleration on Earth, g is roughly 9.81 m/s 2 at sea level

marilu
Download Presentation

Computer Science 1620

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Computer Science 1620 Default Parameter Values

  2. Default Parameters • If a projectile is launched vertically with velocity v0, the maximum height it will reach is given by: • g is the downward gravitational acceleration • on Earth, g is roughly 9.81 m/s2 at sea level • write a function that takes an initial velocity, and returns the height of the projectile g v0

  3. #include <iostream> using namespace std; double height(double v0) { const double G = 9.81; return v0 * v0 / (2 * G); } int main() { cout << "v0 = 5 m/s, height = " << height(5.0) << "m" << endl; cout << "v0 = 10 m/s, height = " << height(10.0) << "m" << endl; cout << "v0 = 15 m/s, height = " << height(15.0) << "m" << endl; cout << "v0 = 20 m/s, height = " << height(20.0) << "m" << endl; cout << "v0 = 25 m/s, height = " << height(25.0) << "m" << endl; return 0; }

  4. Suppose your boss wants this function to work on the moon as well • gravitational acceleration on moon = 1.62 m/s

  5. #include <iostream> using namespace std; double height(double v0) { const double G = 1.62; return v0 * v0 / (2 * G); } int main() { cout << "v0 = 5 m/s, height = " << height(5.0) << "m" << endl; cout << "v0 = 10 m/s, height = " << height(10.0) << "m" << endl; cout << "v0 = 15 m/s, height = " << height(15.0) << "m" << endl; cout << "v0 = 20 m/s, height = " << height(20.0) << "m" << endl; cout << "v0 = 25 m/s, height = " << height(25.0) << "m" << endl; return 0; }

  6. Suppose your boss wants to be able to calculate for both moon and Earth in same program • Solution 1: write two separate functions

  7. #include <iostream> using namespace std; double earth_height(double v0) { const double G = 9.81; return v0 * v0 / (2 * G); } double moon_height(double v0) { const double G = 1.62; return v0 * v0 / (2 * G); } int main() { cout << "v0 = 5 m/s, earth = " << earth_height(5.0) << "m" << endl; cout << "v0 = 5 m/s, moon = " << moon_height(5.0) << "m" << endl; cout << "v0 = 28 m/s, earth = " << earth_height(28.0) << "m" << endl; cout << "v0 = 28 m/s, moon = " << moon_height(28.0) << "m" << endl; return 0; }

  8. This works, but … • repeated code • suppose your boss wants it to work for any planet in the solar system, plus all of their moons • way too many functions • Solution 2: send the gravitational constant as a second parameter

  9. #include <iostream> using namespace std; const double EARTH_G = 9.81; const double MOON_G = 1.62; const double MARS_G = 3.69; double height(double v0, double G) { return v0 * v0 / (2 * G); } int main() { cout << "v0 = 28 m/s, earth = " << height(28.0, EARTH_G) << "m" << endl; cout << "v0 = 28 m/s, moon = " << height(28.0, MOON_G) << "m" << endl; cout << "v0 = 28 m/s, mars = " << height(28.0, MARS_G) << "m" << endl; return 0; }

  10. applepie $ g++ -o default default.cc applepie $ ./default v0 = 28 m/s, earth = 39.9592m v0 = 28 m/s, moon = 241.975m v0 = 28 m/s, mars = 106.233m applepie $

  11. The previous solution works fine • but what if the majority of the time, our calculations are on Earth?

  12. #include <iostream> using namespace std; const double EARTH_G = 9.81; const double MOON_G = 1.62; const double MARS_G = 3.69; double height(double v0, double G) { return v0 * v0 / (2 * G); } int main() { cout << "v0 = 5 m/s, earth = " << height(5.0, EARTH_G) << "m" << endl; cout << "v0 = 24 m/s, earth = " << height(24.0, EARTH_G) << "m" << endl; cout << "v0 = 39 m/s, earth = " << height(39.0, EARTH_G) << "m" << endl; cout << "v0 = 46 m/s, earth = " << height(46.0, EARTH_G) << "m" << endl; cout << "v0 = 59 m/s, earth = " << height(59.0, EARTH_G) << "m" << endl; cout << "v0 = 104 m/s, earth = " << height(104.0, EARTH_G) << "m" << endl; cout << "v0 = 92 m/s, earth = " << height(92.0, EARTH_G) << "m" << endl; cout << "v0 = 28 m/s, moon = " << height(28.0, MOON_G) << "m" << endl; cout << "v0 = 28 m/s, mars = " << height(28.0, MARS_G) << "m" << endl; return 0; } Repeated code.

  13. Default Parameter Value • a programmer can give a parameter in a function a default value • if the function call omits an argument for this parameter, then the default value is used instead • use the assignment operator to indicate a default parameter values

  14. Default Parameter Value • Example: our height function If the user does not send a second argument to height, it uses the value 9.81 for G. double height(double v0, double G = 9.81) { return v0 * v0 / (2 * G); }

  15. For this function call, parameter G gets the value 1.62, since the calling function sent it a value. #include <iostream> using namespace std; const double EARTH_G = 9.81; const double MOON_G = 1.62; const double MARS_G = 3.69; double height(double v0, double G = EARTH_G) { return v0 * v0 / (2 * G); } int main() { cout << "v0 = 28 m/s, moon = " << height(28.0, MOON_G) << "m" << endl; cout << "v0 = 5 m/s, earth = " << height(5.0) << "m" << endl; return 0; }

  16. For this function call, parameter G did not receive a value from the calling function. Therefore, it defaults to 9.81. #include <iostream> using namespace std; const double EARTH_G = 9.81; const double MOON_G = 1.62; const double MARS_G = 3.69; double height(double v0, double G = EARTH_G) { return v0 * v0 / (2 * G); } int main() { cout << "v0 = 28 m/s, moon = " << height(28.0, MOON_G) << "m" << endl; cout << "v0 = 5 m/s, earth = " << height(5.0) << "m" << endl; return 0; }

  17. #include <iostream> using namespace std; const double EARTH_G = 9.81; const double MOON_G = 1.62; const double MARS_G = 3.69; double height(double v0, double G) { return v0 * v0 / (2 * G); } int main() { cout << "v0 = 5 m/s, earth = " << height(5.0, EARTH_G) << "m" << endl; cout << "v0 = 24 m/s, earth = " << height(24.0, EARTH_G) << "m" << endl; cout << "v0 = 39 m/s, earth = " << height(39.0, EARTH_G) << "m" << endl; cout << "v0 = 46 m/s, earth = " << height(46.0, EARTH_G) << "m" << endl; cout << "v0 = 59 m/s, earth = " << height(59.0, EARTH_G) << "m" << endl; cout << "v0 = 104 m/s, earth = " << height(104.0, EARTH_G) << "m" << endl; cout << "v0 = 92 m/s, earth = " << height(92.0, EARTH_G) << "m" << endl; cout << "v0 = 28 m/s, moon = " << height(28.0, MOON_G) << "m" << endl; cout << "v0 = 28 m/s, mars = " << height(28.0, MARS_G) << "m" << endl; return 0; } Repeated code.

  18. #include <iostream> using namespace std; const double EARTH_G = 9.81; const double MOON_G = 1.62; const double MARS_G = 3.69; double height(double v0, double G = EARTH_G) { return v0 * v0 / (2 * G); } int main() { cout << "v0 = 5 m/s, earth = " << height(5.0) << "m" << endl; cout << "v0 = 24 m/s, earth = " << height(24.0) << "m" << endl; cout << "v0 = 39 m/s, earth = " << height(39.0) << "m" << endl; cout << "v0 = 46 m/s, earth = " << height(46.0) << "m" << endl; cout << "v0 = 59 m/s, earth = " << height(59.0) << "m" << endl; cout << "v0 = 104 m/s, earth = " << height(104.0) << "m" << endl; cout << "v0 = 92 m/s, earth = " << height(92.0) << "m" << endl; cout << "v0 = 28 m/s, moon = " << height(28.0, MOON_G) << "m" << endl; cout << "v0 = 28 m/s, mars = " << height(28.0, MARS_G) << "m" << endl; return 0; }

  19. Default Parameters • this is how getline is able to take two or three parameters • the third parameter of getline has a default value of '\n' • therefore, if you don't send it a stopping character, it defaults to this value

  20. Default Parameter Value Rules: • 1) in the function header • if you specify a default parameter value for a parameter, then all of the parameters to the right of that parameter must also have a default parameter • this means that all default parameters are on the right

  21. Example: double height(double v0, double G = EARTH_G) { return v0 * v0 / (2 * G); } OK! double height(double v0 = 1.0, double G) { return v0 * v0 / (2 * G); } Error! double height(double v0 = 1.0, double G = EARTH_G) { return v0 * v0 / (2 * G); } OK!

  22. Default Parameter Value Rules: • 2) in the function call • if you send a value to a default parameter, then you must send a value to all of the parameters to the left of that value

  23. Example (using a round function): double round(double d = 1.234567, int digits = 3) { return static_cast<int>(d * pow(10.0, digits) + 0.5) / pow(10.0, digits); } int main() { cout << round(1.234567, 3) << endl; cout << round(1.234567) << endl; cout << round(, 3) << endl; cout << round() << endl; return 0; } OK! OK! Compiler error! OK!

  24. Default Parameter Rules: • default values can be constants, global variables, or function calls double f() {return 3.0;} double height(double v0 = f(), double G = EARTH_G) { return v0 * v0 / (2 * G); }

More Related