{VERSION 2 3 "IBM INTEL NT" "2.3" } {USTYLETAB {CSTYLE "Maple Input" -1 0 "Courier" 0 1 255 0 0 1 0 1 0 0 1 0 0 0 0 }{CSTYLE "" -1 256 "" 0 1 0 0 0 0 1 0 0 0 0 0 0 0 0 } {CSTYLE "" -1 259 "" 0 1 0 0 136 0 1 0 0 0 0 0 0 0 0 }{PSTYLE "Normal " -1 0 1 {CSTYLE "" -1 -1 "" 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 }0 0 0 -1 -1 -1 0 0 0 0 0 0 -1 0 }{PSTYLE "Heading 1" 0 3 1 {CSTYLE "" -1 -1 "" 1 18 0 0 0 0 0 1 0 0 0 0 0 0 0 }1 0 0 0 6 6 0 0 0 0 0 0 -1 0 }{PSTYLE "Heading 2" 3 4 1 {CSTYLE "" -1 -1 "" 1 14 0 0 2 0 0 0 0 0 0 0 0 0 0 } 0 0 0 -1 4 4 0 0 0 0 0 0 -1 0 }{PSTYLE "Bullet Item" 0 15 1 {CSTYLE " " -1 -1 "" 0 1 74 0 98 0 0 0 0 0 0 0 0 0 0 }0 0 0 -1 3 3 0 0 0 0 0 0 15 2 }{PSTYLE "Title" 0 18 1 {CSTYLE "" -1 -1 "" 1 18 0 0 0 0 0 1 1 0 0 0 0 0 0 }3 0 0 -1 12 12 0 0 0 0 0 0 19 0 }} {SECT 0 {PARA 18 "" 0 "" {TEXT -1 19 "Function Evaluation" }}{PARA 0 " " 0 "" {TEXT -1 0 "" }}{PARA 0 "" 0 "" {TEXT -1 53 "This worksheet is \+ designed to accompany Chapter 5 of " }{TEXT 256 87 "Introduction to Sc ientific Programming: Computational Problem Solving Using Maple and C " }{TEXT -1 107 " by Joseph L. Zachary. In it, we will use Maple to e xplore what happens when a function call is evaluated." }}{PARA 0 "" 0 "" {TEXT -1 0 "" }}{SECT 1 {PARA 3 "" 0 "" {TEXT -1 15 "Getting Star ted" }}{EXCHG {PARA 0 "" 0 "" {TEXT -1 267 "To use this worksheet you \+ will need to use some extensions to Maple that we have created. Read \+ in our function simulation package by evaluating the Maple command bel ow. (You will need to have first installed our custom Maple library a nd configured Maple to use it.)" }}{PARA 0 "" 0 "" {TEXT -1 0 "" }} {PARA 0 "> " 0 "" {MPLTEXT 1 0 24 "with(functionSimulator);" }}}} {SECT 1 {PARA 3 "" 0 "" {TEXT -1 19 "Function Evaluation" }}{PARA 0 " " 0 "" {TEXT -1 117 "In this laboratory we will be exploring how user- defined functions are defined and what happens when they are called." }}{EXCHG {PARA 0 "" 0 "" {TEXT -1 188 "Recall that a function is defin ed by specifying its name, its formal parameters, and its body. For e xample, here is a function called \"avg\" that returns the average of \+ its two parameters." }}{PARA 0 "" 0 "" {TEXT -1 0 "" }}{PARA 0 "> " 0 "" {MPLTEXT 1 0 28 "avg := (x,y) -> (x + y) / 2;" }}}{EXCHG {PARA 0 " " 0 "" {TEXT -1 142 "Once we have defined a function in this way, we c an use it as if it were built in. For example, we can compute the ave rage of 6+6 and 3*6 via" }}{PARA 0 "" 0 "" {TEXT -1 0 "" }}{PARA 0 "> \+ " 0 "" {MPLTEXT 1 0 14 "avg(6+6, 3*6);" }}}{PARA 0 "" 0 "" {TEXT -1 0 "" }}{PARA 0 "" 0 "" {TEXT -1 129 "Here, 6+6 and 3*6 are called the ac tual parameters, to distinguish them from the formal parameters that a re used to define \"avg\"." }}{PARA 0 "" 0 "" {TEXT -1 0 "" }}{EXCHG {PARA 0 "" 0 "" {TEXT -1 105 "Maple follows a three-step process when \+ evaluating a call to a programmer-defined function. In the call " }} {PARA 0 "" 0 "" {TEXT -1 0 "" }}{PARA 0 "> " 0 "" {MPLTEXT 1 0 14 "avg (6+6, 3*6);" }}{PARA 0 "" 0 "" {TEXT -1 84 "the expressions \"6+6\" an d \"3*6\" are evaluated to obtain \"12\" and \"18\", respectively." }} }{PARA 0 "" 0 "" {TEXT -1 0 "" }}{EXCHG {PARA 0 "" 0 "" {TEXT -1 246 " Maple's second step is to substitute the evaluated actual parameters f or the corresponding formal parameters in the body of the function. I n the case of the call to \"avg\" above, 12 corresponds to x and 18 co rresponds to y. The body of \"avg\" is" }}{PARA 0 "" 0 "" {TEXT -1 0 "" }}{PARA 0 "> " 0 "" {MPLTEXT 1 0 18 "1/2 * x + 1/2 * y;" }}} {PARA 0 "" 0 "" {TEXT -1 0 "" }}{EXCHG {PARA 0 "" 0 "" {TEXT -1 56 "Wh en Maple substitutes 12 for x and 18 for y, it obtains" }}{PARA 0 "" 0 "" {TEXT -1 0 "" }}{PARA 0 "> " 0 "" {MPLTEXT 1 0 20 "1/2 * 12 + 1/2 * 18;" }}}{PARA 0 "" 0 "" {TEXT -1 0 "" }}{EXCHG {PARA 0 "" 0 "" {TEXT -1 237 "Maple's third step is to evaluate the expression that re sults from the substitution just as if it had been typed directly into Maple. Since the result of the expression above is 15, this is also \+ the result of the original function call." }}{PARA 0 "" 0 "" {TEXT -1 0 "" }}{PARA 0 "> " 0 "" {MPLTEXT 1 0 14 "(12 + 18) / 2;" }}}}{SECT 1 {PARA 3 "" 0 "" {TEXT -1 30 "Simulating Function Evaluation" }}{PARA 0 "" 0 "" {TEXT -1 204 "When you are first learning how to write funct ions, it can be difficult to visualize these three steps. We have cre ated a special function called \"functionTrace\" to help you do this. \+ Let's try it out. " }}{PARA 0 "" 0 "" {TEXT -1 0 "" }}{EXCHG {PARA 0 "" 0 "" {TEXT -1 210 "Suppose you would like to see what really goes on when you make the function call \"avg(6+6, 3*6)\". You can do thi s by enclosing the function call in backquotes and passing it as a par ameter to \"functionTrace\"." }}{PARA 0 "" 0 "" {TEXT -1 0 "" }}{PARA 0 "> " 0 "" {MPLTEXT 1 0 31 "functionTrace(`avg(6+6, 3*6)`);" }}} {PARA 0 "" 0 "" {TEXT -1 0 "" }}{PARA 0 "" 0 "" {TEXT -1 445 "Look at \+ all of the information that is printed out. We are first shown the fo rmal parameters and the body of \"avg\". We are then shown the result s of evaluating the actual parameters, the expression obtained when th e actual parameters are substituted for the formal parameters in the b ody of the function, and the result of evaluating this expression. Be st of all, \"functionTrace\" will work for any programmer-defined func tion that you create." }}{PARA 0 "" 0 "" {TEXT -1 0 "" }}{EXCHG {PARA 0 "" 0 "" {TEXT -1 61 "For example, let's define a function that cubes its argument." }}{PARA 0 "" 0 "" {TEXT -1 0 "" }}{PARA 0 "> " 0 "" {MPLTEXT 1 0 19 "cube := (x) -> x^3;" }}}{PARA 0 "" 0 "" {TEXT -1 0 " " }}{EXCHG {PARA 0 "" 0 "" {TEXT -1 96 "We can trace Maple's process o f evaluating a call to \"cube\" as easily as we did a call of \"avg\". " }}{PARA 0 "" 0 "" {TEXT -1 0 "" }}{PARA 0 "> " 0 "" {MPLTEXT 1 0 26 "functionTrace(`cube(15)`);" }}}}{SECT 1 {PARA 3 "" 0 "" {TEXT -1 13 " Common Errors" }}{PARA 0 "" 0 "" {TEXT -1 125 "We can use \"functionTr ace\" to illustrate a number of mistakes that beginning programmers te nd to make when calling functions." }}{PARA 0 "" 0 "" {TEXT -1 0 "" }} {SECT 1 {PARA 4 "" 0 "" {TEXT -1 30 "The wrong number of parameters" } }{EXCHG {PARA 0 "" 0 "" {TEXT -1 330 "When you call a function, it is \+ important that there be the same number of actual parameters in the fu nction call as there are formal parameters in the function definition. Otherwise, Maple will not be able to match each actual to a formal p rior to doing the substitution. Here, for example, we give only one p arameter to \"avg\":" }}{PARA 0 "" 0 "" {TEXT -1 0 "" }}{PARA 0 "> " 0 "" {MPLTEXT 1 0 24 "functionTrace(`avg(5)`);" }}}{PARA 0 "" 0 "" {TEXT -1 0 "" }}{EXCHG {PARA 0 "" 0 "" {TEXT -1 70 "This is a mistake, of course. Here we give three parameters to \"avg\":" }}{PARA 0 "" 0 "" {TEXT -1 0 "" }}{PARA 0 "> " 0 "" {MPLTEXT 1 0 28 "functionTrace( `avg(1,2,3)`);" }}}{PARA 0 "" 0 "" {TEXT -1 0 "" }}{EXCHG {PARA 0 "" 0 "" {TEXT -1 107 "This is also a mistake. If we type these function \+ calls directly into Maple instead of into the simulator:" }}{PARA 0 " " 0 "" {TEXT -1 0 "" }}{PARA 0 "> " 0 "" {MPLTEXT 1 0 7 "avg(5);" }}} {PARA 0 "" 0 "" {TEXT -1 0 "" }}{EXCHG {PARA 0 "> " 0 "" {MPLTEXT 1 0 11 "avg(1,2,3);" }}{PARA 0 "" 0 "" {TEXT -1 220 "we see that Maple alw ays complains if you provide too few parameters, but ignores the extra parameters if you supply too many. Nevertheless, you should always s upply exactly the right number of parameters to a function." }}}{PARA 0 "" 0 "" {TEXT -1 0 "" }}}{SECT 1 {PARA 4 "" 0 "" {TEXT -1 17 "Magic \+ invocations" }}{EXCHG {PARA 0 "" 0 "" {TEXT -1 295 "For some reason, m any beginning students appear to expect that the actual parameters wil l get to the function by magic. One commonly attempted tactic is to t ry to assign values to variables with the same name as the formal para meters and to then \"call\" the function by just mentioning its name. " }}{PARA 0 "" 0 "" {TEXT -1 0 "" }}{PARA 0 "> " 0 "" {MPLTEXT 1 0 8 " x := 12;" }}{PARA 0 "> " 0 "" {MPLTEXT 1 0 8 "y := 18;" }}{PARA 0 "> \+ " 0 "" {MPLTEXT 1 0 4 "avg;" }}}{PARA 0 "" 0 "" {TEXT -1 0 "" }} {EXCHG {PARA 0 "" 0 "" {TEXT -1 87 "Not surprisingly, this doesn't wor k! If we run this function \"call\" through the tracer" }}{PARA 0 "" 0 "" {TEXT -1 0 "" }}{PARA 0 "> " 0 "" {MPLTEXT 1 0 21 "functionTrace( `avg`);" }}{PARA 0 "" 0 "" {TEXT -1 38 "we discover the nature of our \+ mistake." }}{PARA 0 "" 0 "" {TEXT -1 0 "" }}}{PARA 0 "" 0 "" {TEXT -1 0 "" }}{EXCHG {PARA 0 "" 0 "" {TEXT -1 14 "Of course, it " }{TEXT 259 6 "would " }{TEXT -1 58 "work if we passed \"x\" and \"y\" as the two \+ actual parameters" }}{PARA 0 "" 0 "" {TEXT -1 0 "" }}{PARA 0 "> " 0 " " {MPLTEXT 1 0 26 "functionTrace(`avg(x,y)`);" }}{PARA 0 "" 0 "" {TEXT -1 43 "since they now have the appropriate values." }}}{PARA 0 " " 0 "" {TEXT -1 0 "" }}{EXCHG {PARA 0 "" 0 "" {TEXT -1 121 "Of course, there is nothing special about the names \"x\" and \"y\". We could j ust as easily have used \"first\" and \"second\":" }}{PARA 0 "" 0 "" {TEXT -1 0 "" }}{PARA 0 "> " 0 "" {MPLTEXT 1 0 12 "first := 12;" }} {PARA 0 "> " 0 "" {MPLTEXT 1 0 13 "second := 18;" }}{PARA 0 "> " 0 "" {MPLTEXT 1 0 36 "functionTrace(`avg(first, second)`);" }}{PARA 0 "" 0 "" {TEXT -1 87 "From the point of view of the tracer, the last two inv ocations of \"avg\" were identical." }}}{PARA 0 "" 0 "" {TEXT -1 0 "" }}{PARA 0 "" 0 "" {TEXT -1 273 "Remember: it doesn't matter how you wr ite the actual parameters--all that matters is what they evaluate to. \+ And when you are using a function, it doesn't matter at all what the \+ formal parameters are called--that is pertinent only within the implem entation of the function." }}}}{SECT 1 {PARA 3 "" 0 "" {TEXT -1 14 "Fr ee Variables" }}{PARA 0 "" 0 "" {TEXT -1 0 "" }}{EXCHG {PARA 0 "" 0 " " {TEXT -1 66 "Let's experiment with a different programmer-defined fu nction now." }}{PARA 0 "" 0 "" {TEXT -1 0 "" }}{PARA 0 "> " 0 "" {MPLTEXT 1 0 33 "distance := (t) -> 1/2 * g * t^2;" }}}{PARA 0 "" 0 " " {TEXT -1 0 "" }}{EXCHG {PARA 0 "" 0 "" {TEXT -1 19 "If we call dista nce" }}{PARA 0 "" 0 "" {TEXT -1 0 "" }}{PARA 0 "> " 0 "" {MPLTEXT 1 0 14 "distance(15.);" }}{PARA 0 "" 0 "" {TEXT -1 0 "" }}{PARA 0 "" 0 "" {TEXT -1 362 "you will notice that the variable \"g\" appears in the r esult. This is because it appears in the body of the function definit ion but is NOT a parameter. As a result, it appears in the substitute d body. When Maple evaluates the substituted body, it tries to look u p the value of \"g\" like it would for any variable, and failing to fi nd one it leaves \"g\" as it is." }}}{PARA 0 "" 0 "" {TEXT -1 0 "" }} {EXCHG {PARA 0 "" 0 "" {TEXT -1 74 "This will be more evident if we tr ace the evaluation of the function call." }}{PARA 0 "" 0 "" {TEXT -1 0 "" }}{PARA 0 "> " 0 "" {MPLTEXT 1 0 31 "functionTrace(`distance(15.) `);" }}}{PARA 0 "" 0 "" {TEXT -1 0 "" }}{EXCHG {PARA 0 "" 0 "" {TEXT -1 26 "If we now give \"g\" a value" }}{PARA 0 "" 0 "" {TEXT -1 0 "" } }{PARA 0 "> " 0 "" {MPLTEXT 1 0 9 "g := 9.8;" }}}{PARA 0 "" 0 "" {TEXT -1 0 "" }}{EXCHG {PARA 0 "" 0 "" {TEXT -1 46 "and then evaluate \+ the call to \"distance\" again" }}{PARA 0 "" 0 "" {TEXT -1 0 "" }} {PARA 0 "> " 0 "" {MPLTEXT 1 0 31 "functionTrace(`distance(15.)`);" }} }{PARA 0 "" 0 "" {TEXT -1 0 "" }}{EXCHG {PARA 0 "" 0 "" {TEXT -1 147 " we get a numerical answer. Notice that the value of \"g\" is not look ed up until the substituted body is actually evaluated. Thus, if we c hange \"g\"" }}{PARA 0 "" 0 "" {TEXT -1 0 "" }}{PARA 0 "> " 0 "" {MPLTEXT 1 0 10 "g := 15.5;" }}}{PARA 0 "" 0 "" {TEXT -1 0 "" }} {EXCHG {PARA 0 "" 0 "" {TEXT -1 21 "and try one more time" }}{PARA 0 " " 0 "" {TEXT -1 0 "" }}{PARA 0 "> " 0 "" {MPLTEXT 1 0 31 "functionTrac e(`distance(15.)`);" }}{PARA 0 "" 0 "" {TEXT -1 26 "we get a different answer." }}}}{SECT 1 {PARA 3 "" 0 "" {TEXT -1 10 "Exercises." }} {PARA 0 "" 0 "" {TEXT -1 342 "You should now experiment with defining \+ some functions of your own. Implement the following functions. Test \+ them out by calling them directly from Maple, and also use functionTra ce to help you visualize what goes on when they are called. Remember: to use functionTrace, give it as a single parameter a function call e nclosed in backquotes." }}{PARA 0 "" 0 "" {TEXT -1 0 "" }}{PARA 15 "" 0 "" {TEXT -1 75 "\"avg4\" should take four numbers as its parameters \+ and return their average." }}{PARA 15 "" 0 "" {TEXT -1 105 "sincos\" s hould take one number as its parameter and return the sum of the sine \+ and cosine of that number." }}{PARA 15 "" 0 "" {TEXT -1 109 "\"miles2i nches\" should take a distance in miles as its parameter and return th e equivalent distance in inches." }}{PARA 15 "" 0 "" {TEXT -1 172 "\"t riangle\" should take as its two parameters the measurement in degrees of two of the angles in a triangle. It should return the measurement in degrees of the third angle." }}{PARA 15 "" 0 "" {TEXT -1 133 "\"hy pot\" should take as its two parameters the length of the sides of a r ight triangle and should return the length of the hypotenuse." }}}} {MARK "0 0" 0 }{VIEWOPTS 1 1 0 1 1 1803 }