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

ตอบกระทู้

รูปแสดงอารมณ์
:icon_plusone: :like: :plusone: :gfb: :-D :) :( :-o 8O :? 8) :lol: :x :P :oops: :cry: :evil: :twisted: :roll: :wink: :!: :?: :idea: :arrow: :| :mrgreen: :angry: :baa: :biggrin:
รูปแสดงอารมณ์อื่นๆ

BBCode เปิด
[img] เปิด
[url] เปิด
[Smile icon] เปิด

กระทู้แนะนำ
   

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

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

โดย jirawoot » 20/06/2019 3:19 pm

โค้ด: เลือกทั้งหมด

def mykey(i,phone='phone number is not', email='email is not'):
    print('ID = ',i)
    print('phone =',phone)
    print('email=', email)

mykey('001','0844235555')
ผลลัพธ์
Selection_018.png
Selection_018.png (7.22 KiB) Viewed 409 times

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

โดย chatee supasand » 10/06/2019 2:04 pm

ทดลองทำตามแล้ว เข้าใจง่ายมากครับ สอนได้เข้าใจมากๆ

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

โดย anuwat somsakul » 10/08/2018 3:30 pm

การสร้างฟังก์ชั่น ในไพทอน 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 771 times

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

โดย prakon » 05/07/2018 6:54 pm

การเรียกใช้งานฟังก์ชั่นต้องมีอย่างน้อย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 1076 times

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

โดย rangsan » 03/05/2018 11:58 am

การสร้างฟังก์ชันโดยมี 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 1100 times
ศึกษาจาก : https://www.youtube.com/watch?v=4cI5sSk ... lzdKrpxsMM

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

โดย Patcharanan.0399 » 20/04/2018 10:10 am

โค้ด: เลือกทั้งหมด

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 1112 times
เรียกใช้ฟังก์ชันครั้งที่ 1 กำหนดข้อมูลให้แต่ไม่ได้ใส่ keyword การแสดงผลก็จะตามลำดับ
เรียกใช้ฟังก์ชันครั้งที่ 2 ได้ทำการกำหนด keyword ให้กับข้อมูลด้วย ทำให้ไม่จำเป็นต้องว่างข้อมูลตามลำดับ แต่การแสดงผลก็จะแสดงตามที่ลำดับได้


ศึกษาจาก https://youtu.be/4cI5sSkOJ_8

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

โดย Jom07 » 25/01/2018 12:13 am

การสร้างฟังก์ชันโดยมี 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

โดย Four » 22/01/2018 11:32 pm

โค้ด: เลือกทั้งหมด

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

โดย Dive Demo » 03/02/2017 12:08 pm

อธิบายไว้ดีมากครับ มีการยกตัวเองการใช้งานด้วยทำให้เข้าใจง่ายครับ

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

โดย dawthana » 09/01/2017 3:13 pm

สามารถศึกษาข้อมูลเพิ่มเติมได้ที่ https://www.mindphp.com/vdo-tutorial-python/ ... yword.html

ข้างบน