หน้า 1 จากทั้งหมด 2
ทำความรู้จักกับภาษา python (17) : การสร้างฟังก์ชันโดยมี Keyword
โพสต์แล้ว: 09/01/2017 3:12 pm
โดย dawthana
การสร้างฟังก์ชันโดยมี Keyword ใน python (ไพทอน)
การสร้าง
ฟังก์ชันโดยมี
Keyword (คีย์เวิร์ด) คือการกำหนด Keyword ให้กับ argument (อาร์กิวเม้นต์) เพื่อในกรณีที่มี argument หลายๆตัว จะไม่ก่อให้เกิดความสับสนและไม่ต้องเรียกใช้งาน argument ตามลำดับ
รูปแบบการสร้าง Keyword Argument (คีย์เวิร์ด อาร์กิวเม้นต์)
โค้ด: เลือกทั้งหมด
def function (keyword1 = arg1 , keyword 2 = arg2 , ...) :
"คำอธิบายฟังก์ชัน"
statement (s)
.........................
.........................
ตัวอย่างการสร้างรูปแบบ Keyword Argument
จะเห็นได้ว่า ถ้าไม่ได้กำหนด คีย์เวิร์ดให้ Argumentจะได้ผลลัพธ์เรียงตามลำดับ ทำให้ข้อมูลที่ได้รับไม่ตรงกับข้อมูลที่ต้องการ
- keyword_1.png (25.51 KiB) Viewed 1190 times
- keyword_2.png (26.79 KiB) Viewed 1190 times
สรุปการสร้างฟังก์ชันโดยมี Keyword
- รูปแบบการเขียนฟังก์ชันโดยมี Keyword ขึ้นต้นด้วย "def" ตามด้วย "ชื่อของฟังก์ชัน ( )" และเครื่องหมาย ":"
- ไม่ต้องเรียกใช้ argument ตามลำดับ
- เมื่อมี argument หลายตัวจะไม่สับสน
- ถ้ามีการข้ามหรือสลับ argument ก็ยังสามารถแทนค่าได้ถูก เพราะมีการระบุค่า Keyword ที่ตรงตัว
- กรณีที่ไม่ได้ระบุ Keyword ให้กับ argument จะทำงานเรียงตามลำดับเหมือนเดิม
ศึกษาข้อมูลมาจาก
https://www.youtube.com/watch?v=4cI5sSk ... M&index=17
Re: ทำความรู้จักกับภาษา python (17) : การสร้างฟังก์ชันโดยมี Keyword
โพสต์แล้ว: 09/01/2017 3:13 pm
โดย dawthana
Re: ทำความรู้จักกับภาษา python (17) : การสร้างฟังก์ชันโดยมี Keyword
โพสต์แล้ว: 03/02/2017 12:08 pm
โดย Dive Demo
อธิบายไว้ดีมากครับ มีการยกตัวเองการใช้งานด้วยทำให้เข้าใจง่ายครับ
Re: ทำความรู้จักกับภาษา python (17) : การสร้างฟังก์ชันโดยมี Keyword
โพสต์แล้ว: 22/01/2018 11:32 pm
โดย Four
โค้ด: เลือกทั้งหมด
def testfunc(name="Not entered", sex="Not entered", age=30):
print ("name :", name)
print ("sex :", sex)
print ("age :", age)
testfunc(name="test", age="24")
testfunc("a", "male")
ผลรัน
Re: ทำความรู้จักกับภาษา python (17) : การสร้างฟังก์ชันโดยมี Keyword
โพสต์แล้ว: 25/01/2018 12:13 am
โดย Jom07
การสร้างฟังก์ชันโดยมี Keyword
โค้ด: เลือกทั้งหมด
def kyefunc(name="Not entered", sex="Not entered", age=25):
print ("name :", name)
print ("sex :", sex)
print ("age :", age)
kyefunc(name="kye", age="20")
kyefunc("a", "male")
ผลรัน
ศึกษาข้อมูลจาก :
https://www.youtube.com/watch?v=4cI5sSk ... lzdKrpxsMM
Re: ทำความรู้จักกับภาษา python (17) : การสร้างฟังก์ชันโดยมี Keyword
โพสต์แล้ว: 20/04/2018 10:10 am
โดย Patcharanan.0399
โค้ด: เลือกทั้งหมด
def mykey(i, phone = "Phone number is not specified", email = "Email is not specified\n"):
print 'ID : ', i
print 'Phone is : ', phone
print 'Email is : ', email
mykey ('001', '02-4567891')
mykey (email = '[email protected]', i = '002')
ผลการรัน
- Default Keyword.JPG (15.06 KiB) Viewed 1107 times
เรียกใช้ฟังก์ชันครั้งที่ 1 กำหนดข้อมูลให้แต่ไม่ได้ใส่ keyword การแสดงผลก็จะตามลำดับ
เรียกใช้ฟังก์ชันครั้งที่ 2 ได้ทำการกำหนด keyword ให้กับข้อมูลด้วย ทำให้ไม่จำเป็นต้องว่างข้อมูลตามลำดับ แต่การแสดงผลก็จะแสดงตามที่ลำดับได้
ศึกษาจาก
https://youtu.be/4cI5sSkOJ_8
Re: ทำความรู้จักกับภาษา python (17) : การสร้างฟังก์ชันโดยมี Keyword
โพสต์แล้ว: 03/05/2018 11:58 am
โดย rangsan
การสร้างฟังก์ชันโดยมี Keyword
โค้ด: เลือกทั้งหมด
def keyWordFunction(ID,Name="Name not found",Lastname="Lastname not found",Gender="Gender is not defind",Phone="Phone is not defind",Email="Email is not defind",Address="Address is not defind"):
print "Go to Function"
print ("---ID : ",ID)
print ("---Name : ",Name)
print ("---Lastname: ",Lastname)
print ("---Gender : ",Gender)
print ("---Phone : ",Phone)
print ("---Email : ",Email,"!!")
print ("---Address : ",Address)
keyWordFunction(58,'Rangsan','Mungklang','Male',Phone='08-78888898',Address="88/99 Hongprayoun Village Nonthaburi Bangbuathong 11110")
print "---Finish Function---”
ผลการรันโค้ด
- keyWord_function.png (42.88 KiB) Viewed 1095 times
ศึกษาจาก :
https://www.youtube.com/watch?v=4cI5sSk ... lzdKrpxsMM
Re: ทำความรู้จักกับภาษา python (17) : การสร้างฟังก์ชันโดยมี Keyword
โพสต์แล้ว: 05/07/2018 6:54 pm
โดย prakon
การเรียกใช้งานฟังก์ชั่นต้องมีอย่างน้อย1argument
โค้ด: เลือกทั้งหมด
def myfunc(name="no name",age='no age'):
print("name '%s' age '%s'"%(name,age))
return 0
myfunc(age="23",name=("yun"))
myfunc()
Re: ทำความรู้จักกับภาษา python (17) : การสร้างฟังก์ชันโดยมี Keyword
โพสต์แล้ว: 10/08/2018 3:30 pm
โดย anuwat somsakul
การสร้างฟังก์ชั่น ในไพทอน Python Defining function โดยมี Keyword Arguments
โค้ด: เลือกทั้งหมด
def mykey(i,phone="Phone number is not specified",email="Email is not specified"):
print("-- ID",i)
print("-- phone",phone)
print("-- keyword arg email:",email,"!")
mykey('001','0485255455')
mykey(email='[email protected]',i=415525)
ผลลัพธ์
- Selection_009.png (11.35 KiB) Viewed 765 times
Re: ทำความรู้จักกับภาษา python (17) : การสร้างฟังก์ชันโดยมี Keyword
โพสต์แล้ว: 10/06/2019 2:04 pm
โดย chatee supasand
ทดลองทำตามแล้ว เข้าใจง่ายมากครับ สอนได้เข้าใจมากๆ