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チームは、平方根、印刷、対数などのサブルーチン library を穿孔テープに保管していました。どのプログラムでも読み込んで呼び出すことができました。
- 難しいのは戻り場所を確保することでした。David Wheelerの Trick、"Wheeler Jump" は、帰還アドレスを保存することで、ルーティンが呼び出されたどこへでも戻れるようにしました。
- 今日書くすべての関数呼び出しも同じことをしており、すべての言語ライブラリはこのテープから派生しています。
- この授業では手続と関数を扱います:定義方法、値の入出力、およびPaper 2の採点基準に従って書く方法。

手続と関数
- 構造化プログラミング は、それぞれ1つのタスクを持つ小さな名付けられた サブルーチン からプログラムを構成します。
- プロシージャとは、アクションを実行して何も返さない名付けられたブロックです:
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).
各々が適切な用途
- プロシージャ:同じ一連の手順を複数の箇所で必要とする場合。入力検証、メニュー表示、2つの値の交換など。手順は一度だけ書き、名前によって呼び出されます。
- 関数:単一の値を計算して式で使用する必要がある場合。合計、
TRUE/FALSEの結果、2つの数のうち大きい方など。返り値が呼び出しを置き換えます。 - 論理が複数の箇所に現れるとき、ブロックに明確な名付けられた目的があるとき、プログラムが分解(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.
パラメータ、引数、およびインターフェース
- パラメータは、サブルーチンが値を受け取るために宣言する変数です。呼び出し側が提供する値は引数と呼ばれます。
- ヘッダーは最初の行です:
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.
worked example: ヘッダー内の各用語を説明する
FUNCTION Pass2(Count : INTEGER) RETURNS BOOLEAN.FUNCTION: 値を返すサブルーチンのこと。Pass2: それを呼び出す際に用いる識別子。Count: パラメータであり、渡された引数を受け取る識別子。INTEGER: そのパラメータのデータ型。RETURNS BOOLEAN: 関数が返す値のデータ型。5つの部分、それぞれ1点ずつ。

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.
値渡しと参照渡し
- 値渡し:ルーチンはコピーを受け取るため、内部での変更は呼び出し側に反映されません。ルーチンが読み取りのみを行う入力に使用します。
- 参照渡し:ルーチンは呼び出し側の自身の变量への参照を受け取るため、変更は呼び出し側に届きます。ルーチンが引数を更新しなければならない場合に使用します(例:
Swap)。 - Cambridgeでは、ヘッダー内に各パラメータの前にモード
BYVALまたはBYREFを記述します。どちらも書かれていない場合、BYVALが仮定されます。

コピー、あるいは呼び出し側の变量へのリンク

worked example: 出力は何か?
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.
ローカル変数とグローバル変数
- ローカル変数はサブルーチン内で宣言され、実行中のみ有効です。グローバル変数は外部で宣言され、どこでも参照可能です。ある名前が参照可能な範囲をスコープと呼びます。
- ローカル変数を好みます。同じ識別子を他の場所で再利用でき、プログラムの他の部分による意図しない変更を防げ、リターン時にメモリが解放され、サブルーチンが自己完結的になるためです。
- ローカル変数は呼び出しごとに新規作成されるため、呼び出し間で値を保持できません。反復する呼び出しで文字列を積み上げるルーチンでは、その文字列をグローバルにするか、
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.
worked example: Paper 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
- シラバスでは機能ごとに点数を授与します:パラメータと返り値を含むヘッダー、ローカル宣言、ループ前のカウンターの初期化、全要素に対するループ、正しい比較を含む条件式、内部での更新、構文の閉じ括り、ループ後の1つの
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.
理解できましたか
- プロシージャはアクションを実行し
CALLされ、関数は式で使用される値を返します - ヘッダーはルーチンの名前、型付きのパラメータ、そして返り値の型を指定し、渡された値は引数です
- BYVAL はコピーを渡す(デフォルト)、BYREF はルーチンが呼び出し側の变量を変更できるようにします
- ローカル変数を優先します。ローカル変数は呼び出しごとに新規作成され、グローバル変数はその値を維持します
- Paper 2モジュール: ヘッダー、宣言、初期化、ループ、条件、更新、閉構造、1
RETURN
何が問題でしたか?この内容はサイトの運営チームだけが確認します。