Functions, Part 2 of 2Functions Can Return ValuesSlide 3Parameter PassingSlide 5Local VariablesParameter Passing and Local VariablesSame Name, Still Different Memory LocationsChanges to Local Variables Do NOT Change Other Variables with the Same Name1Functions, Part 2 of 2TopicsFunctions That Return a ValueParameter PassingLocal Variables2/******************************************************************* AverageTwo - calculates and returns the average of two numbers** Inputs: num1 - a number** num2 - a number** Outputs: the average of num1 and num2*****************************************************************/function AverageTwo (num1, num2){var average; /* average of the two numbers */average = (num1 + num2) / 2;return average;}Functions Can Return Values133<head><title>AverageTwo Example</title><script type="text/javascript"> <!-- function AverageTwo(num1, num2) { var average; average = (num1 + num2) / 2; return average; } //--></script></head><body> <script type="text/javascript"> <!-- var ave, value1 = 5, value2 = 8; ave = AverageTwo(value1, value2); alert("The average is " + ave); //--> </script></body>Using AverageTwo4Parameter PassingActual parameters are the parameters that appear in the function call. average = AverageTwo (value1, value2) ;Formal parameters are the parameters that appear in the function header.function AverageTwo (num1, num2)Actual and formal parameters are matched by position. Each formal parameter receives the value of its corresponding actual parameter.5Parameter PassingCorresponding actual and formal parameters do not have to have the same name, but they may.6Local VariablesFunctions only “see” (have access to) their own local variables.Formal parameters are declarations of local variables. The values passed are assigned to those variables.Other local variables can be declared within the function body.7Parameter Passing and Local Variables<body> <script type="text/javascript"> <!-- var ave, value1 = 5, value2 = 8; ave = AverageTwo(value1, value2); alert("The average is " + ave); //--> </script></body><head><title>AverageTwo Example</title><script type="text/javascript"> <!-- function AverageTwo(num1, num2) { var average; average = (num1 + num2) / 2; return average; } //--></script></head>5 8num1 averagenum2 avevalue2value18Same Name, Still Different Memory Locations<body> <script type="text/javascript"> <!-- var average, num1 = 5, num2 = 8; average = AverageTwo(num1, num2); alert("The average is " + average); //--> </script></body><head><title>AverageTwo Example</title><script type="text/javascript"> <!-- function AverageTwo(num1, num2) { var average; average = (num1 + num2) / 2; return average; } //--></script></head>5 8num1 averagenum2 averagenum2num19Changes to Local Variables Do NOTChange Other Variables with the Same Name<body> <script type="text/javascript"> <!-- var num1 = 5; AddOne(num1); alert("In the body: num1 = " + num1); //--> </script></body><head><title>AddOne Example</title><script type="text/javascript"> <!-- function AddOne(num1) { num1++; alert("In AddOne: num1 = " + num1); } //--></script></head>num1
View Full Document