Bài đăng

Đang hiển thị bài đăng từ Tháng 9, 2017
3.14 Glossary function: A named sequence of statements that performs some useful operation. Functions may or may not take arguments and may or may not produce a result. Hàm: Tên trình tự của câu lệnh mà thực hiện một số phép toán hữu ích. Các hàm có thể hoặc không nhận giá trị đối số và có thể hoặc không đưa ra một kết quả.  function definition: A statement that creates a new function, specifying its name, parameters, and the statements it executes. Định nghĩa hàm: là một câu lệnh tạo một hàm mới, chỉ rõ tên nó, tham số và cú pháp nó thực thi. function object: A value created by a function definition. The name of the function is a variable that refers to a function object. Hàm đối tượng: Một giá trị tạo bởi một hàm định nghĩa. Tên của hàm là một biến dẫn tới một hàm đối tượng. header: The first line of a function definition. Phần đầu:  Dòng đầu tiên của 1 hàm định nghĩa body: The sequence of statements inside a function definition. Phần thân: Tính tuần tự của câu lệ...

3.12 Why functions? - Tại sao sử dụng hàm? (ý nghĩa của hàm)

It may not be clear why it is worth the trouble to divide a program into functions. There are several reasons: • Creating a new function gives you an opportunity to name a group of statements, which makes your program easier to read and debug. • Functions can make a program smaller by eliminating repetitive code. Later, if you make a change, you only have to make it in one place. • Dividing a long program into functions allows you to debug the parts one at a time and then assemble them into a working whole. • Well-designed functions are often useful for many programs. Once you write and debug one, you can reuse it. Có thể không rõ ràng là tại sao lại phải rắc rối để chia các hàm trong một chương trình. Có một số lý do: • Tạo một chức năng mới cho phép bạn đặt tên một nhóm các câu lệnh, làm cho chương trình của bạn dễ đọc và gỡ lỗi hơn. • Chức năng có thể làm cho một chương trình nhỏ hơn bằng cách loại bỏ mã lặp đi lặp lại. Sau đó, nếu bạn thực hiện một thay đổi, bạn chỉ ph...

3.10 Stack diagrams - sơ đồ xếp chồng

Hình ảnh
To keep track of which variables can be used where, it is sometimes useful to draw a stack diagram . Like state diagrams, stack diagrams show the value of each variable, but they also show the function each variable belongs to. Each function is represented by a frame . A frame is a box with the name of a function beside it and the parameters and variables of the function inside it. The stack diagram for the previous example looks like this: Để theo dõi các biến nào có thể được sử dụng ở đâu, đôi khi hữu ích để vẽ sơ đồ ngăn xếp. Giống như sơ đồ trạng thái, biểu đồ ngăn xếp hiển thị giá trị của mỗi biến, nhưng chúng cũng hiển thị chức năng của từng biến. Mỗi hàm được biểu diễn bởi một khung. Khung là một hộp với tên của một hàm bên cạnh nó và các tham số và các biến của hàm bên trong nó. Biểu đồ ngăn xếp cho ví dụ trước đây sẽ như sau: The frames are arranged in a stack that indicates which function called which, and so on. In this example, print_twice was called by cat_twice, a...

3.6 Definitions and uses

Pulling together the code fragments from the previous section, the whole program looks like this: def print_lyrics(): print "I'm a lumberjack, and I'm okay." print "I sleep all night and I work all day." def repeat_lyrics(): print_lyrics() print_lyrics() repeat_lyrics() This program contains two function definitions: print_lyrics and repeat_lyrics. Function definitions get executed just like other statements, but the effect is to create function objects. The statements inside the function do not get executed until the function is called, and the function definition generates no output. Chương trình này bao gồm 2 hàm được xác định: print_lyrics và repeat_lyrics. Hàm xác định nhận thực thi giống như câu lệnh khác nhưng tác dụng của nó là tạo hàm đối tượng. Câu lệnh bên trong hàm không đưa ra thực thi cho tới khi hàm được gọi, và định nghĩa hàm không tạo ra kết quả. As you might expect, you have to create a function before you can execute it. In other words,...