Adding Parameters to functions
Thông tin tài liệu:
Nội dung trích xuất từ tài liệu:
Adding Parameters to functions < Day Day Up >Adding Parameters to FunctionsIn the preceding exercise, you learned how to create a function and call it. In thisexercise, youll add parameters to a function and learn how to use them. Heres the syntaxfor creating a function that accepts parameters:function convertToMoonWeight (myWeight:Number){ var weightOnMoon:Number = myWeight/6.04;}The sole parameter for this function is named myWeight. The value of that parameter isalso used within the function definition (near the end of the second line), just as if it werea preexisting variable. Notice that you should associate a data type with the parameter inthe function definition. In this case, the parameter myWeight represents a numeric value.Heres an example of the syntax used to call this function:convertToMoonWeight(165);Here you can see that we added a numeric value of 165 to the function call. This value issent to the function being called so that it can perform its specified functionality, basedon that value. In this example, the value of 165 in the function call is assigned to themyWeight parameter in the function definition. The result looks something like this:function convertToMoonWeight (165){ var weightOnMoon:Number = 165/6.04;}Thus, in our example, sending a value of 165 to our convertToMoonWeight() functionwould set the value of weightOnMoon to 165/6.04, or 27.32.The myWeight parameter is replaced with the value sent to the function when it is called.The great thing about this is that whenever we call our function again, we can send it adifferent value, which will result in the weightOnMoon variables being set to a differentvalue as well. Take a look at these function calls to the convertToMoonWeight()function:convertToMoonWeight(190);convertToMoonWeight(32);convertToMoonWeight(230);Each of these function calls is to our single function, but because different values are sentto the function in each call, the function performs the same action (converting that valueto moon weight) using the different values.NOTEParameters you define for a function have meaning only within that function. In ourexample, myWeight has no meaning or use outside of the function itself.When sending values to a function, you can also use variables in your function call, as inthe following:convertToMoonWeight(myVariable);When you do this, the current value of the variable is passed to the function.Functions can also be made up of multiple parameters, like this:function openWindow(url:String, window:String){getURL(url, window);}This function definition contains two parameters, url and window, separated by a comma.The function contains a single action, getURL(), which makes use of these twoparameters. Making a call to this function looks like this:openWindow(http://www.yahoo.com, _blank);The function call also sends two values, separated by a comma, to the function: a URLand the HTML target for opening the specified URL. These parameter values are used bythe function in order to perform its specified functionality. In this case, the function callwould result in yahoo.com opening in a new browser window.When defining multiple parameters in a function definition, remember their order withinthe parentheses. Respective values that are defined in the function definition should belisted in that same order in the function call.Heres a function call to the openWindow() function that wont work because theparameters of the function definition dictate that the URL should be listed first in thefunction call:openWindow(_blank, http://www.yahoo.com);NOTEWhen a function is called, a temporary array called arguments is created. This arraycontains all parameters passed to the function—even if you specified none when definingyour function. Here is an example of how to access the arguments array:function traceNames() { trace(This function was passed + arguments.length + arguments); trace(The value of the first argument is: + arguments[0]); trace(The value of the second argument is: + arguments[1]);}traceNames(Kelly,Chance);In this example, these strings appear in the output window:This function was passed two argumentsThe value of the first argument is: KellyThe value of the second argument is: ChanceAccessing the arguments array enables you to create functions that can adapt theirfunctionality based on how many parameters are passed to them. For more informationabout arrays, see Lesson 6, Creating and Manipulating Data.In this exercise, youll add functionality to the numeric keypad on the remote control andto the TV channel Up and Down buttons, allowing them to change the channel displayedon the TV screen. The numeric buttons work by calling a function and passing in thenumber of the channel t ...
Tìm kiếm theo từ khóa liên quan:
máy tính mạng máy tính internet phần mềm ứng dụng lập trình SQL HTML sever web XMLGợi ý tài liệu liên quan:
-
Giáo án Tin học lớp 9 (Trọn bộ cả năm)
149 trang 268 0 0 -
Ngân hàng câu hỏi trắc nghiệm môn mạng máy tính
99 trang 253 1 0 -
Giáo trình Hệ thống mạng máy tính CCNA (Tập 4): Phần 2
102 trang 248 0 0 -
Bài giảng: Lịch sử phát triển hệ thống mạng
118 trang 247 0 0 -
47 trang 240 3 0
-
Đề cương chi tiết học phần Thiết kế và cài đặt mạng
3 trang 236 0 0 -
80 trang 222 0 0
-
122 trang 217 0 0
-
Giáo trình Hệ thống mạng máy tính CCNA (Tập 4): Phần 1
122 trang 215 0 0 -
Giáo trình môn học/mô đun: Mạng máy tính (Ngành/nghề: Quản trị mạng máy tính) - Phần 1
68 trang 205 0 0 -
Giáo trình căn bản về mạng máy tính -Lê Đình Danh 2
23 trang 193 0 0 -
6 trang 192 0 0
-
Giáo trình môn học Mạng máy tính - Nghề: Quản trị mạng máy tính - Trình độ: Cao đẳng nghề (Phần 2)
39 trang 186 0 0 -
Bài giảng Lý thuyết mạng máy tính: Chương 3 - ThS. Lương Minh Huấn
73 trang 174 0 0 -
6 trang 174 0 0
-
139 trang 169 0 0
-
Giáo trình Mạng máy tính (Nghề: Tin học ứng dụng - Trung cấp) - Trường Cao đẳng Cộng đồng Đồng Tháp
189 trang 164 0 0 -
Đề cương chi tiết học phần: Mạng máy tính và lập trình mạng
4 trang 160 0 0 -
Giáo trình Ứng dụng công nghiệp thông tin cơ bản: Phần 1
73 trang 159 0 0 -
129 trang 157 0 0