Scope & arguments
Local variables
- A variable made inside a function is local.
- It only exists while the function runs.
- Code outside the function cannot see it.
ローカル変数
- 関数の内部で作成された変数はローカルです。
- 関数が実行されている間のみ存在します。
- 関数外のコードからは見ることができません。
def make_message():
note = "secret"
print(note)
make_message()
# print(note) would be an error: note is local
Global variables
- A variable made outside all functions is global.
- A function can read a global value.
- To change a global inside a function you need the
globalkeyword — but it is usually cleaner toreturna value instead.
グローバル変数
- 全ての関数の外側で作成された変数はグローバルです。
- 関数はグローバルな値を読み込むことができます。
- 関数の内部でグローバルを変更するには
globalキーワードが必要ですが、通常はreturnを使用した方がすっきりしています。
Default values
- A parameter can have a default value:
def f(x, step=1):. - If the caller leaves it out, the default is used.
- Parameters with defaults come after the normal ones.
デフォルト値
- パラメータにはデフォルト値を設定できます:
def f(x, step=1):。 - 呼び出し側が省略した場合、デフォルト値が使用されます。
- デフォルト値を持つパラメータは、通常のパラメータの後に来ます。
def power(base, exp=2):
return base ** exp
print(power(5))
print(power(2, 3))
How arguments are passed
- Python passes a reference to the object you give it.
- Numbers and strings are immutable: a function cannot change the caller's value.
- Lists are mutable: changes made inside a function do affect the caller's list.
引数の渡し方
- Pythonは与えたオブジェクトへの参照を渡します。
- 数字や文字列は不変性があります: 関数が呼び出し側の値を変えることはありません。
- リストは可変性があります: 関数の内部で行った変更は、呼び出し側のリストに影響します。
def try_change(n):
n = n + 100 # changes only the local copy
print(n)
x = 5
try_change(x)
print(x) # x is still 5
In Cambridge pseudocode (A-Level 9618)
BYVALpasses a copy (caller unchanged);BYREFshares the same data (caller changed).- ⚠️ These two keywords are A-Level 9618 only. IGCSE 0478 passes every parameter by value; to send a result back you use a
FUNCTIONandRETURNit.
Cambridge擬似コード (A-Level 9618)
BYVALはコピーを渡す(呼び出し側は変化しない);BYREFは同じデータを共有する(呼び出し側は変化する)。- ⚠️ この2つのキーワードはA-Level 9618専用です。IGCSE 0478ではすべてのパラメータを値渡しで渡します。結果を返すには
FUNCTIONを使い、RETURNします。
PROCEDURE Increase(BYREF N : INTEGER)
N ← N + 1
ENDPROCEDURE
PROCEDURE Show(BYVAL N : INTEGER)
OUTPUT N
ENDPROCEDURE
Common mistakes
- A variable made inside a function is local — it cannot be seen outside.
- Arguments fill the parameters in the order you write them.
- To use a result outside,
returnit and store what comes back.
よくあるミス
- 関数の内部で作成された変数はローカルであり、外から見ることはできません。
- 引数は書いた順にパラメータに割り当てられます。
- 結果を外で使用するには、
returnして戻り値を格納します。
Now you try
- The checks call your function with different arguments.
- Press Check answer to test your code.
あなたも試してみよう
- チェックは異なる引数であなたの関数を呼び出します。
- 回答を確認 を押してコードを試してください。
Arguments live in the call
Each call gets its own copy of the arguments — they vanish when it returns.
Write greet(name, greeting="Hi") that returns greeting, then , , then name, then !. So greet("Sam") is Hi, Sam! and greet("Sam", "Hello") is Hello, Sam!.
Click Run to see the output here. · 実行ボタンをクリックして出力を確認してください。
Write add_item(lst, x) that appends x to the list lst. Do not return anything — the caller's list should change because lists are mutable.
Click Run to see the output here. · 実行ボタンをクリックして出力を確認してください。
Write between(low, high, x) that returns True if x is from low to high (inclusive), otherwise False.
Click Run to see the output here. · 実行ボタンをクリックして出力を確認してください。
Write make_countdown(start, stop=1) that returns a list counting down from start to stop. So make_countdown(3) is [3, 2, 1] and make_countdown(5, 3) is [5, 4, 3].
Click Run to see the output here. · 実行ボタンをクリックして出力を確認してください。