Varsayılan Bağımsız Değişken Değerleri
Bazı parametreleri isteğe bağlı hâle getirin.
Varsayılan Bağımsız Değişken Değerleri, CoddyKit'te ücretsiz bir Mojo Academy dersidir. Bu, 4 dersinin 2. dersidir. Aşağıdan dersin tamamını ücretsiz okuyabilir, sonra tarayıcıda yerleşik kod editörü ve 7/24 yapay zeka koçu ile uygulamalı olarak pratik yapabilirsin. Bu, Mojo Academy öğrenme yolunun bir parçasıdır ve ilerlemeniz web ve CoddyKit uygulaması arasında senkronize olur. Mojo Academy kursu toplamda 4 dersten oluşur.
Bu dersin bazı bölümleri henüz çevrilmemiş olup İngilizce olarak gösterilmektedir.
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 = 3Why 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"): ... # okA 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 80Multiple 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 normDefaults 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.
Sıkça Sorulan Sorular
“Varsayılan Bağımsız Değişken Değerleri” dersi ücretsiz mi?
Evet — “Varsayılan Bağımsız Değişken Değerleri” dersin tüm metni burada web'de ücretsiz olarak okunabilir. Etkileşimli olarak pratik yapmak (yerleşik kod editörü ve 7/24 yapay zeka koçu) ve Mojo Academy kursunun geri kalanını açmak için CoddyKit PRO'ya yükselt. Mojo Academy kursu toplamda 4 dersten oluşur.
“Varsayılan Bağımsız Değişken Değerleri” dersinde ne öğreneceğim?
Bazı parametreleri isteğe bağlı hâle getirin. Mojo Academy ile uygulamalı kodu tarayıcıda doğrudan çalıştırarak pratik yaparsın ve 7/24 yapay zeka koçu dersi çalışırken sorularını yanıtlar.
Mojo Academy öğrenmeye başlamak için deneyim gerekli mi?
Önceden deneyim gerekmez. CoddyKit'te Mojo Academy, başlangıçtan ileri seviyeye kadar yapılandırıldığı için buradan başlayabilir veya başından başlayıp kendi hızında ilerleme yapabilirsin. Bu, 4 dersinin 2. dersidir.
“Varsayılan Bağımsız Değişken Değerleri” dersi ne kadar sürer?
Çoğu CoddyKit dersi yaklaşık 5–10 dakika sürer. Her biri kısa ve etkileşimli olduğu için sabit ilerleme yaparsın ve web ile uygulama arasında tam olarak bıraktığın yerden devam edebilirsin.
Bu Mojo Academy dersinde kod yazıp çalıştırabilir miyim?
Evet. Her Mojo Academy dersi yerleşik bir kod editörü içerir, bu sayede tarayıcıda gerçek kod yazıp çalıştırabilir ve anlık yapay zeka geri bildirimi alırsın — yerel kurulum gerekli değildir.
Bu kursun tüm dersleri
- Konumsal ve Anahtar Sözcüklü Bağımsız Değişkenler
- Varsayılan Bağımsız Değişken Değerleri
- Birden Çok Değer Döndürme
- İşlev İmzalarını Okuma