การกำหนดฟังก์ชันเพียงแค่ให้มันชื่อ ระบุพารามิเตอร์ที่จะรวมอยู่ในฟังก์ชัน และจัดโครงสร้างบล็อกโค้ด
เมื่อโครงสร้างพื้นฐานของฟังก์ชันเสร็จสมบูรณ์ คุณสามารถดำเนินการได้โดยการเรียกมันจากฟังก์ชันอื่นหรือโดยตรงจาก Python prompt ตัวอย่างต่อไปเป็นตัวอย่างการเรียกฟังก์ชัน printme():
#!/usr/bin/python
# Function definition is here
def printme(str):
"This prints a passed string into this function"
print str
return;
# Now you can call printme function
printme("I'm first call to user defined function!")
printme("Again second call to the same function")
ผลลัพธ์ (Output)
เมื่อรันโค้ดข้างต้น จะได้ผลลัพธ์ดังนี้:
I'm first call to user defined function!
Again second call to the same function