Funkcje parametryzowane
Specjalizuj funkcję za pomocą parametrów w [nawiasach].
Funkcje parametryzowane to bezpłatna lekcja Mojo Academy na CoddyKit. To lekcja 2 z 4. Możesz przeczytać całą lekcję poniżej za darmo — a potem ćwiczyć ją interaktywnie w przeglądarce z wbudowanym edytorem kodu i tutorem AI dostępnym 24/7. To część ścieżki edukacyjnej Mojo Academy, a Twój postęp synchronizuje się między webem a aplikacją CoddyKit. Kurs Mojo Academy zawiera 4 lekcji w sumie.
Części tej lekcji nie zostały jeszcze przetłumaczone i są wyświetlane po angielsku.
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. 🎯
Często zadawane pytania
Czy lekcja „Funkcje parametryzowane” jest bezpłatna?
Tak — pełny tekst „Funkcje parametryzowane” jest dostępny za darmo tutaj w sieci. Aby ćwiczyć ją interaktywnie (wbudowany edytor kodu i tutor AI dostępny 24/7) i odblokować resztę kursu Mojo Academy, przejdź na CoddyKit PRO. Kurs Mojo Academy zawiera 4 lekcji w sumie.
Co nauczysz się w „Funkcje parametryzowane”?
Specjalizuj funkcję za pomocą parametrów w [nawiasach]. Ćwiczysz Mojo Academy z praktycznym kodem, który uruchamiasz bezpośrednio w przeglądarce, a tutor AI dostępny 24/7 odpowiada na Twoje pytania podczas pracy nad lekcją.
Czy potrzebuję doświadczenia, aby zacząć Mojo Academy?
Nie wymagamy żadnego doświadczenia. Mojo Academy w CoddyKit jest strukturyzowany dla początkujących i zaawansowanych użytkowników, więc możesz zacząć tutaj lub od początku i uczyć się w swoim tempie. To lekcja 2 z 4.
Ile czasu zajmuje lekcja „Funkcje parametryzowane”?
Większość lekcji CoddyKit trwa około 5–10 minut. Każda lekcja to mały, interaktywny krok, dzięki czemu robisz systematyczne postępy i zawsze wracasz dokładnie do tego samego miejsca — na webie i w aplikacji.
Czy mogę pisać i uruchamiać kod w tej lekcji Mojo Academy?
Tak. Każda lekcja Mojo Academy zawiera wbudowany edytor kodu, więc piszesz i uruchamiasz prawdziwy kod bezpośrednio w przeglądarce i od razu otrzymujesz sprzężenie zwrotne od AI — bez konfiguracji na komputerze.
Wszystkie lekcje w tym kursie
- Parametry a argumenty
- Funkcje parametryzowane
- Struktury parametryzowane
- Dlaczego kompilacja przyspiesza działanie