More on Defining a function€¦ · More on Defining a function ... ในภาษาPython...

17
Computer Science, CMU More on Defining a function CS101: Introduction to Computer

Transcript of More on Defining a function€¦ · More on Defining a function ... ในภาษาPython...

Page 1: More on Defining a function€¦ · More on Defining a function ... ในภาษาPython สามารถท าได้โดยใช้syntax ... หมายความถึง

Computer Science, CMU

More on Defining a function

CS101: Introduction to Computer

Page 2: More on Defining a function€¦ · More on Defining a function ... ในภาษาPython สามารถท าได้โดยใช้syntax ... หมายความถึง

Computer Science, CMU

การนยามฟงกชน คอการตงชอให กลมของค าสง เพอทเราจะสามารถน ากลมของค าสงนนกลบมาใชไดอกในภาษา Python สามารถท าไดโดยใช syntax ตอไปน

สวนของ parameter จะมหรอไมมกได แลวแตหนาทของฟงกชนProcess แตละตวทอยในฟงกชนตองท าการยอหนาใหถกตอง

Recap

def functionName(parameter1, parameter2, ...):process1process2

. . .processN

CS101: Introduction to Computer

กลมค ำสง

Page 3: More on Defining a function€¦ · More on Defining a function ... ในภาษาPython สามารถท าได้โดยใช้syntax ... หมายความถึง

Computer Science, CMU

Global variableหมายความถง ตวแปรทประกาศไว นอก function ซงฟงกชนทกตวสามารถเรยกใชงานได

Local variableหมายความถง ตวแปรทประกาศไว ใน function รวมถงตวแปรทท ำหนำทเปน argument ดวยจะมแตฟงกชนทเปนเจาของเทานนทเรยกใชงานได

Variable scope

CS101: Introduction to Computer

Page 4: More on Defining a function€¦ · More on Defining a function ... ในภาษาPython สามารถท าได้โดยใช้syntax ... หมายความถึง

Computer Science, CMU

โปรแกรมตอไปนท างานไดหรอไม

CS101: Introduction to Computer

Page 5: More on Defining a function€¦ · More on Defining a function ... ในภาษาPython สามารถท าได้โดยใช้syntax ... หมายความถึง

Computer Science, CMU

โปรแกรมตอไปนท างานไดหรอไม

CS101: Introduction to Computer

global

เรยกใชไมได

เพรำะเปนlocal

local

local

Page 6: More on Defining a function€¦ · More on Defining a function ... ในภาษาPython สามารถท าได้โดยใช้syntax ... หมายความถึง

Computer Science, CMU

จะเกดอะไรขนกบคาตวแปรทสงเปน argument เขาไปในฟงกชน แลวมการเปลยนแปลงคาของ argument ภายในฟงกชน

What happens to function argument?

CS101: Introduction to Computer

Page 7: More on Defining a function€¦ · More on Defining a function ... ในภาษาPython สามารถท าได้โดยใช้syntax ... หมายความถึง

Computer Science, CMU

Experiment 1: โปรแกรมถกตองไหม

CS101: Introduction to Computer

Page 8: More on Defining a function€¦ · More on Defining a function ... ในภาษาPython สามารถท าได้โดยใช้syntax ... หมายความถึง

Computer Science, CMU

Experiment 2: แลวโปรแกรมนละ

CS101: Introduction to Computer

Page 9: More on Defining a function€¦ · More on Defining a function ... ในภาษาPython สามารถท าได้โดยใช้syntax ... หมายความถึง

Computer Science, CMU

Immutable

การเปลยนแปลงตอ argument ทเกดขนใน function จะคงอยเฉพาะในฟงกชนเทานน

ตวอยางของตวแปร ImmutableInt, float, long, complex (ใน experiment 1)

Str

Tuple

Bytes

Mutable

การเปลยนแปลงตอ argument ทเกดขนใน function จะสงผลตอภายนอกดวย

ตวอยางของตวแปร mutableList (ใน experiment 2)

Set

Dict

ตวแปรใน Python มสองแบบ

CS101: Introduction to Computer

Page 10: More on Defining a function€¦ · More on Defining a function ... ในภาษาPython สามารถท าได้โดยใช้syntax ... หมายความถึง

Computer Science, CMU

จงเขยนฟงกชน min_of_three(a,b,c) ซงรบ argument เปนตวเลขจ านวนเตม สามตว แลว return ตวเลขทมคานอยทสดกลบคนมา

ตวอยาง ผลลพธ

Exercise 1

CS101: Introduction to Computer

