Danh mục

Adding Parameters to functions

Số trang: 13      Loại file: pdf      Dung lượng: 31.13 KB      Lượt xem: 26      Lượt tải: 0    
Jamona

Phí tải xuống: 4,000 VND Tải xuống file đầy đủ (13 trang) 0
Xem trước 2 trang đầu tiên của tài liệu này:

Thông tin tài liệu:

Thêm tham số để hàm Trong bài tập trước, bạn đã học cách tạo một hàm và gọi nó là. Trong bài tập này, bạn sẽ thêm một tham số cho chức năng và tìm hiểu làm thế nào để sử dụng chúng. Đây là cú pháp để tạo ra một chức năng chấp nhận các thông số
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ài liệu được xem nhiều:

Gợi ý tài liệu liên quan: