Procedures, functions and structured programming
แนะนำใหม่
23:18
พื้นฐานการเขียนโปรแกรม
โปรแกรมต้องขอรหัสผ่าน และจะขอรหัสซ้ำจนกว่าจะถูกต้อง ง่ายมาก — แต่จะเลือกใช้ลูปแบบใด? โปรแกรมมีการตัดสินใจและทำงานซ้ำกัน และการเลือก…
บรรยายภาษาอังกฤษ · สลับคำบรรยายภาษาอังกฤษและภาษาจีนได้
ในบทเรียนนี้
| English | ความหมาย |
|---|---|
| library/ˈlaɪbrəri/ | A collection of reusable routines that programs can call. |
| subroutines/ˈsʌbruːtiːnz/ | ซับรูทีน |
| punched paper tape | A strip of paper with holes that represent stored data or instructions. |
| call/kɔːl/ | To make a named routine run from another part of a program. |
| return address/rɪˈtɜːn əˈdres/ | The location where execution should resume after a called routine finishes, saved as part of managing the call. |
| function call | An instruction to run a function and use the value it returns. |
| procedure/prəˈsiːdʒə/ | ขั้นตอน |
| function/ˈfʌŋkʃn/ | ฟังก์ชัน |
| Structured programming/ˈstrʌktʃəd ˈprəʊɡræmɪŋ/ | A way of building programs using clear structures and smaller named routines. |
| expression/ekˈspreʃn/ | A combination of values, variables and operators representing a quantity or a value to be evaluated. |
| return value/rɪˈtɜːn ˈvæljuː/ | The value a function passes back to the part of the program that called it. |
| decomposition/ˌdiːkɒmpəˈzɪʃn/ | การแยกย่อย |
| Parameters/pəˈræmɪtəz/ | The variables a subroutine declares to receive values; the values the caller supplies are the arguments. |
| arguments/ˈɑːɡjuːmənts/ | อาร์กิวเมนต์ |
| interface/ˈɪntəfeɪs/ | A way for users or programs to communicate with the computer's hardware. |
| variables/ˈveərɪəblz/ | A quantity or named value that can change or take different values. |
| header/ˈhedə/ | Control information placed before a message's payload, such as addresses, sequence numbers or protocol identifiers. |
| signature/ˈsɪɡnɪtʃə/ | ลายมือชื่อ (signature) |
| types/taɪps/ | Categories that describe what kind of data values can be stored or used. |
| return type/rɪˈtɜːn taɪp/ | The data type of the value a function passes back. |
| identifier/aɪˈdentɪfaɪə/ | A name used to identify a program element such as a variable, function or class. |
| data type/ˈdeɪtə taɪp/ | A classification of values that determines their representation and permitted operations. |
| Pass by value/pæs baɪ ˈvæljuː/ | The routine receives a copy, so changes inside it do not reach the caller. Use it for inputs the routine only reads. |
| Pass by reference/pæs baɪ ˈrefrəns/ | A way of passing a variable that lets a routine change the caller's variable. |
| reference/ˈrefrəns/ | A link to a variable that lets a routine access and change it. |
| global variable/ˈɡləʊbl ˈveərɪəbl/ | ตัวแปรสเกล globale |
| local variable/ˈləʊkl ˈveərɪəbl/ | ตัวแปรสเกล local |
| scope/skəʊp/ | ขอบเขต (scope) |
| memory/ˈmeməri/ | The part of a computer that holds data and programs currently in use. |
| string/strɪŋ/ | A sequence of characters treated as a single data value. |
| statement/ˈsteɪtmənt/ | A line of code that tells a program what to do. |
| module/ˈmɒdjuːl/ | A separate part of a program that performs a particular task. |
| array/əˈreɪ/ | An indexed collection of elements arranged in one or more dimensions. |
| declarations | Statements that give variables their names and data types, and arrays their bounds. |
| counter/ˈkaʊntə/ | A device or variable that records how many events or items have occurred. |
| initialised | Given a starting value before being used in a program. |
| loop/luːp/ | A structure that repeats a sequence of instructions. |
| element/ˈelɪmənt/ | A substance made of atoms with the same proton number. |
| condition/kənˈdɪʃn/ | A requirement or circumstance that affects what happens or whether something is true. |
| comparison/kəmˈpærɪsn/ | Examining two or more items to identify similarities, differences or their relative order. |
| constructs | Parts of a program with a defined structure that control how instructions run. |
| pseudocode/ˈsuːdəʊkəʊd/ | A language-independent description of an algorithm using structured, programming-like statements. |
| search/sɜːtʃ/ | To look through data for an item that matches a given value. |
เทปของ routines ที่ใครก็ยืมได้
- โดย 1951 ทีม EDSAC ใน Cambridge เก็บ library ของ subroutines บน paper tape: square roots, printing, logarithms. Program ใดก็อ่านเข้าและเรียกใช้ได้
- สิ่งที่ยากคือการกลับมา David Wheeler's trick, "Wheeler jump", เก็บที่อยู่การคืนค่าเพื่อให้ routine กลับไปที่จุดที่ถูกเรียกจากได้
- ทุก function call ที่คุณเขียนวันนี้ทำสิ่งเดียวกัน, และทุก language library สืบเนื่องมาจากเทปนั้น
- บทเรียนนี้คือ procedures และ functions: วิธีนิยาม, วิธีส่งค่าเข้าออก, และวิธีเขียนหนึ่งแบบที่ Paper 2 ให้คะแนน

