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ʃə/ | חתימה |
| 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/ | משתנה גלובלי |
| local variable/ˈləʊkl ˈveərɪəbl/ | משתנה מקומי |
| scope/skəʊp/ | תחום |
| 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. |
רצועת תוכניות שכל אחד יכול היה להעתיק
- עד שנת 1951, צוות EDSAC בקמברידג' שמר ספריית תת-תוכניות על רצועת נייר מקורבת: שורשים ריבועיים, הדפסה, לוגריתמים. כל תוכנית יכלה לקרוא אחת ולקרוא אותה.
- החלק הקשה היה לחזור. הטריק של דויד וילר, "קפיצת וילר", שמר את כתובת החזרה כך שתת-תוכנית תוכל לחזור לנקודה ממנה נקראה.
- כל קריאת פונקציה שאתה כותב היום עושה את אותו דבר, וכל ספריית שפות נגזרת מרצועה זו.
- LESSON זו עוסקת בפרוצדורות ובפונקציות: כיצד להגדיר אותן, כיצד לעביר ערכים לתוכן ומחוץ, וכיצד לכתוב אותן כך שהPaper 2 תעניק עליהן נקודות.

הליכים ופונקציות
- תכנות מבני בונה תוכנית מתת-תוכניות קטנות וממונות, כל אחת עם משימה אחת.
- הליך הוא בלוק ממונה שעושה פעולה ואינו מחזיר שום דבר:
PROCEDURE Greet(Name : STRING) … ENDPROCEDURE, מורץ עםCALL Greet("Ada"). - פונקציה מחזירה ערך הופך לחלק מביטוי:
FUNCTION Square(X : INTEGER) RETURNS INTEGER … RETURN X * X … ENDFUNCTION, משמש כResult ← Square(5) + 1.

הליך עושה משהו; פונקציה מחזירה ערך

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).
היכן כל אחד מהם מתאים
- הליך כאשר אותה קבוצת שלבים נדרשת במספר נקודות: לאמת כניסה, להדפיס תפריט, להחליף שני ערכים. השלבים נכתבים פעם אחת ונקראים בשם.
- פונקציה כאשר יש לחשב ערך יחיד ולאחר מכן להשתמש בו בביטוי: סכום, תוצאת
TRUE/FALSE, הגדול מבין שני מספרים. הערך המוחזר מחליף את הקריאה. - השתמש בתת-תוכנית כאשר לוגיקה מופיעה במקום אחד או יותר, כאשר בלוק יש לו מטרה נקובה וברורה, כאשר התוכנית מספיק מורכבת כדי להצריך פירוק, או כאשר תרצה לבדוק חלק אחד בעצמו.
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.
פרמטרים, ארגומנטים והממשק
- פרמטרים הם המשתנים שמתגבשים בתוך תת-תוכנית לקבלת ערכים; הערכים שהקורא מספק הם ה-ארגומנטים.
- ה-כותרת היא השורה הראשונה:
PROCEDURE Name(Param : TYPE)אוFUNCTION Name(Param : TYPE) RETURNS TYPE. ה-ממשק, או ה-חתימה, הוא השם, הפרמטרים בסדר עם סוגיהם, וסוג ההחזרה: כל מה שקורא צריך לדעת. - ה-ערך החזרה הוא מה שפונקציה מחזירה עם
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.
דוגמה פתורה: תאר כל איבר בכותרת
FUNCTION Pass2(Count : INTEGER) RETURNS BOOLEAN.FUNCTION: תת-תוכנית המשבה ערך.Pass2: המזהה המשמש לקריאה אליה.Count: הפרמטר, המזהה המקבל את הארגומנט שעובר אליו.INTEGER: סוג הנתונים של פרמטר זה.RETURNS BOOLEAN: סוג הנתונים של הערך שמחזירה הפונקציה. חמישה חלקים, נקודה אחת לכל חלק.

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.
העברה על ידי ערך והעברה על ידי רפרנס
- מעבר בערך: התוכנית מקבלת עתיקה, ולכן שינויים בתוכה אינם מגיעים לקורא. השתמש בו עבור קלטות שהתוכנית רק קוראת.
- מעבר בערך (Reference): התוכנית מקבלת רפרנס למשתנה הקורא עצמו, ולכן שינויים כן מגיעים לקורא. השתמש בו כאשר התוכנית צריכה לשנות את הארגומנט, כמו ב-
Swap. - Cambridge כותבת את המודל בכותרת,
BYVALאוBYREF, לפני כל פרמטר. אם אין כלום כתוב,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הוא ייחוס ל-A, ולכןAהופך ל-8.Yהיא העתק שלB, ולכן הכפלתYמשאירה אתBב-3.- התפוקה היא
8, 3. אילו הכותרת הייתה אומרת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.
משתנים מקומיים וגלובליים
- משתנה מקומי מתגבש בתוך תת-תוכנית ומתקיים רק בזמן הביצוע שלו; משתנה גלובלי מתגבש מחוץ ונראה בכל מקום. האזור שבו שם נתון נראה הוא ה-תחום (scope) שלו.
- משתנים מקומיים עדיפים: אותו מזהה ניתן לשימוש חוזר במקום אחר ללא התנגשות, הערך לא יכול להשתנות באופן מקרי על ידי חלקים אחרים בתוכנה, הזיכרון משוחרר בהחזרה, והתת-תוכנית עצמאית.
- משתנה מקומי נוצר מחדש בכל קריאה, ולכן אינו יכול לשאת ערך בין קריאות. שגרת בונה מחרוזת לאורך קריאות חוזרות זקוקה למחרוזת זו כגלובלית, או מעבירה אותה
BYREF.

