TAILIEUCHUNG - Học JavaScript qua ví dụ part 17

Quay trở lại giá trị Chức năng có thể trở về giá trị với một tuyên bố trở lại. Các từ khóa trả về là tùy chọn và chỉ có thể tồn tại bên trong một hàm. Khi các từ khoá trả về là đạt trong một hàm, không xử lý thêm trong chức năng xảy ra. | What Is a Function 153 Figure Output from Example . Return Values Functions can return values with a return statement. The return keyword is optional and can only exist within a function. When the return keyword is reached in a function no further processing within the function occurs. A return can be used to send back the result of some task such as a calculation or to exit a function early if some condition occurs. If a function doesn t have a return statement it returns the undefined value. FORMAT return return expression EXAMPLE function sum a b var result a b return result If the call to the function is made part of an expression the returned value can be assigned to a variable. In Example the sum function is called with two arguments 5 and 10. The sum function s return value will be assigned to the variable total. var total sum 5 10 From the Library of 154 Chapter 7 Functions EXAMPLE html head title Return Value title script type text javascript 1 function mileage miles gas 2 return miles gas Return the result of the division script head body bgcolor lightgreen font face arial size 1 div align center img src script type text javascript 3 var distance eval prompt How many miles did you drive var amount eval prompt How much gas did you use 4 var rate mileage distance amount Return value assigned to rate 5 alert Your mileage rate miles per gallon. n script div font body html EXPLANATION 1 A function called mileage is defined in this JavaScript program located between the head tags of the document. 2 The return statement sends back to the caller of the function the result of the division. That returned value will be assigned to the variable rate on line 4. 3 The user is asked for input. The number of miles driven and the amount of gas used are assigned to the variables called distance and amount respectively see Figure . 4 The mileage function is called passing two arguments. Because the mileage function is .

TỪ KHÓA LIÊN QUAN
TAILIEUCHUNG - Chia sẻ tài liệu không giới hạn
Địa chỉ : 444 Hoang Hoa Tham, Hanoi, Viet Nam
Website : tailieuchung.com
Email : tailieuchung20@gmail.com
Tailieuchung.com là thư viện tài liệu trực tuyến, nơi chia sẽ trao đổi hàng triệu tài liệu như luận văn đồ án, sách, giáo trình, đề thi.
Chúng tôi không chịu trách nhiệm liên quan đến các vấn đề bản quyền nội dung tài liệu được thành viên tự nguyện đăng tải lên, nếu phát hiện thấy tài liệu xấu hoặc tài liệu có bản quyền xin hãy email cho chúng tôi.
Đã phát hiện trình chặn quảng cáo AdBlock
Trang web này phụ thuộc vào doanh thu từ số lần hiển thị quảng cáo để tồn tại. Vui lòng tắt trình chặn quảng cáo của bạn hoặc tạm dừng tính năng chặn quảng cáo cho trang web này.