Định nghĩa hàm chỉ đặt tên, chỉ định tham số và cấu trúc khối mã.
Khi cấu trúc cơ bản của hàm hoàn tất, bạn có thể thực thi nó bằng cách gọi từ hàm khác hoặc trực tiếp từ Python prompt. Ví dụ gọi hàm printme():
#!/usr/bin/python
# Định nghĩa hàm
def printme(str):
"This prints a passed string into this function"
print str
return
# Gọi printme
printme("I'm first call to user defined function!")
printme("Again second call to the same function")
Kết quả
Khi mã chạy, kết quả:
I'm first call to user defined function! Again second call to the same function