Page 11: More on Defining a function€¦ · More on Defining a function ... ในภาษาPython สามารถท าได้โดยใช้syntax ... หมายความถึง

Computer Science, CMU

โปรแกรม word processing อยาง Microsoft office มฟงกชนทผใชสามารถจดยอหนาในสวยงามไดทง จดชดซาย จดชดขวา และจดกงกลาง แบบฝกหดน ใหเขยนฟงกชนชอ right_justify(string) เพอรบขอความหนงขอความ (a string) เขามา แลว ท าการจดชดขวา ใหสวยงาม ก าหนดใหหนากระดาษมความกวาง 50 whitespaces และขอความทเขามามความยาวไมเกน 50 อกขระ ฟงกชนนไมมการ return คากลบ

Exercise 2

CS101: Introduction to Computer

Page 12: More on Defining a function€¦ · More on Defining a function ... ในภาษาPython สามารถท าได้โดยใช้syntax ... หมายความถึง

Computer Science, CMU

Exercise 2: Example

CS101: Introduction to Computer

Page 13: More on Defining a function€¦ · More on Defining a function ... ในภาษาPython สามารถท าได้โดยใช้syntax ... หมายความถึง

Computer Science, CMU

string built-in functions ฟงกชนส าเรจรปในการจดการรวมกบสตรง ไดแก ฟงกชน ljust(), rjust() และ center() โดยฟงกชนแตละตวมรปแบบไวยากรณ (syntax) ทคลายกนดงน center(len, fillchar) , rjust(len, fillchar) , ljust(len, fillchar)

โดยท len คอ ความกวางของหนากระดาษทตองการจดตวอกษร

fillchar (เปน option หรอตวเสรมไมใชกได) คอ อกขระทตองการน ามาเตมเตมชองวางทเหลอเมอมการจดต าแหนงขอความทตองการแลว

ตวอยางการใชฟงกชน ljust() : string_in = “Hello Python”

string_in.ljust(20) ผลลพธคอ ‘Hello Python ’

string_in.ljust(20,’#’) ผลลพธคอ ‘Hello Python########### ’

Exercise 2: String built-in functions

CS101: Introduction to Computer

Page 14: More on Defining a function€¦ · More on Defining a function ... ในภาษาPython สามารถท าได้โดยใช้syntax ... หมายความถึง

Computer Science, CMU

ป 2050 มนษยชาตคนพบดาว UN-11S ทนาตนเตนกวานนสงมชวตบนดาวนใชภาษาองกฤษในการสอสาร ถงแมวาจะใชภาษาเดยวกนแตวฒนธรรมทางภาษาไมเหมอนกน สงมชวตบนดาวนเนนสขภาพมาก ฉะนนค าวา butter cake chip hamburger sugar ถอเปนค าทไมสภาพอยางมาก เพอการสอสารกบสงมชวตดงกลาวใหเปนไปอยางราบรน เราจะตอง censor ค าทมค าตองหาม 5 ค าดงกลาวเปนสวนประกอบ โดยหากพบค าทวาในประโยค ใหท าการ censor โดยแปลงอกษรตวท 2 ถง n-1 เปนเครองหมาย * เรมจากโครงรางโปรแกรมทมมาให จงเขยนเตมฟงกชนทชอ censor() ทรบประโยคภาษาองกฤษเขาไปแลว สงประโยคทท าการเซนเซอรแลวกลบคนมาเชน Cake, cakes, CAKES, หรอ cheesecake ถอวาเปนค าไมสภาพเหมอนกน

Exercise 3

CS101: Introduction to Computer

Page 15: More on Defining a function€¦ · More on Defining a function ... ในภาษาPython สามารถท าได้โดยใช้syntax ... หมายความถึง

Computer Science, CMU

Exercise 3: Code template

CS101: Introduction to Computer

Page 16: More on Defining a function€¦ · More on Defining a function ... ในภาษาPython สามารถท าได้โดยใช้syntax ... หมายความถึง

Computer Science, CMU

Input: I will bake butter cakes for you

Output: I will bake b****r c***s for you

Input: I hate fast food

Output: I hate fast food

Input: Hamburger or cheeseburger ?

Output: H*******r or cheeseb****r ?

Exercise 3 ตวอยาง

CS101: Introduction to Computer

Page 17: More on Defining a function€¦ · More on Defining a function ... ในภาษาPython สามารถท าได้โดยใช้syntax ... หมายความถึง

Computer Science, CMU

Hands-on Python tutorialanh.cs.luc.edu/python/hands-on/3.1/handsonHtml/functions.html

Think Python: How to Think Like a Computer Scientist

References

CS101: Introduction to Computer