DOC PREVIEW
UMBC CMSC 104 - Functions, Part 2 of 2

This preview shows page 1-2-3 out of 9 pages.

Save
View full document
View full document
Premium Document
Do you want full access? Go Premium and unlock all 9 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 9 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 9 pages.
Access to all documents
Download any document
Ad free experience
Premium Document
Do you want full access? Go Premium and unlock all 9 pages.
Access to all documents
Download any document
Ad free experience

Unformatted text preview:

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 2TopicsFunctions That Return a ValueParameter PassingLocal 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 PassingActual 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 PassingCorresponding actual and formal parameters do not have to have the same name, but they may.6Local VariablesFunctions 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

UMBC CMSC 104 - Functions, Part 2 of 2

Download Functions, Part 2 of 2
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view Functions, Part 2 of 2 and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Functions, Part 2 of 2 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?