0Pricing
Mojo Academy · レッスン

引数のデフォルト値

一部のパラメーターを省略可能にします

「引数のデフォルト値」はCoddyKit上の無料Mojo Academyレッスンです。 これはレッスン2/4です。 下記で完全なレッスンを無料で読むことができます。その後、ブラウザ内の組み込みコードエディタと24時間対応のAIチューターでハンズオン演習できます。 これはMojo Academy学習パスの一部であり、ウェブとCoddyKitアプリ全体で進捗が同期されます。 Mojo Academyコースには全4レッスンが含まれています。

このレッスンの一部はまだ翻訳されておらず、英語で表示されています。

Making a Parameter Optional

A default value lets a parameter stand in for itself. If the caller skips it, Mojo quietly uses the value you wrote in the definition.

fn greet(name: String, times: Int = 1):
    for _ in range(times):
        print(name)

Calling With and Without

Now both calls work. Omit times and you get the default of 1; pass it and you override the default for that one call.

greet("Mojo")        # times = 1
greet("Mojo", 3)     # times = 3

Why Defaults Are Handy

Defaults capture the common case. Most callers never touch the option, yet the power is there for the few who need it.

Defaults Go Last

Parameters with defaults must come after required ones. Otherwise Mojo could not tell which value you meant to skip.

fn open(path: String, mode: String = "r"): ...  # ok

A Required-Then-Optional Shape

Think of it as required-first, optional-last. Reading left to right, you supply what is needed, then accept defaults for the rest. This shape is predictable.

Overriding Just One Default

To change a later default while keeping earlier ones, use a keyword. You skip straight to the option you care about.

fn make(width: Int = 80, height: Int = 24): ...
make(height=40)  # width stays 80

Multiple Defaults Together

A function can give several parameters defaults at once. Callers fill only what they want and let the rest fall back.

fn box(w: Int = 10, h: Int = 10, fill: String = "x"): ...

Defaults Build Friendly APIs

Good defaults make a function easy to start with and powerful to grow into. Beginners call it plainly; experts tune the options.

Choosing a Sensible Default

Pick the value most callers would want anyway. A surprising default is worse than none, because it hides behavior the reader did not expect.

The Default Is Evaluated Once

The default expression belongs to the signature, not the body. Treat it as a fixed fallback that documents the normal value.

fn retry(count: Int = 3): ...  # 3 is the documented norm

Defaults Plus Keywords Shine

Defaults and keyword calls are a great pair. Together they let you write functions with many knobs that still feel simple to call.

request(url, timeout=30, retries=5)

Quick Check

Where must a parameter with a default value be placed in a Mojo function?

Recap: Smart Fallbacks

Default values make parameters optional and capture the common case. Put them last, choose them wisely, and override single ones with keywords.

よくある質問

「引数のデフォルト値」レッスンは無料ですか?

はい。「引数のデフォルト値」の完全なテキストはこのウェブで無料で読めます。インタラクティブに演習し(組み込みコードエディタと24時間対応のAIチューター)、Mojo Academyコースの残りをアンロックするには、CoddyKit PROにアップグレードしてください。 Mojo Academyコースには全4レッスンが含まれています。

「引数のデフォルト値」で何を学びますか?

一部のパラメーターを省略可能にします ブラウザで直接実行するハンズオンコードでMojo Academyを演習し、24時間対応のAIチューターがレッスンを進める中での質問に答えます。

Mojo Academyを始めるのに経験は必要ですか?

事前経験は必要ありません。CoddyKitのMojo Academyは初級者から上級者向けに構成されているため、ここから始めるか最初から始めて、自分のペースで進むことができます。 これはレッスン2/4です。

「引数のデフォルト値」レッスンにはどのくらい時間がかかりますか?

ほとんどのCoddyKitレッスンは約5~10分かかります。各レッスンはコンパクトでインタラクティブなので、着実に進歩し、ウェブとアプリ全体で正確に前回の場所から再開できます。

このMojo Academyレッスンでコードを書いて実行できますか?

はい。すべてのMojo Academyレッスンに組み込みコードエディタが含まれているため、ブラウザでリアルコードを書いて実行し、即座のAIフィードバックを取得できます。ローカル設定は不要です。

このコースのすべてのレッスン

  1. 位置引数とキーワード引数
  2. 引数のデフォルト値
  3. 複数の値を返す
  4. 関数シグネチャの読み方
← Mojo Academyに戻る