fn関数の記述
厳密な型付けとパフォーマンスを重視して記述します
「fn関数の記述」はCoddyKit上の無料Mojo Academyレッスンです。 これはレッスン2/4です。 下記で完全なレッスンを無料で読むことができます。その後、ブラウザ内の組み込みコードエディタと24時間対応のAIチューターでハンズオン演習できます。 これはMojo Academy学習パスの一部であり、ウェブとCoddyKitアプリ全体で進捗が同期されます。 Mojo Academyコースには全4レッスンが含まれています。
このレッスンの一部はまだ翻訳されておらず、英語で表示されています。
Meet the fn Keyword
The fn keyword defines a stricter function. It trades some flexibility for safety and the speed Mojo is famous for. ⚡
fn greet():
print("Hello")Types Are Required
Every parameter in an fn must declare its type. This lets the compiler check your code and generate fast machine code.
fn add(a: Int, b: Int) -> Int:
return a + bDeclare the Return Type
An fn states what it returns with an arrow, like -> Int. If it returns something, the type must be spelled out.
fn square(x: Int) -> Int:
return x * xNo Return Type Means None
Leave off the arrow when an fn returns nothing. It performs an action, like printing, and hands back no value.
fn shout(msg: String):
print(msg)Arguments Are Immutable by Default
In an fn, arguments are read-only by default. Trying to reassign one is a compile error, which prevents subtle bugs.
fn show(x: Int):
print(x)The Compiler Has Your Back
Because types are known, the compiler catches mismatches before the program runs, turning many runtime crashes into early errors.
Strictness Unlocks Speed
Knowing every type ahead of time lets Mojo skip dynamic checks, so an fn can reach the performance of low-level languages.
Local Variables Need var
Inside an fn, declare new local variables with var. The strict scope rules keep your function predictable.
fn total() -> Int:
var sum = 0
sum = sum + 5
return sumErrors Must Be Declared
An fn that can fail must say so with raises. Without it, the compiler assumes the function never throws.
fn risky() raises:
raise Error("nope")Same Indentation Rules
Like def, an fn uses indentation for its body. The difference is the strictness of types, not the surrounding layout.
fn info():
print("a")
print("b")Built for Hot Paths
Reach for fn in the parts of your program that run millions of times, where every saved nanosecond truly adds up.
Quick Check
Recall the default behavior of fn arguments.
Recap
You met fn: strict, typed, and fast. Types are required, arguments are read-only by default, and the compiler guards correctness. 🎯
よくある質問
「fn関数の記述」レッスンは無料ですか?
はい。「fn関数の記述」の完全なテキストはこのウェブで無料で読めます。インタラクティブに演習し(組み込みコードエディタと24時間対応のAIチューター)、Mojo Academyコースの残りをアンロックするには、CoddyKit PROにアップグレードしてください。 Mojo Academyコースには全4レッスンが含まれています。
「fn関数の記述」で何を学びますか?
厳密な型付けとパフォーマンスを重視して記述します ブラウザで直接実行するハンズオンコードでMojo Academyを演習し、24時間対応のAIチューターがレッスンを進める中での質問に答えます。
Mojo Academyを始めるのに経験は必要ですか?
事前経験は必要ありません。CoddyKitのMojo Academyは初級者から上級者向けに構成されているため、ここから始めるか最初から始めて、自分のペースで進むことができます。 これはレッスン2/4です。
「fn関数の記述」レッスンにはどのくらい時間がかかりますか?
ほとんどのCoddyKitレッスンは約5~10分かかります。各レッスンはコンパクトでインタラクティブなので、着実に進歩し、ウェブとアプリ全体で正確に前回の場所から再開できます。
このMojo Academyレッスンでコードを書いて実行できますか?
はい。すべてのMojo Academyレッスンに組み込みコードエディタが含まれているため、ブラウザでリアルコードを書いて実行し、即座のAIフィードバックを取得できます。ローカル設定は不要です。
このコースのすべてのレッスン
- def関数の記述
- fn関数の記述
- それぞれを使う場面
- 戻り値の型とNone