משתנה גלובלי נראה בכל מקום; משתנה מקומי חי רק בתוך תת-תוכנית משלו

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והוסףRETURNS <type>לכותרת. - החלף את ה
OUTPUT, או את הBYREFפרמטר שהעביר את התוצאה החוצה, עם הוראת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.
דוגמה פתורה: כתיבת מודול לבחינת נייר 2
- מערך גלובלי
Score : ARRAY[1:50] OF INTEGERאחסן ציוני מבחן. כתוב פונקציהCountAbove(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
- המערכת מעניקה נקודה לכל מאפיין: הכותרת עם הפרמטר וסוג ההחזרה, ההצהרות המקומיות, הספן המיוזם לפני הלולאה, הלולאה על כל אלמנט, התנאי עם ההשוואה הנכונה, עדכון בתוכו, סגירת המבנים, ו
RETURNאחד לאחר הלולאה. - מודול שלא הושלם עדיין זוכה נקודות על כל חלק נכון, לכן כתוב את כולם.

כל חלק בתשובת מודול נושא נקודה משלו

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.
הכרטיס הזה תוקן, לכן הוא מוצג באנגלית עד שהתרגום שלו יעודכן.
Marks that slip away
- A routine that must change the caller's variable needs
BYREFin its header; without itBYVALis assumed and the change is lost. - In a function that must process every element, put the final
RETURNafter the loop and close the function withENDFUNCTION. ARETURNinside the loop ends the function when it is executed. DECLAREevery local, and initialise a counter or total to 0 before the loop.- "Efficient pseudocode 伪代码" means moving work that does not change out of the loop, stopping a search 查找 at the first match, and not repeating a call 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.
הבנתך מושלמת
- תת-תוכנית (procedure) מבצעת פעולה ומוחזרת
CALL; פונקציה (function) מחזירה ערך המשמש ביטוי - הכותרת שמה את השגרת, את הפרמטרים שלה עם סוגים ואת סוג ההחזרה; ארגומנטים הם הערכים המועברים לתוכה
- BYVAL מעביר העתק (הברירת מחדל); BYREF מאפשר לרוטינה לשנות את המשתנה של הקורא
- העדיפו משתנים מקומיים; משתנה מקומית היא חדשה בכל קריאה, בעוד שגלובלי שומר על ערכו
- מודול ב-Paper 2: כותרת, הצהרות, התחלה, לולאה, תנאי, עדכון, מבנים סגורים,
RETURNאחד
מה השתבש? רק צוות האתר רואה זאת.