Procedures และ functions
- Structured programming สร้างโปรแกรมจาก subroutines ขนาดเล็กที่มีชื่อ, แต่ละตัวมีงานเดียว
- ** Procedure ** คือบล็อกที่มีชื่อซึ่งดำเนินการและไม่ส่งค่ากลับ:
PROCEDURE Greet(Name : STRING) … ENDPROCEDURE, เรียกใช้ด้วยCALL Greet("Ada"). - ** Function ** ส่งค่ากลับมาซึ่งเป็นส่วนหนึ่งของนิพจน์:
FUNCTION Square(X : INTEGER) RETURNS INTEGER … RETURN X * X … ENDFUNCTION, ใช้ในResult ← Square(5) + 1.

Workspace (กระบวนการ) ทำสิ่งใดสิ่งหนึ่ง; Function (ฟังก์ชัน) ส่งค่ากลับคืน
The call stack: push on call, pop on return
Calling a subroutine pushes a new frame on top; returning pops it and hands a value back to the caller. The call that is running is always the frame on top.
The key difference between a procedure and a function is that a function:
ความยาก 1/5 · 100 เหรียญ + บอนัสน้อยๆ เมื่อทำสำเร็จครั้งแรกในวันนี้
A function returns a value (used in an expression); a procedure performs an action and returns nothing.
A function Square(x) ผลตอบแทน x * x. What does the call Square(5) return?
ความยาก 2/5 · 140 เหรียญ + บอนัสน้อยๆ เมื่อทำสำเร็จครั้งแรกในวันนี้
5 × 5 = 25 — the value the function hands back to its caller (the frame popped off the call stack).
เมื่อใดควรใช้แต่ละแบบ
- ** Procedure ** เมื่อต้องการชุดขั้นตอนเดียวกันหลายจุด: ตรวจสอบข้อมูลเข้า, แสดงเมนู, สลับค่าสองตัว เขียนขั้นตอนครั้งเดียวแล้วเรียกตามชื่อ
- ** Function ** เมื่อต้องคำนวณค่าหนึ่งแล้วนำไปใช้ในนิพจน์: ผลรวม, ค่า
TRUE/FALSE, หรือค่าที่มากกว่าของสองจำนวน ค่าที่ส่งกลับมาจะแทนที่การเรียกใช้ - ใช้ Subroutine เมื่อมีตรรกะปรากฏในหลายที่, บล็อกมีวัตถุประสงค์ที่ชัดเจน, โปรแกรมซับซ้อนพอที่ต้องการ ** Decomposition **, หรือเมื่อต้องการทดสอบชิ้นส่วนหนึ่งโดยอิสระ
A good reason to write a subroutine is that:
ความยาก 1/5 · 100 เหรียญ + บอนัสน้อยๆ เมื่อทำสำเร็จครั้งแรกในวันนี้
Subroutines remove duplication, give a named purpose, and can be tested in isolation.
Parameters, Arguments และ Interface
- ** Parameters ** คือตัวแปรที่ Subroutine ประกาศเพื่อรับค่า ส่วนค่าที่ผู้เรียกให้มาคือ ** Arguments **
- ** Header ** คือบรรทัดแรก:
PROCEDURE Name(Param : TYPE)หรือFUNCTION Name(Param : TYPE) RETURNS TYPE** Interface ** หรือ ** Signature ** คือชื่อ, parameters ตามลำดับพร้อมชนิด, และชนิดที่ส่งกลับมา: ทุกสิ่งที่ผู้เรียกต้องรู้ - ** Return value ** คือสิ่งที่ Function ส่งกลับมาพร้อมกับ
RETURN
Match each term to what it means.
ความยาก 2/5 · 140 เหรียญ + บอนัสน้อยๆ เมื่อทำสำเร็จครั้งแรกในวันนี้
Function vs procedure = returns a value or not; by value vs by reference = copy or original.
ตัวอย่างฝึกหัด: อธิบายแต่ละคำใน Header
FUNCTION Pass2(Count : INTEGER) RETURNS BOOLEAN.FUNCTION: Subroutine ที่ส่งค่ากลับPass2:.Identifier ที่ใช้ในการเรียกใช้มันCount: Parameter, Identifier ที่รับ Argument ที่ส่งเข้ามาINTEGER: ชนิดข้อมูลของ Parameter นั้นRETURNS BOOLEAN: ชนิดข้อมูลของค่าที่ Function ส่งกลับมา ห้าส่วน คะแนนละหนึ่ง

