0Pricing
Mojo Academy · Lektion

Eine fn-Funktion schreiben

Strikt, typisiert und auf Leistung ausgerichtet.

Eine fn-Funktion schreiben ist eine kostenlose Mojo Academy-Lektion auf CoddyKit. Dies ist Lektion 2 von 4. Du kannst die komplette Lektion unten kostenlos lesen – dann übst du sie direkt im Browser mit einem integrierten Code-Editor und einem KI-Tutor rund um die Uhr. Sie ist Teil des Mojo Academy-Lernpfads, und dein Fortschritt wird über Web und CoddyKit-App synchronisiert. Der Mojo Academy-Kurs umfasst insgesamt 4 Lektionen.

Teile dieser Lektion wurden noch nicht übersetzt und werden auf Englisch angezeigt.

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 + b

Declare 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 * x

No 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 sum

Errors 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. 🎯

Häufig gestellte Fragen

Ist die Lektion „Eine fn-Funktion schreiben“ kostenlos?

Ja — der vollständige Text von „Eine fn-Funktion schreiben“ ist hier im Web kostenlos zu lesen. Um sie interaktiv zu üben (integrierter Code-Editor und 24/7 KI-Tutor) und den Rest des Mojo Academy-Kurses freizuschalten, upgrade auf CoddyKit PRO. Der Mojo Academy-Kurs umfasst insgesamt 4 Lektionen.

Was lerne ich in „Eine fn-Funktion schreiben“?

Strikt, typisiert und auf Leistung ausgerichtet. Du übst Mojo Academy mit praktischem Code, den du direkt im Browser ausführst, und ein 24/7 KI-Tutor beantwortet deine Fragen während du die Lektion bearbeitest.

Brauche ich Erfahrung, um Mojo Academy zu starten?

Keine Vorkenntnisse erforderlich. Mojo Academy auf CoddyKit ist für Anfänger bis fortgeschrittene Lernende strukturiert, sodass du hier starten oder von Anfang an beginnen und in deinem eigenen Tempo voranschreiten kannst. Dies ist Lektion 2 von 4.

Wie lange dauert die Lektion „Eine fn-Funktion schreiben“?

Die meisten CoddyKit-Lektionen dauern etwa 5–10 Minuten. Jede ist kompakt und interaktiv, sodass du stetig Fortschritte machst und genau dort weitermachst, wo du aufgehört hast – im Web und in der App.

Kann ich in dieser Mojo Academy-Lektion Code schreiben und ausführen?

Ja. Jede Mojo Academy-Lektion enthält einen integrierten Code-Editor, sodass du echten Code direkt in deinem Browser schreibst und ausführst und sofort KI-Feedback erhältst — ohne lokale Einrichtung erforderlich.

Alle Lektionen in diesem Kurs

  1. Eine def-Funktion schreiben
  2. Eine fn-Funktion schreiben
  3. Wann Sie welche Variante verwenden
  4. Rückgabetypen und None
← Zurück zu Mojo Academy