TAILIEUCHUNG - PHP Game Programming 2004 phần 4

Xem như thế nào giá trị của $ someNum thay đổi? Giá trị của nó là kết quả từ các hoạt động bên trong hàm. Đi qua biến bằng cách tham chiếu là một cách rất tiện dụng để trả lại kết quả cho các biến nhiều bởi vì một chức năng có thể trở lại chỉ có một giá trị nếu bạn đang sử dụng các từ khóa trở lại. | 98 Chapter 5 Operators Statements and Functions See how the value of someNum changed Its value is the result from the operations inside the function. Passing variables by reference is a very handy way to return results for multiple variables because a function can return only one value if you are using the return keyword. Recursion PHP functions also support recursion. Recursion is simply when a function calls itself. One of the easiest ways to explain recursion is just to show you take this for example function power base exponent if exponent return base power base exponent - 1 return 1 Notice how the return statement is calling the power function but it is subtracting one from the variable exponent every time it is called. Eventually exponent will hit zero and the if statement will not execute. At this point is when the recursion ends. Take an in-depth look at how this works. echo power 2 3 1. The function power is called with the base set to 2 and the exponent set to 3. 2. Next the exponent is tested since it is non-zero it succeeds and proceeds to the next line. 3. The return statement calls the power function again except this time the base is 2 and the exponent is also 2. 4. The exponent variable is tested again. It is still a non-zero value. 5. Power is called again with the value 2 as the base and 1 as the exponent. 6. The exponent variable is tested again it is still a non-zero value. 7. Power is called a final time with the value 2 as the base and 0 as the exponent. 8. This time the test fails and the function returns 1 to the third invocation of the function. 9. 1 is now multiplied by the base and the third invocation of the function returns 2 to the second invocation of the function. The Magic of Including Files 99 10. 2 is now multiplied by the base value giving us 4 and 4 is returned to the first invocation of the power function. 11. 4 is now multiplied by the base giving us the final value of 8 and 8 is returned from the power function back to your .

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.