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, the
function definition has to be executed before the first time it is called.
Exercise 3.1 Move the last line of this program to the top, so the function call appears before the
definitions. Run the program and see what error message you get.
Exercise 3.2 Move the function call back to the bottom and move the definition of print_lyrics
after the definition of repeat_lyrics. What happens when you run this program?



3.7 Flow of execution - Dòng thực hiện
In order to ensure that a function is defined before its first use, you have to know the order in which
statements are executed, which is called the flow of execution.
Execution always begins at the first statement of the program. Statements are executed one at a time,
in order from top to bottom.

Để chắc chắn là một hàm được định nghĩa trước khi sử dụng lần đầu tiên, bạn cần phải biết thứ tự mà các câu lệnh được thực thi, điều này gọi là dòng thực thi (Dòng chảy thực thi các câu lệnh)
Việc thực thi luôn bắt đầu từ câu lệnh đầu tiên của chương trình. Câu lệnh được thực thi một lần trong 1 thời điểm, thự tự từ trên xuống dưới.

Function definitions do not alter the flow of execution of the program, but remember that statements
inside the function are not executed until the function is called.
A function call is like a detour in the flow of execution. Instead of going to the next statement, the
flow jumps to the body of the function, executes all the statements there, and then comes back to
pick up where it left off.


Các định nghĩa hàm không làm thay đổi luồng thực hiện chương trình, nhưng hãy nhớ rằng các câu lệnh bên trong hàm không được thực thi cho đến khi hàm được gọi.
Một cuộc gọi hàm giống như một đường vòng trong dòng thực thi. Thay vì đi tới câu lệnh kế tiếp, dòng chảy (thực thi) nhảy tới phần thân của hàm, thực hiện tất cả các câu lệnh ở đó, và sau đó quay trở lại để tìm nơi nó dừng lại (để tiếp tục)

That sounds simple enough, until you remember that one function can call another. While in the
middle of one function, the program might have to execute the statements in another function. But
while executing that new function, the program might have to execute yet another function!
Fortunately, Python is good at keeping track of where it is, so each time a function completes, the
program picks up where it left off in the function that called it. When it gets to the end of the
program, it terminates.
What’s the moral of this sordid tale? When you read a program, you don’t always want to read from
top to bottom. Sometimes it makes more sense if you follow the flow of execution.


Nghe có vẻ đủ đơn giản, chỉ tới khi bạn nhớ tới việc một hàm có thể gọi một hàm khác. Trong khi (dòng chảy thực thi) đang đến đoạn giữa của 1 hàm, chương trình có thể phải thực hiện các câu lệnh của 1 hàm khác. Nhưng khi thực hiện câu lệnh của "1 hàm khác" thì lại liên quan tới 1 hàm khác nữa - điều này đồng nghĩa với việc thực hiện hàm A - hàm B - Hàm C - quay lại hàm A thực hiện tiếp.

May mắn thay, Python giỏi theo dõi vị trí của nó, vì vậy mỗi khi một hàm hoàn thành, chương trình sẽ chọn nơi nó dừng lại ở hàm gọi nó. Khi kết thúc chương trình, nó sẽ kết thúc.

Ý nghãi của việc này thì mày cần phải theo dõi luồng thực hiện chứ đừng theo dõi hàm.


3.8 Parameters and arguments - Tham số và đối số
Some of the built-in functions we have seen require arguments. For example, when you call
math.sin you pass a number as an argument. Some functions take more than one argument:
math.pow takes two, the base and the exponent.
Inside the function, the arguments are assigned to variables called parameters. Here is an example
of a user-defined function that takes an argument:
def print_twice(bruce):
print(bruce)
print(bruce)

Một số  chức năng tích hợp trong hàm chúng ta sẽ thấy yêu  cầu đối số. Ví dụ, khi bạn gọi hàm

math.sin bạn ghi một số như một đối số. Một vài hàm cần nhiều hơn 1 đối số:

math.pow cần 2, cơ sở và sso mũ.

Trong hàm, đối số được gán cho biến gọi là tham số. Đây là một ví dụ của người dùng định nghĩa hàm vaf nhận một đối số:

def print_twice(bruce):
print(bruce)
print(bruce)


This function assigns the argument to a parameter named bruce. When the function is called, it
prints the value of the parameter (whatever it is) twice.

Hàm số này gán cho đối số là một tham số tên là bruce. Khi hàm số được gọi nó sẽ in giá trị của tham số (bất kỳ nó là cái gì) 2 lần.

This function works with any value that can be printed.
>>> print_twice('Spam')
Spam
Spam
>>> print_twice(17)
17
17
>>> print_twice(math.pi)
3.14159265359
3.14159265359

The same rules of composition that apply to built-in functions also apply to user-defined functions,
so we can use any kind of expression as an argument for print_twice:

Các quy tắc tương tự về thành phần áp dụng cho các hàm được xây dựng cũng áp dụng cho các hàm do người dùng định nghĩa, vì vậy chúng ta có thể sử dụng bất kỳ dạng biểu thức nào như một đối số cho print_twice:

>>> print_twice('Spam '*4)
Spam Spam Spam Spam
Spam Spam Spam Spam
>>> print_twice(math.cos(math.pi))
-1.0
-1.0
The argument is evaluated before the function is called, so in the examples the expressions
'Spam '*4 and math.cos(math.pi) are only evaluated once.
Đối số được xác định trướck hi hàm được gọi, ví dụ này là biểu thức "spam"*4 và math.cos(math.pi) chỉ được đánh giá một lần duy nhất.

You can also use a variable as an argument:
>>> michael = 'Eric, the half a bee.'
>>> print_twice(michael)
Eric, the half a bee.
Eric, the half a bee.
The name of the variable we pass as an argument (michael) has nothing to do with the name of the
parameter (bruce). It doesn’t matter what the value was called back home (in the caller); here in
print_twice, we call everybody bruce.

Tên của biến mà chúng ta gán như một đối số (michael) không có liên quan gì với cái tên của tham số. Nô không phải là vấn đề giá trị được gọi lại (trong lời gọi); đây là in 2 lần, chúng ta (có thể) gọi mọi thứ là bruce.

3.9 Variables and parameters are local - Biến và tham số vùng.
When you create a variable inside a function, it is local, which means that it only exists inside the
function.
Khi bạn tạo 1 biến trong hàm, nó có tính khu vực, điều này nghĩa là nó chỉ có nghĩa khi ở trong hàm
For example:
def cat_twice(part1, part2):
cat = part1 + part2
print_twice(cat)
This function takes two arguments, concatenates them, and prints the result twice. Here is an example that uses it:
3.10. Stack diagrams 23
>>> line1 = 'Bing tiddle '
>>> line2 = 'tiddle bang.'
>>> cat_twice(line1, line2)
Bing tiddle tiddle bang.
Bing tiddle tiddle bang.
When cat_twice terminates, the variable cat is destroyed. If we try to print it, we get an exception:
>>> print(cat)
NameError: name 'cat' is not defined
Parameters are also local. For example, outside print_twice, there is no such thing as bruce
Tham số cũng có tính khuvực, ví dụ kết quả hiển thị 2 lần, ở đây không có nghĩa gì - liên quan gì như bruce.

Nhận xét

Bài đăng phổ biến từ blog này

2.7. Operators and Operands - toán tử và toán hạng

1.11. Formal and Natural Languages