![Page 1: FUNCTIONS. • Function เป็นชื่อของชุดคำสั่ง • Syntax ของฟังก์ชั่น Def NAME (PARAMETERS ): STATEMENTS • NAME เป็นชื่อของฟังก์ชัน](https://reader035.vdocuments.pub/reader035/viewer/2022062404/551a6ad05503463e778b5dc1/html5/thumbnails/1.jpg)
FUNCTIONS
![Page 2: FUNCTIONS. • Function เป็นชื่อของชุดคำสั่ง • Syntax ของฟังก์ชั่น Def NAME (PARAMETERS ): STATEMENTS • NAME เป็นชื่อของฟังก์ชัน](https://reader035.vdocuments.pub/reader035/viewer/2022062404/551a6ad05503463e778b5dc1/html5/thumbnails/2.jpg)
FUNCTIONS
• Function เป็�นชื่��อของชื่�ดคำ��สั่��ง• Syntax ของฟั�งก์�ชื่� �น
Def NAME (PARAMETERS ): STATEMENTS
• NAME เป็�นชื่��อของฟั�งก์�ชื่�น สั่�ม�รถใชื่�ชื่��อใดๆต�ม ก์ฎก์�รต��งชื่��อ ยก์เว้�น keyword
• ม!คำ��สั่��งได�ไม#จำ��ก์�ดจำ��นว้น แต#ต�องเย��องเข��ไป็จำ�ก์def ( คำว้รใชื่�เป็�นม�ตรฐ�น อ�ทิ( 4 spaces)
![Page 3: FUNCTIONS. • Function เป็นชื่อของชุดคำสั่ง • Syntax ของฟังก์ชั่น Def NAME (PARAMETERS ): STATEMENTS • NAME เป็นชื่อของฟังก์ชัน](https://reader035.vdocuments.pub/reader035/viewer/2022062404/551a6ad05503463e778b5dc1/html5/thumbnails/3.jpg)
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 เป็นชื่อของฟังก์ชัน](https://reader035.vdocuments.pub/reader035/viewer/2022062404/551a6ad05503463e778b5dc1/html5/thumbnails/4.jpg)
![Page 5: FUNCTIONS. • Function เป็นชื่อของชุดคำสั่ง • Syntax ของฟังก์ชั่น Def NAME (PARAMETERS ): STATEMENTS • NAME เป็นชื่อของฟังก์ชัน](https://reader035.vdocuments.pub/reader035/viewer/2022062404/551a6ad05503463e778b5dc1/html5/thumbnails/5.jpg)
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 เป็นชื่อของฟังก์ชัน](https://reader035.vdocuments.pub/reader035/viewer/2022062404/551a6ad05503463e778b5dc1/html5/thumbnails/6.jpg)
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 เป็นชื่อของฟังก์ชัน](https://reader035.vdocuments.pub/reader035/viewer/2022062404/551a6ad05503463e778b5dc1/html5/thumbnails/7.jpg)
Function Call
![Page 8: FUNCTIONS. • Function เป็นชื่อของชุดคำสั่ง • Syntax ของฟังก์ชั่น Def NAME (PARAMETERS ): STATEMENTS • NAME เป็นชื่อของฟังก์ชัน](https://reader035.vdocuments.pub/reader035/viewer/2022062404/551a6ad05503463e778b5dc1/html5/thumbnails/8.jpg)
![Page 9: FUNCTIONS. • Function เป็นชื่อของชุดคำสั่ง • Syntax ของฟังก์ชั่น Def NAME (PARAMETERS ): STATEMENTS • NAME เป็นชื่อของฟังก์ชัน](https://reader035.vdocuments.pub/reader035/viewer/2022062404/551a6ad05503463e778b5dc1/html5/thumbnails/9.jpg)
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 เป็นชื่อของฟังก์ชัน](https://reader035.vdocuments.pub/reader035/viewer/2022062404/551a6ad05503463e778b5dc1/html5/thumbnails/10.jpg)
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 เป็นชื่อของฟังก์ชัน](https://reader035.vdocuments.pub/reader035/viewer/2022062404/551a6ad05503463e778b5dc1/html5/thumbnails/11.jpg)
• 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 เป็นชื่อของฟังก์ชัน](https://reader035.vdocuments.pub/reader035/viewer/2022062404/551a6ad05503463e778b5dc1/html5/thumbnails/12.jpg)
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 เป็นชื่อของฟังก์ชัน](https://reader035.vdocuments.pub/reader035/viewer/2022062404/551a6ad05503463e778b5dc1/html5/thumbnails/13.jpg)
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 เป็นชื่อของฟังก์ชัน](https://reader035.vdocuments.pub/reader035/viewer/2022062404/551a6ad05503463e778b5dc1/html5/thumbnails/14.jpg)
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 เป็นชื่อของฟังก์ชัน](https://reader035.vdocuments.pub/reader035/viewer/2022062404/551a6ad05503463e778b5dc1/html5/thumbnails/15.jpg)
![Page 16: FUNCTIONS. • Function เป็นชื่อของชุดคำสั่ง • Syntax ของฟังก์ชั่น Def NAME (PARAMETERS ): STATEMENTS • NAME เป็นชื่อของฟังก์ชัน](https://reader035.vdocuments.pub/reader035/viewer/2022062404/551a6ad05503463e778b5dc1/html5/thumbnails/16.jpg)
![Page 17: FUNCTIONS. • Function เป็นชื่อของชุดคำสั่ง • Syntax ของฟังก์ชั่น Def NAME (PARAMETERS ): STATEMENTS • NAME เป็นชื่อของฟังก์ชัน](https://reader035.vdocuments.pub/reader035/viewer/2022062404/551a6ad05503463e778b5dc1/html5/thumbnails/17.jpg)
![Page 18: FUNCTIONS. • Function เป็นชื่อของชุดคำสั่ง • Syntax ของฟังก์ชั่น Def NAME (PARAMETERS ): STATEMENTS • NAME เป็นชื่อของฟังก์ชัน](https://reader035.vdocuments.pub/reader035/viewer/2022062404/551a6ad05503463e778b5dc1/html5/thumbnails/18.jpg)
![Page 19: FUNCTIONS. • Function เป็นชื่อของชุดคำสั่ง • Syntax ของฟังก์ชั่น Def NAME (PARAMETERS ): STATEMENTS • NAME เป็นชื่อของฟังก์ชัน](https://reader035.vdocuments.pub/reader035/viewer/2022062404/551a6ad05503463e778b5dc1/html5/thumbnails/19.jpg)
![Page 20: FUNCTIONS. • Function เป็นชื่อของชุดคำสั่ง • Syntax ของฟังก์ชั่น Def NAME (PARAMETERS ): STATEMENTS • NAME เป็นชื่อของฟังก์ชัน](https://reader035.vdocuments.pub/reader035/viewer/2022062404/551a6ad05503463e778b5dc1/html5/thumbnails/20.jpg)
![Page 21: FUNCTIONS. • Function เป็นชื่อของชุดคำสั่ง • Syntax ของฟังก์ชั่น Def NAME (PARAMETERS ): STATEMENTS • NAME เป็นชื่อของฟังก์ชัน](https://reader035.vdocuments.pub/reader035/viewer/2022062404/551a6ad05503463e778b5dc1/html5/thumbnails/21.jpg)
![Page 22: FUNCTIONS. • Function เป็นชื่อของชุดคำสั่ง • Syntax ของฟังก์ชั่น Def NAME (PARAMETERS ): STATEMENTS • NAME เป็นชื่อของฟังก์ชัน](https://reader035.vdocuments.pub/reader035/viewer/2022062404/551a6ad05503463e778b5dc1/html5/thumbnails/22.jpg)
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 เป็นชื่อของฟังก์ชัน](https://reader035.vdocuments.pub/reader035/viewer/2022062404/551a6ad05503463e778b5dc1/html5/thumbnails/23.jpg)
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 เป็นชื่อของฟังก์ชัน](https://reader035.vdocuments.pub/reader035/viewer/2022062404/551a6ad05503463e778b5dc1/html5/thumbnails/24.jpg)
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 เป็นชื่อของฟังก์ชัน](https://reader035.vdocuments.pub/reader035/viewer/2022062404/551a6ad05503463e778b5dc1/html5/thumbnails/25.jpg)
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 เป็นชื่อของฟังก์ชัน](https://reader035.vdocuments.pub/reader035/viewer/2022062404/551a6ad05503463e778b5dc1/html5/thumbnails/26.jpg)
Variable lifetime
• The variable a only exists while the function is being executed.
![Page 27: FUNCTIONS. • Function เป็นชื่อของชุดคำสั่ง • Syntax ของฟังก์ชั่น Def NAME (PARAMETERS ): STATEMENTS • NAME เป็นชื่อของฟังก์ชัน](https://reader035.vdocuments.pub/reader035/viewer/2022062404/551a6ad05503463e778b5dc1/html5/thumbnails/27.jpg)
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 เป็นชื่อของฟังก์ชัน](https://reader035.vdocuments.pub/reader035/viewer/2022062404/551a6ad05503463e778b5dc1/html5/thumbnails/28.jpg)
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 เป็นชื่อของฟังก์ชัน](https://reader035.vdocuments.pub/reader035/viewer/2022062404/551a6ad05503463e778b5dc1/html5/thumbnails/29.jpg)
![Page 30: FUNCTIONS. • Function เป็นชื่อของชุดคำสั่ง • Syntax ของฟังก์ชั่น Def NAME (PARAMETERS ): STATEMENTS • NAME เป็นชื่อของฟังก์ชัน](https://reader035.vdocuments.pub/reader035/viewer/2022062404/551a6ad05503463e778b5dc1/html5/thumbnails/30.jpg)