+ All Categories
Home > Documents > Computer Science 1620

Computer Science 1620

Date post: 19-Jan-2016
Category:
Upload: marilu
View: 26 times
Download: 0 times
Share this document with a friend
Description:
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 - PowerPoint PPT Presentation
Popular Tags:
27
Computer Science 1620 Default Parameter Values
Transcript
Page 1: Computer Science 1620

Computer Science 1620

Default Parameter Values

Page 2: Computer Science 1620

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

vh

2

20=

v0

g

Page 3: Computer Science 1620

#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;

}

Page 4: Computer Science 1620
Page 5: Computer Science 1620

Suppose your boss wants this function to work on the moon as wellgravitational acceleration on moon = 1.62

m/s

Page 6: Computer Science 1620

#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;

}

Page 7: Computer Science 1620
Page 8: Computer Science 1620

Suppose your boss wants to be able to calculate for both moon and Earth in same programSolution 1: write two separate functions

Page 9: Computer Science 1620

#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;

}

Page 10: Computer Science 1620
Page 11: Computer Science 1620

This works, but … repeated codesuppose your boss wants it to work for any

planet in the solar system, plus all of their moons

way too many functionsSolution 2: send the gravitational constant

as a second parameter

Page 12: Computer Science 1620

#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;

}

Page 13: Computer Science 1620

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

Page 14: Computer Science 1620

The previous solution works finebut what if the majority of the time, our

calculations are on Earth?

Page 15: Computer Science 1620

#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.

Page 16: Computer Science 1620

Default Parameter Valuea 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

Page 17: Computer Science 1620

Default Parameter ValueExample: our height function

double height(double v0, double G = 9.81) {

return v0 * v0 / (2 * G);

}

If the user does not send a second argument to height, it uses the value 9.81 for G.

Page 18: Computer Science 1620

#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;

}

For this function call, parameter G gets the value 1.62, since the calling function sent it a value.

Page 19: Computer Science 1620

#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;

}

For this function call, parameter G did not receive a value from the calling function. Therefore, it defaults to 9.81.

Page 20: Computer Science 1620

#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.

Page 21: Computer Science 1620

#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;

}

Page 22: Computer Science 1620

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

Page 23: Computer Science 1620

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

Page 24: Computer Science 1620

Example:double height(double v0, double G = EARTH_G) {

return v0 * v0 / (2 * G);

}

double height(double v0 = 1.0, double G) {

return v0 * v0 / (2 * G);

}

double height(double v0 = 1.0, double G = EARTH_G) {

return v0 * v0 / (2 * G);

}

OK!

Error!

OK!

Page 25: Computer Science 1620

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

Page 26: Computer Science 1620

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!

OK!

Compiler error!

Page 27: Computer Science 1620

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);

}


Recommended