Home > front end >  Output var not calculating
Output var not calculating

Time:12-07

This is a change counter program. The change is counting correctly, but the remainder is not (remChange()). After watching a few videos, and w3schools tutorials, I'm still coming up blank as to why this is. I've added a returnChange at the bottom of each function and was able to get that last 2 functions rounded (from the float). Did I do the floating point wrong in the println? Are my return tags in the wrong place? I'm just trying to get it to work by counting the coins and giving me back the change.

Here is my code: let me know, ill be researching in the meantime.

fun main(args: Array<String>) {
    var  change = 10.88

    getDollars(change)
    getQuarters(change)
    getDimes(change)
    getNickles(change)
}

fun getDollars(change: Double): Double{
    val numofdolla = change/(1.00).toFloat();
    println(numofdolla.toInt());
    val remChange =  change - numofdolla * 1.00;
    println(remChange)
    return change
}

fun getQuarters(change: Double): Double{
    val numofqtr = change/(0.25).toFloat();
    println(numofqtr.toInt());
    val remChange = change - numofqtr * 0.25;
    println(remChange)
    return change
}

fun getDimes(change: Double): Double{
    val numofdime = change/(0.10).toFloat();
    println(numofdime.toInt());
    val remChange =  change - numofdime * 0.10;
    println("%.2f".format(remChange))
    return change
}

fun getNickles(change: Double): Double{
    val numofnick = change/(0.05).toFloat();
    println(numofnick.toInt());
    val remChange = change - numofnick * 0.05;
    println("%.2f".format(remChange))
    return change
}

I'm not looking for anyone to write my code, as an explanation would be just perfect.

CodePudding user response:

I'm not sure what you're expecting to happen here, but the title of your question seems to suggest that you expect the change variable to change when calling those helper functions.

You cannot mutate a function argument within a function in Kotlin, but you can indeed return a new value from the function. It seems you tried to do that, but here your main function is not using the return values of the getXxxx functions. So your change variable is never updated.

You can update the change variable based on the result of those functions:

fun main(args: Array<String>) {
    var change = 10.88

    change = getDollars(change)
    change = getQuarters(change)
    change = getDimes(change)
    change = getNickles(change)
}

That said, those functions currently return the original change variable (which is unchanged), so if you want the value to change, you probably want to return remChange instead.

CodePudding user response:

I am having the same problem.

  println(getDollars(50.25));
  println(getQuarters(DO I ADD IT HERE?);

  }
  fun dollars(changeDue: Double): Double{
  var numOfDollars = changeDue/1.0;
  var remainder = changeDue%1.0   
   println(numOfDollars);
return remainder;
}

fun quarters(DO I ADD HERE? ): Double{
var numOfQuarters = changeDue/0.25;
println(numOfQuarters);
}

I am trying to Return values from one function to another?

Thank you

  • Related