How I am able to redefine Val in kotlin in the below program in description? Why does this program works? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

How I am able to redefine Val in kotlin in the below program in description? Why does this program works?

In kotlin the function argument is Val by default, but i re initialized the val argument successful inside function body. We can redefine only Var variable. But still this program worked. How? The program is : fun shippingCost(amount: Double, international: Boolean): Double { val amount:Double = when(international){ false -> { if(amount>=75.0) 0.0 else (amount*0.1) } true -> { if((amount*0.15)>50.0) 50.0 else (amount*0.15) } } return amount } fun main(args: Array<String>) { val total = readLine()!!.toDouble() val international = readLine()!!.toBoolean() println(shippingCost(total, international)) }

7th Aug 2021, 6:17 PM
Darshan Pagar
Darshan Pagar - avatar
1 Answer
- 1
In this code, the variable (val amount) is defined once, depending on the condition associated with the function argument, which has the same name (amount), about which a warning is displayed.
7th Aug 2021, 7:13 PM
Solo
Solo - avatar