ทำความรู้จักกับภาษา python (17) : การสร้างฟังก์ชันโดยมี Keyword

แชร์ความรู้ภาษา Python ไพทอน การเขียนโปรแกรมภาษาไพทอน

Moderator: mindphp, ผู้ดูแลกระดาน

dawthana
PHP Super Hero Member
PHP Super Hero Member
โพสต์: 604
ลงทะเบียนเมื่อ: 07/12/2016 10:55 am

ทำความรู้จักกับภาษา python (17) : การสร้างฟังก์ชันโดยมี Keyword

โพสต์ที่ยังไม่ได้อ่าน โดย 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
keyword_1.png (25.51 KiB) Viewed 1172 times
keyword_2.png
keyword_2.png (26.79 KiB) Viewed 1172 times
สรุปการสร้างฟังก์ชันโดยมี Keyword
- รูปแบบการเขียนฟังก์ชันโดยมี Keyword ขึ้นต้นด้วย "def" ตามด้วย "ชื่อของฟังก์ชัน ( )" และเครื่องหมาย ":"
- ไม่ต้องเรียกใช้ argument ตามลำดับ
- เมื่อมี argument หลายตัวจะไม่สับสน
- ถ้ามีการข้ามหรือสลับ argument ก็ยังสามารถแทนค่าได้ถูก เพราะมีการระบุค่า Keyword ที่ตรงตัว
- กรณีที่ไม่ได้ระบุ Keyword ให้กับ argument จะทำงานเรียงตามลำดับเหมือนเดิม

ศึกษาข้อมูลมาจาก https://www.youtube.com/watch?v=4cI5sSk ... M&index=17
แก้ไขล่าสุดโดย dawthana เมื่อ 24/01/2017 5:45 pm, แก้ไขไปแล้ว 2 ครั้ง.
dawthana
PHP Super Hero Member
PHP Super Hero Member
โพสต์: 604
ลงทะเบียนเมื่อ: 07/12/2016 10:55 am

Re: ทำความรู้จักกับภาษา python (17) : การสร้างฟังก์ชันโดยมี Keyword

โพสต์ที่ยังไม่ได้อ่าน โดย dawthana »

สามารถศึกษาข้อมูลเพิ่มเติมได้ที่ https://www.mindphp.com/vdo-tutorial-python/ ... yword.html
Dive Demo
PHP Sr. Member
PHP Sr. Member
โพสต์: 84
ลงทะเบียนเมื่อ: 01/02/2017 11:10 am

Re: ทำความรู้จักกับภาษา python (17) : การสร้างฟังก์ชันโดยมี Keyword

โพสต์ที่ยังไม่ได้อ่าน โดย Dive Demo »

อธิบายไว้ดีมากครับ มีการยกตัวเองการใช้งานด้วยทำให้เข้าใจง่ายครับ
Four
PHP Super Hero Member
PHP Super Hero Member
โพสต์: 813
ลงทะเบียนเมื่อ: 08/01/2018 9:55 am

Re: ทำความรู้จักกับภาษา python (17) : การสร้างฟังก์ชันโดยมี Keyword

โพสต์ที่ยังไม่ได้อ่าน โดย 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")
ผลรัน

รูปภาพ
I am slow walker, but I never walk back. (Abraham Lincoln)
ภาพประจำตัวสมาชิก
Jom07
PHP Super Hero Member
PHP Super Hero Member
โพสต์: 514
ลงทะเบียนเมื่อ: 08/01/2018 9:56 am

Re: ทำความรู้จักกับภาษา python (17) : การสร้างฟังก์ชันโดยมี Keyword

โพสต์ที่ยังไม่ได้อ่าน โดย 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
รูปภาพ
Patcharanan.0399
PHP Hero Member
PHP Hero Member
โพสต์: 114
ลงทะเบียนเมื่อ: 09/04/2018 10:04 am

Re: ทำความรู้จักกับภาษา python (17) : การสร้างฟังก์ชันโดยมี Keyword

โพสต์ที่ยังไม่ได้อ่าน โดย 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
Default Keyword.JPG (15.06 KiB) Viewed 1089 times
เรียกใช้ฟังก์ชันครั้งที่ 1 กำหนดข้อมูลให้แต่ไม่ได้ใส่ keyword การแสดงผลก็จะตามลำดับ
เรียกใช้ฟังก์ชันครั้งที่ 2 ได้ทำการกำหนด keyword ให้กับข้อมูลด้วย ทำให้ไม่จำเป็นต้องว่างข้อมูลตามลำดับ แต่การแสดงผลก็จะแสดงตามที่ลำดับได้


ศึกษาจาก https://youtu.be/4cI5sSkOJ_8
rangsan
PHP Hero Member
PHP Hero Member
โพสต์: 199
ลงทะเบียนเมื่อ: 30/04/2018 9:44 am

Re: ทำความรู้จักกับภาษา python (17) : การสร้างฟังก์ชันโดยมี Keyword

โพสต์ที่ยังไม่ได้อ่าน โดย 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
keyWord_function.png (42.88 KiB) Viewed 1077 times
ศึกษาจาก : https://www.youtube.com/watch?v=4cI5sSk ... lzdKrpxsMM
It’s never too late to start again.
prakon
PHP Super Hero Member
PHP Super Hero Member
โพสต์: 561
ลงทะเบียนเมื่อ: 02/07/2018 10:45 am

Re: ทำความรู้จักกับภาษา python (17) : การสร้างฟังก์ชันโดยมี Keyword

โพสต์ที่ยังไม่ได้อ่าน โดย 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()
แนบไฟล์
run1.png
run1.png (1.63 KiB) Viewed 1053 times
anuwat somsakul
PHP Full Member
PHP Full Member
โพสต์: 44
ลงทะเบียนเมื่อ: 08/08/2018 9:49 am

Re: ทำความรู้จักกับภาษา python (17) : การสร้างฟังก์ชันโดยมี Keyword

โพสต์ที่ยังไม่ได้อ่าน โดย 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
Selection_009.png (11.35 KiB) Viewed 746 times
ภาพประจำตัวสมาชิก
chatee supasand
PHP VIP Members
PHP VIP Members
โพสต์: 1666
ลงทะเบียนเมื่อ: 04/06/2019 10:06 am

Re: ทำความรู้จักกับภาษา python (17) : การสร้างฟังก์ชันโดยมี Keyword

โพสต์ที่ยังไม่ได้อ่าน โดย chatee supasand »

ทดลองทำตามแล้ว เข้าใจง่ายมากครับ สอนได้เข้าใจมากๆ
รูปภาพ
ตอบกลับโพส
  • Similar Topics
    ตอบกลับ
    แสดง
    โพสต์ล่าสุด

ผู้ใช้งานขณะนี้

สมาชิกกำลังดูบอร์ดนี้: ไม่มีสมาชิกใหม่ และบุคลทั่วไป 93