In FUNCTION Pass2(Count : INTEGER) RETURNS BOOLEAN, the identifier Count is the ____.
ความยาก 1/5 · 100 เหรียญ + บอนัสน้อยๆ เมื่อทำสำเร็จครั้งแรกในวันนี้
The parameter receives the argument the caller passes in and is used inside the function like a local variable.
Pass by Value และ Pass by Reference
- ** Pass by Value **: Routine ได้รับ ** Copy **, ดังนั้นการเปลี่ยนแปลงภายในจะไม่ส่งผลต่อผู้เรียก ใช้สำหรับ Input ที่ Routine อ่านเพียงอย่างเดียว
- ** Pass by Reference **:Routine ได้รับ Ref ของตัวแปรของผู้เรียกเอง ดังนั้นการเปลี่ยนแปลง ** จะ ** ส่งผลต่อผู้เรียก ใช้เมื่อ Routine ต้องอัปเดต Argument เช่นใน
Swap - Cambridge เขียน Mode ใน Header,
BYVALหรือBYREF, ก่อนแต่ละ Parameter หากไม่เขียนอะไรBYVALจะถูกสมมติไว้

สำเนา, หรือลิงก์ไปยังตัวแปรของผู้เรียกใช้
ตัวอย่างฝึกหัด: ผลลัพธ์คืออะไร?
PROCEDURE Adjust(BYREF X : INTEGER, BYVAL Y : INTEGER)
X <- X + Y
Y <- Y * 2
ENDPROCEDURE
A <- 5
B <- 3
CALL Adjust(A, B)
OUTPUT A, B
Xเป็น Ref ของA, ดังนั้นAจึงเป็น 8Yเป็น Copy ของB, ดังนั้นการคูณสองYก็ทำให้Bยังคงอยู่ที่ 3- ผลลัพธ์คือ
8, 3หาก Header บอกว่าBYVAL X,Aจะยังคงเป็น 5

In the worked example, what is the value of A after CALL Adjust(A, B)?
ความยาก 2/5 · 140 เหรียญ + บอนัสน้อยๆ เมื่อทำสำเร็จครั้งแรกในวันนี้
X is passed BYREF, so X ← X + Y adds 3 to the caller's A: 5 + 3 = 8. B stays 3 because Y was a copy.
ตัวแปร Local และ Global
- ** Local variable ** ถูกประกาศภายใน Subroutine และมีอยู่เฉพาะขณะทำงาน; ** Global variable ** ถูกประกาศภายนอกและมองเห็นได้ทุกที่ พื้นที่ที่ชื่อนั้นมองเห็นได้เรียกว่า ** Scope ** ของมัน
- Locals are preferred: the same identifier can be reused elsewhere without a clash, the value cannot be changed accidentally by other parts of the program, the memory is released on return, and the subroutine is self-contained.
- Local ถูกสร้างใหม่ทุกครั้งที่เรียกใช้ ดังนั้นจึงไม่สามารถเก็บค่าระหว่างครั้งเรียกได้ Routine ที่สร้าง string ต่อเนื่องหลายครั้งจำเป็นต้องใช้ string นั้นเป็น Global หรือส่งเข้า
BYREF

