パラメーター化された関数
[bracket]パラメーターで関数を特殊化します
「パラメーター化された関数」はCoddyKit上の無料Mojo Academyレッスンです。 これはレッスン2/4です。 下記で完全なレッスンを無料で読むことができます。その後、ブラウザ内の組み込みコードエディタと24時間対応のAIチューターでハンズオン演習できます。 これはMojo Academy学習パスの一部であり、ウェブとCoddyKitアプリ全体で進捗が同期されます。 Mojo Academyコースには全4レッスンが含まれています。
このレッスンの一部はまだ翻訳されておらず、英語で表示されています。
Functions With a Parameter
A parameterized function takes a compile-time value in square brackets, letting one definition adapt itself before the program runs. ⚙️
fn repeat[count: Int](msg: String):
for i in range(count):
print(msg)The Bracket List
Place your parameters in brackets right after the function name, then your normal arguments in parentheses just after that.
fn scale[factor: Int](x: Int) -> Int:
return x * factorCalling With a Parameter
At the call site you fill the bracket with a constant, then pass runtime arguments in parentheses as usual.
var y = scale[10](4)Each Value Makes a Version
Mojo builds a fresh specialized copy of the function for each parameter value you use, each one tuned exactly for that constant.
Type Parameters
A parameter can even be a type. This is how you write one function that works for Int, Float, or any type you choose.
fn first[T: AnyType](value: T) -> T:
return valueGeneric With Traits
Constrain a type parameter with a trait so the body can safely use the methods that trait promises every conforming type has.
fn show[T: Stringable](value: T):
print(String(value))The Compiler Inlines It
Because the parameter is a constant, the compiler can fold it directly into the code, often removing loops or branches entirely.
Mixing Parameters and Arguments
One function can take both: a compile-time parameter for structure and runtime arguments for the actual data it processes.
fn fill[n: Int](value: Int):
for i in range(n):
print(value)Inferred Parameters
Sometimes Mojo can infer a parameter from the arguments, so you write less and the compiler fills in the rest for you.
Why It Stays Fast
Each specialization is its own optimized function with no runtime cost for the parameter, which is the heart of Mojo's speed.
One Source, Many Builds
You maintain a single readable definition, and the compiler quietly produces many fast, specialized builds behind the scenes.
Quick Check
Recall how a parameterized function is written.
Recap
You wrote parameterized functions: brackets hold compile-time values, the compiler specializes each version, and one source becomes many fast builds. 🎯
よくある質問
「パラメーター化された関数」レッスンは無料ですか?
はい。「パラメーター化された関数」の完全なテキストはこのウェブで無料で読めます。インタラクティブに演習し(組み込みコードエディタと24時間対応のAIチューター)、Mojo Academyコースの残りをアンロックするには、CoddyKit PROにアップグレードしてください。 Mojo Academyコースには全4レッスンが含まれています。
「パラメーター化された関数」で何を学びますか?
[bracket]パラメーターで関数を特殊化します ブラウザで直接実行するハンズオンコードでMojo Academyを演習し、24時間対応のAIチューターがレッスンを進める中での質問に答えます。
Mojo Academyを始めるのに経験は必要ですか?
事前経験は必要ありません。CoddyKitのMojo Academyは初級者から上級者向けに構成されているため、ここから始めるか最初から始めて、自分のペースで進むことができます。 これはレッスン2/4です。
「パラメーター化された関数」レッスンにはどのくらい時間がかかりますか?
ほとんどのCoddyKitレッスンは約5~10分かかります。各レッスンはコンパクトでインタラクティブなので、着実に進歩し、ウェブとアプリ全体で正確に前回の場所から再開できます。
このMojo Academyレッスンでコードを書いて実行できますか?
はい。すべてのMojo Academyレッスンに組み込みコードエディタが含まれているため、ブラウザでリアルコードを書いて実行し、即座のAIフィードバックを取得できます。ローカル設定は不要です。
このコースのすべてのレッスン
- パラメーターと引数
- パラメーター化された関数
- パラメーター化されたStruct
- コンパイル時処理が高速化につながる理由