Computer Science 1620
Default Parameter Values
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
#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;
}
Suppose your boss wants this function to work on the moon as wellgravitational acceleration on moon = 1.62
m/s
#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;
}
Suppose your boss wants to be able to calculate for both moon and Earth in same programSolution 1: write two separate functions
#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;
}
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
#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;
}
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 $
The previous solution works finebut what if the majority of the time, our
calculations are on Earth?
#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.
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
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.
#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.
#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.
#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.
#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;
}
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
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
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!
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
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!
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);
}