ทำความรู้จักกับภาษา python (20) : การ return (รีเทิน) ค่าออกจากฟังก์ชัน

ตอบกระทู้

รูปแสดงอารมณ์
: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 (20) : การ return (รีเทิน) ค่าออกจากฟังก์ชัน

Re: ทำความรู้จักกับภาษา python (20) : การ return (รีเทิน) ค่าออกจากฟังก์ชัน

โดย MBMoo » 08/06/2020 7:21 pm

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

def bo(b1,b2):
    bo = b1 +b2
    print ("ออก1",bo)
    return bo

bow = bo(2,3)
print("ออก2",bow)
ผลลัพธ์
Python Knowledge-1.png
Python Knowledge-1.png (1.3 KiB) Viewed 378 times

Re: ทำความรู้จักกับภาษา python (20) : การ return (รีเทิน) ค่าออกจากฟังก์ชัน

โดย jirawoot » 20/06/2019 4:21 pm

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

def myfunc(a1,a2):
    total = a1+a2
    print ("ตัวแปรภายในฟังก์ชั่น",total)
    return total

total = myfunc(10,20)
print ("ตัวแปรนอกฟังก์ชั่น ",total)
ผลลัพธ์
Selection_022.png
Selection_022.png (10.15 KiB) Viewed 496 times

Re: ทำความรู้จักกับภาษา python (20) : การ return (รีเทิน) ค่าออกจากฟังก์ชัน

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

การสร้างฟังก์ชั่น ส่งค่าออกจากฟังก์ชั่นไพทอน The return Statement:

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

total = 10
def myfucn1(arg1,arg2):
    a = 15
    total = arg1 + arg2
    print("ตัวแปรภายในฟังก์ชัน :",a)
    print("ตัวแปร total ที่อยู่ภายในฟังก์ชัน :", total)
    return total

total = myfucn1(10,20)
print("ตัวแปร total ที่อยู่ภายนอกฟังก์ชัน :",total)
ผลลัพธ์
Selection_012.png
Selection_012.png (12.93 KiB) Viewed 872 times

Re: ทำความรู้จักกับภาษา python (20) : การ return (รีเทิน) ค่าออกจากฟังก์ชัน

โดย prakon » 06/07/2018 3:27 pm

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

total = 10
def myfunc(ag1,ag2):
    total = ag1+ag2
    print("ค่าในฟังก์ชั่น = '%d'"%total)
    return total
total = myfunc(10,20)
print("ค่านอกฟังก์ชั่นจะเท่ากับ'%d'"%total)
สาเหตุที่ค่า total ไมเป็น 10 เพราะในฟังก์ชั่น อาร์กิวเมนทฺภายใน ได้ถูก กำหนดค่ามาจากด้านนอกแล้ว

Re: ทำความรู้จักกับภาษา python (20) : การ return (รีเทิน) ค่าออกจากฟังก์ชัน

โดย rangsan » 03/05/2018 1:19 pm

การ return (รีเทิน) ค่าออกจากฟังก์ชัน

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

def myfuncReturn( arg1 , arg2 , arg3 , arg4 ):
    total1 = ( arg1 * arg2 ) + ( arg3 * arg4 )
    total2 = arg1 + arg2 + arg3 + arg4 
    total = total1+total2
    print "Output of Total1 : ", total1
    print "Output of Total2 : ", total2
    return total

total = myfuncReturn( 4 , 5 , 2 , 3 )
print "--------------------------"
print "SumTotal is      : ", total

ผลการรันโค้ด
Return Statement.png
Return Statement.png (16.95 KiB) Viewed 1297 times
ศึกษาจาก : https://www.youtube.com/watch?v=T37IyoSgeP0&t=10s

Re: ทำความรู้จักกับภาษา python (20) : การ return (รีเทิน) ค่าออกจากฟังก์ชัน

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

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

total = 10
def myfunc (arg1,arg2):
    a = 15
    total = arg1 + arg2
    print 'Variable in function is : ', a
    print '\nTotal in function is : ', total
    return total

total = myfunc(15,65)
print '\nOut of function is : ',total
ผลการรัน
Return.JPG
Return.JPG (12.68 KiB) Viewed 1321 times
ไม่มีการ return ค่าสู่ภายนอก
ไม่มีการ return ค่าสู่ภายนอก
Return1.JPG (13.4 KiB) Viewed 1321 times

เมื่อไม่มีการรีเทิร์นค่าออกมานอกฟังก์ชัน ภายนอกก็จะไม่สามารถแสดงผลลัพธ์ได้ เพราะไม่รู้ค่านั้น


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

Re: ทำความรู้จักกับภาษา python (20) : การ return (รีเทิน) ค่าออกจากฟังก์ชัน

โดย Jom07 » 25/01/2018 1:01 am

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

test = 10

def func(x, y):
    test = x + y
    print("ในฟังก์ชัน", test)
    return test

test = func(600, 900)
print("นอกฟังก์ชัน",test)
ผลรัน

รูปภาพ

ศึกษาข้อมูลจาก : https://www.youtube.com/watch?v=T37IyoS ... M&index=20

Re: ทำความรู้จักกับภาษา python (20) : การ return (รีเทิน) ค่าออกจากฟังก์ชัน

โดย Four » 23/01/2018 3:32 pm

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

num = 10

def func(a, b):
    num = a + b
    print("ในฟังก์ชัน", num)
    return num

num = func(100, 200)
print("นอกฟังก์ชัน",num)
ผลรัน

รูปภาพ

Re: ทำความรู้จักกับภาษา python (20) : การ return (รีเทิน) ค่าออกจากฟังก์ชัน

โดย Dive Demo » 03/02/2017 3:27 pm

อธิบายได้เข้าใจง่ายมากเลยครับ

ข้างบน