Global (ตัวแปรสากล) เข้าถึงได้ทุกที่; Local (ตัวแปรเฉพาะที่) มีอยู่ภายใน subroutine ของมันเองเท่านั้น
A local variable exists only inside the subroutine where it is declared, while a global variable is visible everywhere in the program.
ความยาก 1/5 · 100 เหรียญ + บอนัสน้อยๆ เมื่อทำสำเร็จครั้งแรกในวันนี้
Keeping variables local limits their scope, avoids name clashes, and makes a subroutine testable on its own — globals are best avoided.
Which are benefits of using local variables? Select ทั้งหมด that apply.
ความยาก 2/5 · 140 เหรียญ + บอนัสน้อยๆ เมื่อทำสำเร็จครั้งแรกในวันนี้
A local is created fresh on every call, so it cannot keep a value between calls. That is the one job a global, or a BYREF parameter, does instead.
เปลี่ยน Procedure เป็น Function
- เปลี่ยน
PROCEDUREเป็นFUNCTIONและเพิ่มRETURNS <type>ลงใน Header - แทนที่
OUTPUT, หรือBYREFParameter ที่นำผลลัพธ์ออก, ด้วยคำสั่งRETURN - เปลี่ยนทุกการเรียกให้ใช้ค่าที่ส่งกลับมา:
Result ← Unpack(Text)แทนCALL Unpack(Text, Result)
To convert a procedure into a function you change the header to FUNCTION with RETURNS, replace the OUTPUT with RETURN, and change the calls to use the returned value.
ความยาก 1/5 · 100 เหรียญ + บอนัสน้อยๆ เมื่อทำสำเร็จครั้งแรกในวันนี้
Three changes, three marks: the header, the RETURN, and the calls.
ตัวอย่างฝึกหัด: เขียน Module สำหรับ Paper 2
- Global Array
Score : ARRAY[1:50] OF INTEGERเก็บคะแนนสอบ เขียน FunctionCountAbove(Limit : INTEGER)ที่ส่งกลับจำนวนคะแนนที่มากกว่าLimit
FUNCTION CountAbove(BYVAL Limit : INTEGER) RETURNS INTEGER
DECLARE Index, Count : INTEGER
Count <- 0
FOR Index <- 1 TO 50
IF Score[Index] > Limit THEN
Count <- Count + 1
ENDIF
NEXT Index
RETURN Count
ENDFUNCTION
- Scheme ให้คะแนน per feature: header พร้อม parameter และ return type, local declarations, counter ที่ถูก initialise ก่อนลูป, loop ครอบคลุมทุก element, condition ที่มี comparison ที่ถูกต้อง, update ภายใน constructs, constructs ถูกปิด, และหนึ่ง
RETURNหลังลูป - Module ที่ยังไม่เสร็จสิ้นก็ได้รับคะแนนสำหรับแต่ละส่วนที่ถูกต้อง ดังนั้นให้เขียนทั้งหมด

แต่ละส่วนของคำตอบโมดูลมีคะแนนของตนเอง
Put the parts of a Paper 2 module answer in the order they are written.
ความยาก 3/5 · 200 เหรียญ + บอนัสน้อยๆ เมื่อทำสำเร็จครั้งแรกในวันนี้
Header, declare, loop, condition, update, return. Each part is a mark, even if another part is wrong.
คะแนนที่หลุดหายไป
- Routine ที่ต้องเปลี่ยนตัวแปรของผู้เรียกต้องใช้
BYREFใน Header; หากไม่มีBYVALจะถูกสมมติไว้และการเปลี่ยนแปลงจะหายไป - ฟังก์ชันมี
RETURNหนึ่งตัว, หลังลูป, และENDFUNCTION. การมีRETURNภายในลูปจะทำให้ลูปสิ้นสุดที่องค์ประกอบแรกทันที DECLAREทุก Local, และเริ่มต้น Counter หรือ Total เป็น 0 ก่อน Loop- "Efficient pseudocode" หมายความว่าย้ายงานที่ไม่เปลี่ยนออกจาก Loop, หยุดการค้นหาที่การจับคู่ครั้งแรก, และไม่เรียกซ้ำ whose result could be stored
To make a loop more efficient, a value that does not change with the loop counter should be:
ความยาก 1/5 · 100 เหรียญ + บอนัสน้อยๆ เมื่อทำสำเร็จครั้งแรกในวันนี้
Hoisting a loop invariant out avoids recomputing the same value on every iteration.
คุณเข้าใจแล้ว
- a ** procedure ** ทำการและถูก
CALLed; a ** function ** ส่งค่ากลับมาใช้ในนิพจน์ - the ** header ** ชื่อ Routine, ** parameters ** พร้อมชนิดและชนิดที่ส่งกลับมา; ** arguments ** คือค่าที่ส่งเข้ามา
- ** BYVAL ** ส่ง Copy (ค่าเริ่มต้น); ** BYREF ** ให้ Routine เปลี่ยนตัวแปรของผู้เรียกได้
- prefer ** local ** variables; a local ถูกสร้างใหม่ทุกครั้งที่เรียก, a ** global ** รักษาค่าของมัน
- โมดูล Paper 2: หัวข้อ, การประกาศ, การเริ่มต้น, ลูป, เงื่อนไข, การอัปเดต, โครงสร้างปิด, และ
RETURNหนึ่งตัว
เกิดปัญหาอะไรขึ้น? เฉพาะทีมเว็บไซต์เท่านั้นที่เห็นข้อมูลนี้