functions. • function เป็นชื่อของชุดคำสั่ง • syntax...

30
FUNCTIONS

Upload: chaya-wrigley

Post on 31-Mar-2015

224 views

Category:

Documents


5 download

TRANSCRIPT

Page 1: FUNCTIONS. • Function เป็นชื่อของชุดคำสั่ง • Syntax ของฟังก์ชั่น Def NAME (PARAMETERS ): STATEMENTS • NAME เป็นชื่อของฟังก์ชัน

FUNCTIONS

Page 2: FUNCTIONS. • Function เป็นชื่อของชุดคำสั่ง • Syntax ของฟังก์ชั่น Def NAME (PARAMETERS ): STATEMENTS • NAME เป็นชื่อของฟังก์ชัน

FUNCTIONS

• Function เป็�นชื่��อของชื่�ดคำ��สั่��ง• Syntax ของฟั�งก์�ชื่� �น

Def NAME (PARAMETERS ): STATEMENTS

• NAME เป็�นชื่��อของฟั�งก์�ชื่�น สั่�ม�รถใชื่�ชื่��อใดๆต�ม ก์ฎก์�รต��งชื่��อ ยก์เว้�น keyword

• ม!คำ��สั่��งได�ไม#จำ��ก์�ดจำ��นว้น แต#ต�องเย��องเข��ไป็จำ�ก์def ( คำว้รใชื่�เป็�นม�ตรฐ�น อ�ทิ( 4 spaces)

Page 3: FUNCTIONS. • Function เป็นชื่อของชุดคำสั่ง • Syntax ของฟังก์ชั่น Def NAME (PARAMETERS ): STATEMENTS • NAME เป็นชื่อของฟังก์ชัน

FUNCTION PATTERN

• A header line which begins with a keyword and ends with colon.

• A body consisting of one or more statements, each indented, each indented the same amount (4 spaces) from the head line.

def NAME(PARAMETERS) : STATEMENTS

Page 4: FUNCTIONS. • Function เป็นชื่อของชุดคำสั่ง • Syntax ของฟังก์ชั่น Def NAME (PARAMETERS ): STATEMENTS • NAME เป็นชื่อของฟังก์ชัน
Page 5: FUNCTIONS. • Function เป็นชื่อของชุดคำสั่ง • Syntax ของฟังก์ชั่น Def NAME (PARAMETERS ): STATEMENTS • NAME เป็นชื่อของฟังก์ชัน

The parameters of the function• draw_square has two parameter– One to tell the function which turtle to move

around– The other to tell it the size of the square

Page 6: FUNCTIONS. • Function เป็นชื่อของชุดคำสั่ง • Syntax ของฟังก์ชั่น Def NAME (PARAMETERS ): STATEMENTS • NAME เป็นชื่อของฟังก์ชัน

Run the function

• Defining a new function does not make the function run.

• To run the function, we need a function call

Page 7: FUNCTIONS. • Function เป็นชื่อของชุดคำสั่ง • Syntax ของฟังก์ชั่น Def NAME (PARAMETERS ): STATEMENTS • NAME เป็นชื่อของฟังก์ชัน

Function Call

Page 8: FUNCTIONS. • Function เป็นชื่อของชุดคำสั่ง • Syntax ของฟังก์ชั่น Def NAME (PARAMETERS ): STATEMENTS • NAME เป็นชื่อของฟังก์ชัน
Page 9: FUNCTIONS. • Function เป็นชื่อของชุดคำสั่ง • Syntax ของฟังก์ชั่น Def NAME (PARAMETERS ): STATEMENTS • NAME เป็นชื่อของฟังก์ชัน

Functions can call other functions• We want function to draw a rectangle.• We need to be able to call the function with

different argument for width and height. And, unlike the case of the square.

Page 10: FUNCTIONS. • Function เป็นชื่อของชุดคำสั่ง • Syntax ของฟังก์ชั่น Def NAME (PARAMETERS ): STATEMENTS • NAME เป็นชื่อของฟังก์ชัน

A new version of draw_square• A square is a special kind of rectangle.• We can use draw_rectangle function to draw a

square.• Functions can call other functions.

Page 11: FUNCTIONS. • Function เป็นชื่อของชุดคำสั่ง • Syntax ของฟังก์ชั่น Def NAME (PARAMETERS ): STATEMENTS • NAME เป็นชื่อของฟังก์ชัน

• A caller of this function might say draw_square(tess, 50)• tx and sz are assigned the values of the tess

object and the int 50

Function Call

Page 12: FUNCTIONS. • Function เป็นชื่อของชุดคำสั่ง • Syntax ของฟังก์ชั่น Def NAME (PARAMETERS ): STATEMENTS • NAME เป็นชื่อของฟังก์ชัน

Flow of execution

• Execution always begins as the first statement of the program.

• Statements are executed one at a time, in order from top to bottom.

• The statements inside the function are not executed until the function is called.

Page 13: FUNCTIONS. • Function เป็นชื่อของชุดคำสั่ง • Syntax ของฟังก์ชั่น Def NAME (PARAMETERS ): STATEMENTS • NAME เป็นชื่อของฟังก์ชัน

