位置引数とキーワード引数
位置または名前で値を渡します
「位置引数とキーワード引数」はCoddyKit上の無料Mojo Academyレッスンです。 これはレッスン1/4です。 下記で完全なレッスンを無料で読むことができます。その後、ブラウザ内の組み込みコードエディタと24時間対応の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.
よくある質問
「位置引数とキーワード引数」レッスンは無料ですか?
はい。「位置引数とキーワード引数」の完全なテキストはこのウェブで無料で読めます。インタラクティブに演習し(組み込みコードエディタと24時間対応のAIチューター)、Mojo Academyコースの残りをアンロックするには、CoddyKit PROにアップグレードしてください。 Mojo Academyコースには全4レッスンが含まれています。
「位置引数とキーワード引数」で何を学びますか?
位置または名前で値を渡します ブラウザで直接実行するハンズオンコードでMojo Academyを演習し、24時間対応のAIチューターがレッスンを進める中での質問に答えます。
Mojo Academyを始めるのに経験は必要ですか?
事前経験は必要ありません。CoddyKitのMojo Academyは初級者から上級者向けに構成されているため、ここから始めるか最初から始めて、自分のペースで進むことができます。 これはレッスン1/4です。
「位置引数とキーワード引数」レッスンにはどのくらい時間がかかりますか?
ほとんどのCoddyKitレッスンは約5~10分かかります。各レッスンはコンパクトでインタラクティブなので、着実に進歩し、ウェブとアプリ全体で正確に前回の場所から再開できます。
このMojo Academyレッスンでコードを書いて実行できますか?
はい。すべてのMojo Academyレッスンに組み込みコードエディタが含まれているため、ブラウザでリアルコードを書いて実行し、即座のAIフィードバックを取得できます。ローカル設定は不要です。
このコースのすべてのレッスン
- 位置引数とキーワード引数
- 引数のデフォルト値
- 複数の値を返す
- 関数シグネチャの読み方