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.
局部变量
- 在函数内部创建的变量是局部的(local)。
- 它只在函数运行期间存在。
- 函数外面的代码看不到它。
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)。
- 函数可以读取全局值。
- 想在函数里修改全局变量,需要用
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 传递的是指向对象的引用(reference)。
- 数字和字符串是不可变的(immutable):函数无法改变调用者的值。
- 列表是可变的(mutable):在函数内部所做的修改确实会影响调用者的列表。
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.
用剑桥伪代码表示(A-Level 9618)
BYVAL传递一个副本(调用者不变);BYREF共享同一份数据(调用者会改变)。- ⚠️ 这两个关键字仅属于 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!. · 编写 greet(name, greeting="Hi"),返回 greeting、, 、name、再加 !。即 greet("Sam") 是 Hi, Sam!,greet("Sam", "Hello") 是 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. · 编写 add_item(lst, x),把 x 追加到列表 lst。不要返回任何东西 —— 因为列表是可变的,调用者的列表会被改变。
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. · 编写 between(low, high, x),如果 x 在 low 到 high 之间(含两端),返回 True,否则返回 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]. · 编写 make_countdown(start, stop=1),返回一个从 start 倒数到 stop 的列表。例如 make_countdown(3) 是 [3, 2, 1],make_countdown(5, 3) 是 [5, 4, 3]。
Click Run to see the output here. · 点击“运行”查看此处输出。