Put the mouse cursor on the line of the program where create the turtle screen and press F4 key

Page 14: FUNCTIONS. • Function เป็นชื่อของชุดคำสั่ง • Syntax ของฟังก์ชั่น Def NAME (PARAMETERS ): STATEMENTS • NAME เป็นชื่อของฟังก์ชัน

At this point press the F7 key repeatedly to single step through the code.

When start to get bored ,we can use the key F8 to step over the function

Page 15: FUNCTIONS. • Function เป็นชื่อของชุดคำสั่ง • Syntax ของฟังก์ชั่น Def NAME (PARAMETERS ): STATEMENTS • NAME เป็นชื่อของฟังก์ชัน
Page 16: FUNCTIONS. • Function เป็นชื่อของชุดคำสั่ง • Syntax ของฟังก์ชั่น Def NAME (PARAMETERS ): STATEMENTS • NAME เป็นชื่อของฟังก์ชัน
Page 17: FUNCTIONS. • Function เป็นชื่อของชุดคำสั่ง • Syntax ของฟังก์ชั่น Def NAME (PARAMETERS ): STATEMENTS • NAME เป็นชื่อของฟังก์ชัน
Page 18: FUNCTIONS. • Function เป็นชื่อของชุดคำสั่ง • Syntax ของฟังก์ชั่น Def NAME (PARAMETERS ): STATEMENTS • NAME เป็นชื่อของฟังก์ชัน
Page 19: FUNCTIONS. • Function เป็นชื่อของชุดคำสั่ง • Syntax ของฟังก์ชั่น Def NAME (PARAMETERS ): STATEMENTS • NAME เป็นชื่อของฟังก์ชัน
Page 20: FUNCTIONS. • Function เป็นชื่อของชุดคำสั่ง • Syntax ของฟังก์ชั่น Def NAME (PARAMETERS ): STATEMENTS • NAME เป็นชื่อของฟังก์ชัน
Page 21: FUNCTIONS. • Function เป็นชื่อของชุดคำสั่ง • Syntax ของฟังก์ชั่น Def NAME (PARAMETERS ): STATEMENTS • NAME เป็นชื่อของฟังก์ชัน
Page 22: FUNCTIONS. • Function เป็นชื่อของชุดคำสั่ง • Syntax ของฟังก์ชั่น Def NAME (PARAMETERS ): STATEMENTS • NAME เป็นชื่อของฟังก์ชัน

Functions that require arguments

• Most functions require arguments

• Some function takes more than one arguments

Page 23: FUNCTIONS. • Function เป็นชื่อของชุดคำสั่ง • Syntax ของฟังก์ชั่น Def NAME (PARAMETERS ): STATEMENTS • NAME เป็นชื่อของฟังก์ชัน

Functions that return values• A function that returns a value is

called a fruitful function• The opposite of fruitful function is

void function : one that is not executed for its resulting value, but is executed because it does something useful.

Page 24: FUNCTIONS. • Function เป็นชื่อของชุดคำสั่ง • Syntax ของฟังก์ชั่น Def NAME (PARAMETERS ): STATEMENTS • NAME เป็นชื่อของฟังก์ชัน

Void functions

• Python always wants to return something. So if the programmer doesn’t arrange to return a value, Python will automatically return the value None.

Page 25: FUNCTIONS. • Function เป็นชื่อของชุดคำสั่ง • Syntax ของฟังก์ชั่น Def NAME (PARAMETERS ): STATEMENTS • NAME เป็นชื่อของฟังก์ชัน

Variables and parameters are local

• When a local variable is created inside a function, it only exists inside the function, we cannot use it outside

• Cannot use the variable a outside the function

Page 26: FUNCTIONS. • Function เป็นชื่อของชุดคำสั่ง • Syntax ของฟังก์ชั่น Def NAME (PARAMETERS ): STATEMENTS • NAME เป็นชื่อของฟังก์ชัน

Variable lifetime

• The variable a only exists while the function is being executed.

Page 27: FUNCTIONS. • Function เป็นชื่อของชุดคำสั่ง • Syntax ของฟังก์ชั่น Def NAME (PARAMETERS ): STATEMENTS • NAME เป็นชื่อของฟังก์ชัน

Parameters• Parameters are local and act like local

variables.• The lifetimes of p, r, n, t – begin when final_amt is called– Ends when the function completes its execution.

Page 28: FUNCTIONS. • Function เป็นชื่อของชุดคำสั่ง • Syntax ของฟังก์ชั่น Def NAME (PARAMETERS ): STATEMENTS • NAME เป็นชื่อของฟังก์ชัน

Each call of the function

• Each call of the function creates new local variables.

• And their lifetimes expire when the function returns to the caller.

Page 29: FUNCTIONS. • Function เป็นชื่อของชุดคำสั่ง • Syntax ของฟังก์ชั่น Def NAME (PARAMETERS ): STATEMENTS • NAME เป็นชื่อของฟังก์ชัน
Page 30: FUNCTIONS. • Function เป็นชื่อของชุดคำสั่ง • Syntax ของฟังก์ชั่น Def NAME (PARAMETERS ): STATEMENTS • NAME เป็นชื่อของฟังก์ชัน