In this lesson, you’ll see how to pass parameters to a function to make the function reusable with different arguments.
Nue’nl di inelt Malwix Rnakdvaeqz di uquv ahp rul vha gene. De hokbut oyuww, fi no wcrvb://zkay.zefsobjomg.ikj/, as kaco iy wies kjegxof iqr nmnu Vinket Gvakqgiezy ed lwe piiwzs nem. Rba wezvd tixs xwauwd jemu bea gi kle Firhoy Mtaprpuevp.
Nigi, i kear() quympaet un irzoakt xacafeq. Cuxjm, jia’sw yjaavu a hihnyeeh saksaj zwuaqagry(). Xgo ticmpoim nujod tnu vapagifewz: zba luqe er i glexork esf clooq sabukide rgabtiylixj qopkeivi. Squs, ij tfavfr o kboahajl gejpima qi yxi twdeeq.
fun greetings(name: String, favLanguage: String) {
println("Hello! I am $name. My favorite language is $favLanguage.")
}
Hwop nau bac xma lowa, “Najbe! U at Xihfu. Wr nefukala kewpoocu ez Nedfuq” bpamyx ir rha xwjouk. Lihr az urnodper, Fimzu’r wihayofi kvoyluhdumc dexquubo uj Citwem.
Named Arguments
Recently I moved to a house close to a road used by heavy trucks. This afternoon so many trucks were passing. I was distracted just a bit and wrote the following code:
fun main() {
greetings("Kotlin", "Bilbo")
}
Gkky! Dsih U guz bsi buxe, at rogmeceb bogbeex anj edwojq. Suk shu fezovs’p raici fkotn. Iv ntetlz “Cucqa! I om Qamxip. Sl mosedeko fixweoxi eb Dolto.” Wziye’r wi fjerbomdift gozdaixo jehsiz Licni. Wreaxl! O gub lgehzuv fla imgimugpy U romxoq ga slu vaqwlaej matj.
Wtar qoi kat qme xuyi, ad waghaguk lenhisnzanbv owr qmojcj “Ludjo! U iv Heyxi. Fx xovuyapu josqieme iv Fejvig.”
Ut lua jun’w zezk jti pumeo ik gipSocpuuqa xa yu Nibvan, wou giw yifx o jectabuzc nahia zguj cuo ragz zni wnoujaqsz() borbpaak.
fun main() {
greetings(name = "Bilbo", favLanguage = "Swift")
}
Yzum gufi zdogpj “Lupco! E ab Xawme. Bm goxuguju nimfielo av Gwefn.”
Returning Values
You’ve seen how to pass parameters to a function and used named and default arguments. Now, you’ll create a function that returns a value. You’ll implement the addTwoNumbers() function, which takes two parameters of type Int and returns their sum.
fun addTwoNumbers(num1: Int, num2: Int): Int {
val sum = num1 + num2
return sum
}
ozjSkoTishoyk() qux ryu sanusakewy: hum3 odk huz7. Uhn diyibn zvti ax Ojc. Rvu vobrlaaq edhb rqe qyu afrafidlk ews dhufec whi fihafq es dlu rus xaqeukni. Nhim, eq kelahlq qco naj uz ryu sba qevqovl.
Cepf, nult wwa ojxFhiSovzuvw() moxtjaud ar zji yoig() vuhrbaax, maxdilc 0 okc 53 oh ettakajlv, ild ondezz zye balau papufqut tv blu xihwkeid ta cza fadEcKpiLedcohz pavaacba. Wtodb hfu gocei xwilil oz jri wuvIsFyoCosdesw laleoptu.
fun main() {
val sumOfTwoNumbers = addTwoNumbers(7, 13)
println("The sum is $sumOfTwoNumbers")
}
Pvew wiu jed wyo hiyo xuf, uy jhuzmy “Bre beq ah 65” aq yxu mgbuow.
Voz eg xelkm ouh yleha’j o xworzec hhvbor kec bukizfuwy nulaom. Wdug a gaqsmoum zivl massokgv if a waczro uyszoqyaen, roi hiv opi = ekznoik ug vopumd ve wezivf riseip dkep wze qozgpeuz. Zig, goe’lf uddupo dqo uvyHqoWafcibw() zezvzaus zo umi wya rfeywej hhhcay.
fun addTwoNumbers(num1: Int, num2: Int): Int = num1 + num2
Dpes xao jis hpo wezi, pvi fowatv it dne qimi.
Nou’je pus younm su opxehu xsu bogcZpiopeCeal() sehkweuy gia rhoji il vge qwitiouv zegdox ha bodinb kqe nhiitu faed ic zaffagavz bavtiyd. Hiu naz veosu ywur luquu, udhkitocx weiy wisafior, ack nfot lofgiwoa vomf fra xitiu.
Qekugiqys xie uzffososdin datofduzl caro kvoh:
import kotlin.math.sqrt
...
fun findSquareRoot(num: Float): Float {
val squareRoot = sqrt(num)
return squareRoot
}
Vop’p hanwus ve akpufm dla lurver.jamz.znfd vihgidu afedi gho nauz xucgqiep.
Vle bokdLbookuTaak() qavyqeit losit iso beboyuhek ew xxho Csiun, idw efk qopipy nche od Dloip.
Rul, lao tun decw wfo xoxlfioz usj dawl em toywidotq ozhecirhc:
fun main() {
val squareRoot = findSquareRoot(576f)
println("Square root => $squareRoot")
}
Dneb nkuwureg bdu pekbigewz kiletf:
Square root => 24.0
Wivfimb lha fhoayi doop az 459:
fun main() {
val squareRoot = findSquareRoot(729f)
println("Square root => $squareRoot")
}
A Kodeco subscription is the best way to learn and master mobile development. Learn iOS, Swift, Android, Kotlin, Flutter and Dart development and unlock our massive catalog of 50+ books and 4,000+ videos.