位置参数与关键字参数
按位置或名称传递值
位置参数与关键字参数 是 CoddyKit 上的免费 Mojo Academy 课时。 这是第 1 节课,共 4 节。 你可以在下方免费阅读本课时的完整内容 — 然后在浏览器中使用内置代码编辑器和全天候 AI 导师进行实践。 这是 Mojo Academy 学习路径的一部分,你的进度在网页和 CoddyKit 应用中同步。 Mojo Academy 课程共包含 4 节课。
本课时的部分内容尚未翻译,以英文显示。
Two Ways to Pass Values
Every Mojo function call sends in values called arguments. You can send them by position or by name, and both styles can live in one call.
Positional Arguments
With positional arguments, order is everything. The first value fills the first parameter, the second fills the second, and so on.
fn greet(name: String, times: Int):
for _ in range(times):
print(name)
greet("Mojo", 2)Order Matters Here
Swap two positional arguments and the meaning changes. Here Mojo would read 2 as the name and "Mojo" as the count, so order must match the parameters.
greet(2, "Mojo") # wrong: positions no longer matchKeyword Arguments
A keyword argument names the parameter you mean. Now the call reads clearly and order stops mattering.
greet(name="Mojo", times=2)Keywords Free You from Order
Because each value is labeled, you can list keyword arguments in any order. Mojo matches them by name, not by position.
greet(times=2, name="Mojo") # same resultMixing Both Styles
You can combine the two, but positional values must come first. Once a keyword appears, everything after it must also be a keyword.
greet("Mojo", times=2) # ok
# greet(name="Mojo", 2) # errorWhy Keywords Help Readers
Keyword arguments turn a mystery call into a sentence. Reading times=2 tells you exactly what the 2 does, even months later.
Avoiding Position Bugs
When a function takes several same-typed values, positions are easy to mix up. Naming them with keywords removes that whole class of bug.
draw(width=800, height=600) # clearer than draw(800, 600)Names Come from Parameters
The keyword you type must match the parameter name in the function definition. A typo like nme= is an error, not a new argument.
fn greet(name: String, times: Int): ...
greet(name="Mojo", times=2)Both Styles, One Function
You do not pick a style per function. The caller decides, so the same function accepts positional, keyword, or a mix on each call.
A Practical Rule of Thumb
Use positional for the obvious leading value, then switch to keywords for extras. This keeps short calls tidy and long calls clear.
connect("localhost", port=8080, retries=3)Quick Check
You want order to stop mattering in a function call. Which approach helps?
Recap: By Position or Name
Positional arguments rely on order; keyword arguments rely on names. Lead with positions, label the rest, and your calls stay both short and clear.
常见问题解答
「位置参数与关键字参数」课时是免费的吗?
是的 — 「位置参数与关键字参数」的完整文本可在网页上免费阅读。要进行交互式练习(内置代码编辑器和全天候 AI 导师)并解锁 Mojo Academy 课程的其余内容,请升级到 CoddyKit PRO。 Mojo Academy 课程共包含 4 节课。
「位置参数与关键字参数」这节课中我会学到什么?
按位置或名称传递值 你通过在浏览器中直接运行的动手代码来练习 Mojo Academy,全天候 AI 导师会在你学习这节课的过程中回答你的问题。
学习 Mojo Academy 需要有经验吗?
无需任何先前经验。CoddyKit 上的 Mojo Academy 课程适合初学者到高级学习者,你可以从这里开始或从头开始,按照自己的节奏学习。 这是第 1 节课,共 4 节。
「位置参数与关键字参数」课时需要多长时间?
大多数 CoddyKit 课程大约需要 5–10 分钟。每节课都很精短且互动,所以你能稳步进步,并在网页和应用中从离开的地方继续。
我能在这节 Mojo Academy 课中编写并运行代码吗?
能。每节 Mojo Academy 课都包含内置代码编辑器,你可以在浏览器中直接编写并运行真实代码,并获得即时 AI 反馈 — 无需本地设置。