Computer Science 1620 - PowerPoint PPT Presentation

About This Presentation
Title:

Computer Science 1620

Description:

Computer Science 1620 Default Parameter Values – PowerPoint PPT presentation

Number of Views:32
Avg rating:3.0/5.0
Slides: 28
Provided by: Kevi1259
Category:

less

Transcript and Presenter's Notes

Title: Computer Science 1620


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 ltiostreamgt
  • using namespace std
  • double height(double v0)
  • const double G 9.81
  • return v0 v0 / (2 G)
  • int main()
  • cout ltlt "v0 5 m/s, height " ltlt height(5.0)
    ltlt "m" ltlt endl
  • cout ltlt "v0 10 m/s, height " ltlt
    height(10.0) ltlt "m" ltlt endl
  • cout ltlt "v0 15 m/s, height " ltlt
    height(15.0) ltlt "m" ltlt endl
  • cout ltlt "v0 20 m/s, height " ltlt
    height(20.0) ltlt "m" ltlt endl
  • cout ltlt "v0 25 m/s, height " ltlt
    height(25.0) ltlt "m" ltlt endl
  • return 0

4
(No Transcript)
5
  • Suppose your boss wants this function to work on
    the moon as well
  • gravitational acceleration on moon 1.62 m/s

6
  • include ltiostreamgt
  • using namespace std
  • double height(double v0)
  • const double G 1.62
  • return v0 v0 / (2 G)
  • int main()
  • cout ltlt "v0 5 m/s, height " ltlt height(5.0)
    ltlt "m" ltlt endl
  • cout ltlt "v0 10 m/s, height " ltlt
    height(10.0) ltlt "m" ltlt endl
  • cout ltlt "v0 15 m/s, height " ltlt
    height(15.0) ltlt "m" ltlt endl
  • cout ltlt "v0 20 m/s, height " ltlt
    height(20.0) ltlt "m" ltlt endl
  • cout ltlt "v0 25 m/s, height " ltlt
    height(25.0) ltlt "m" ltlt endl
  • return 0

7
(No Transcript)
8
  • Suppose your boss wants to be able to calculate
    for both moon and Earth in same program
  • Solution 1 write two separate functions

9
  • include ltiostreamgt
  • 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 ltlt "v0 5 m/s, earth " ltlt
    earth_height(5.0) ltlt "m" ltlt endl
  • cout ltlt "v0 5 m/s, moon " ltlt
    moon_height(5.0) ltlt "m" ltlt endl
  • cout ltlt "v0 28 m/s, earth " ltlt
    earth_height(28.0) ltlt "m" ltlt endl

10
(No Transcript)
11
  • 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

12
  • include ltiostreamgt
  • 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 ltlt "v0 28 m/s, earth " ltlt
    height(28.0, EARTH_G) ltlt "m" ltlt endl
  • cout ltlt "v0 28 m/s, moon " ltlt
    height(28.0, MOON_G) ltlt "m" ltlt endl
  • cout ltlt "v0 28 m/s, mars " ltlt
    height(28.0, MARS_G) ltlt "m" ltlt endl
  • return 0

13
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
14
  • The previous solution works fine
  • but what if the majority of the time, our
    calculations are on Earth?

15
  • include ltiostreamgt
  • 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 ltlt "v0 5 m/s, earth " ltlt height(5.0,
    EARTH_G) ltlt "m" ltlt endl
  • cout ltlt "v0 24 m/s, earth " ltlt
    height(24.0, EARTH_G) ltlt "m" ltlt endl
  • cout ltlt "v0 39 m/s, earth " ltlt
    height(39.0, EARTH_G) ltlt "m" ltlt endl
  • cout ltlt "v0 46 m/s, earth " ltlt
    height(46.0, EARTH_G) ltlt "m" ltlt endl
  • cout ltlt "v0 59 m/s, earth " ltlt
    height(59.0, EARTH_G) ltlt "m" ltlt endl
  • cout ltlt "v0 104 m/s, earth " ltlt
    height(104.0, EARTH_G) ltlt "m" ltlt endl

Repeated code.
16
  • 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

17
  • 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)
18
  • include ltiostreamgt
  • 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 ltlt "v0 28 m/s, moon " ltlt
    height(28.0, MOON_G) ltlt "m" ltlt endl
  • cout ltlt "v0 5 m/s, earth " ltlt height(5.0)
    ltlt "m" ltlt endl
  • return 0

For this function call, parameter G gets the
value 1.62, since the calling function sent it a
value.
19
  • include ltiostreamgt
  • 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 ltlt "v0 28 m/s, moon " ltlt
    height(28.0, MOON_G) ltlt "m" ltlt endl
  • cout ltlt "v0 5 m/s, earth " ltlt height(5.0)
    ltlt "m" ltlt endl
  • return 0

For this function call, parameter G did not
receive a value from the calling function.
Therefore, it defaults to 9.81.
20
  • include ltiostreamgt
  • 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 ltlt "v0 5 m/s, earth " ltlt height(5.0,
    EARTH_G) ltlt "m" ltlt endl
  • cout ltlt "v0 24 m/s, earth " ltlt
    height(24.0, EARTH_G) ltlt "m" ltlt endl
  • cout ltlt "v0 39 m/s, earth " ltlt
    height(39.0, EARTH_G) ltlt "m" ltlt endl
  • cout ltlt "v0 46 m/s, earth " ltlt
    height(46.0, EARTH_G) ltlt "m" ltlt endl
  • cout ltlt "v0 59 m/s, earth " ltlt
    height(59.0, EARTH_G) ltlt "m" ltlt endl
  • cout ltlt "v0 104 m/s, earth " ltlt
    height(104.0, EARTH_G) ltlt "m" ltlt endl

Repeated code.
21
  • include ltiostreamgt
  • 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 ltlt "v0 5 m/s, earth " ltlt height(5.0)
    ltlt "m" ltlt endl
  • cout ltlt "v0 24 m/s, earth " ltlt
    height(24.0) ltlt "m" ltlt endl
  • cout ltlt "v0 39 m/s, earth " ltlt
    height(39.0) ltlt "m" ltlt endl
  • cout ltlt "v0 46 m/s, earth " ltlt
    height(46.0) ltlt "m" ltlt endl
  • cout ltlt "v0 59 m/s, earth " ltlt
    height(59.0) ltlt "m" ltlt endl
  • cout ltlt "v0 104 m/s, earth " ltlt
    height(104.0) ltlt "m" ltlt endl

22
  • 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

23
  • 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

24
  • 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!
25
  • 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

26
  • Example (using a round function)

double round(double d 1.234567, int digits 3)
return static_castltintgt(d pow(10.0,
digits) 0.5) / pow(10.0, digits) int main()
cout ltlt round(1.234567, 3) ltlt endl cout
ltlt round(1.234567) ltlt endl cout ltlt round(, 3)
ltlt endl cout ltlt round() ltlt endl return
0
OK!
OK!
Compiler error!
OK!
27
  • 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)
Write a Comment
User Comments (0)
About PowerShow.com