Computer >> บทช่วยสอนคอมพิวเตอร์ >  >> การเขียนโปรแกรม >> Python

อาร์กิวเมนต์ที่จำเป็น (Required arguments) ใน Python

Required arguments คืออาร์กิวเมนต์ที่ส่งผ่านให้ฟังก์ชันในลำดับตำแหน่งที่ถูกต้อง ที่นี่ จำนวนอาร์กิวเมนต์ในการเรียกฟังก์ชันควรตรงกับการกำหนดฟังก์ชันทุกประการ

เพื่อเรียกฟังก์ชัน printme() คุณต้องส่งอาร์กิวเมนต์อย่างน้อยหนึ่งตัว มิฉะนั้นจะให้ syntax error ดังนี้:

ตัวอย่าง

#!/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()

ผลลัพธ์ (Output)

เมื่อรันโค้ดข้างต้น จะได้ผลลัพธ์ดังนี้:

Traceback (most recent call last):
  File "test.py", line 11, in 
    printme();
TypeError: printme() takes exactly 1 argument (0 given)