UnknownError
UnknownError

Reputation: 103

Kotlin Return Expression

I have google this and searched here but I have hit a wall.

I have a function which is to return an Int. It tells me I need to add another return expression to the overall function but I can't figure out where it needs to go to be done correctly, i have tried a few variations including adding the same return I use within the if statements but before the closing tag which throws up errors within the rest of the code

I call the function like this;

var ans = GetQuestion(1)


fun GetQuestion(cat:Int): Int {
val btn1 = findViewById(R.id.button1) as Button
val btn2 = findViewById(R.id.button2) as Button
            val btn3 = findViewById(R.id.button3) as Button
            val btn4 = findViewById(R.id.button4) as Button

            btn1.setVisibility(View.VISIBLE)
            btn2.setVisibility(View.VISIBLE)
            btn3.setVisibility(View.VISIBLE)
            btn4.setVisibility(View.VISIBLE)

            val initialimageview = findViewById(R.id.imageview_Qresponse) as ImageView
            initialimageview.setBackgroundResource(R.drawable.question_2);

            val viewQ = findViewById(R.id.textview_question) as TextView
            if (cat==1) { // Multiplication
                var a = rand(5,80)
                var b = rand(2,50)
                val ans = a * b
                var opType = " X "
                viewQ.text = ("" + a + " " + opType + " " + b + "?");
                val listOfAns = mutableListOf(ans,ans-a,ans-b,ans+a)
                listOfAns.shuffle()
                btn1.text=("" + listOfAns.elementAt(0))
                btn2.text=("" + listOfAns.elementAt(1))
                btn3.text=("" + listOfAns.elementAt(2))
                btn4.text=("" + listOfAns.elementAt(3))
                return ans
                listOfAns.clear()
            }
            if (cat==2) { // Addition
                var a = rand(5,250)
                var b = rand(2,140)
                val ans = a + b
                var opType = " + "
                viewQ.text = ("" + a + " " + opType + " " + b + "?");
                val listOfAns = mutableListOf(ans,ans+rand(2,4),ans+rand(5,10),ans-rand(2,10))
                listOfAns.shuffle()
                btn1.text=("" + listOfAns.elementAt(0))
                btn2.text=("" + listOfAns.elementAt(1))
                btn3.text=("" + listOfAns.elementAt(2))
                btn4.text=("" + listOfAns.elementAt(3))
                return ans
                listOfAns.clear()
            }
            if (cat==3) { // Subtraction
                var a = rand(80,150)
                var b = rand(2,79)
                var ans = a - b
                var opType = " - "
                viewQ.text = ("" + a + " " + opType + " " + b + "?");
                val listOfAns = mutableListOf(ans,ans+rand(2,5),ans+rand(6,10),ans-rand(2,10))
                listOfAns.shuffle()
                btn1.text=("" + listOfAns.elementAt(0))
                btn2.text=("" + listOfAns.elementAt(1))
                btn3.text=("" + listOfAns.elementAt(2))
                btn4.text=("" + listOfAns.elementAt(3))
                return ans
                listOfAns.clear()
            }
            if (cat==4) { // Division
                var safedivision = rand(3,16)
                var b = rand(2,20)
                var a = b * safedivision
                var ans = a / b
                var opType = " / "
                viewQ.text = ("" + a + " " + opType + " " + b + "?");
                val listOfAns = mutableListOf(ans,ans+rand(2,5),ans+rand(6,10),ans-rand(2,10))
                listOfAns.shuffle()
                btn1.text=("" + listOfAns.elementAt(0))
                btn2.text=("" + listOfAns.elementAt(1))
                btn3.text=("" + listOfAns.elementAt(2))
                btn4.text=("" + listOfAns.elementAt(3))
                return ans
                listOfAns.clear()
            }
  }

As a side note I wasn't sure if I have to use var of val when writing var ans = GetQuestion(1) however both methods throw up the same return error so I can amend that later if required

Upvotes: 2

Views: 241

Answers (2)

Tugay
Tugay

Reputation: 2214

If your function body doesn't have return keyword, then both sides of if-else must have return in them, for example:

fun dummyFunc(Int a):Int{
    if (a > 0){
        return 5
    }else{
        return -5
    }
}

In this case, you don't need to add another return at the end of your code. But in your code, you don't have else block, so either you will do like the above answer by Priyanka Rajput or you will ad else statement. And after all, your code has reused code in if blocks, you can extract them and add after all the ifs you have written:

fun GetQuestion(cat:Int): Int {
    val btn1 = findViewById(R.id.button1) as Button
    val btn2 = findViewById(R.id.button2) as Button
    val btn3 = findViewById(R.id.button3) as Button
    val btn4 = findViewById(R.id.button4) as Button

    btn1.setVisibility(View.VISIBLE)
    btn2.setVisibility(View.VISIBLE)
    btn3.setVisibility(View.VISIBLE)
    btn4.setVisibility(View.VISIBLE)

    val initialimageview = findViewById(R.id.imageview_Qresponse) as ImageView
    initialimageview.setBackgroundResource(R.drawable.question_2);

    val viewQ = findViewById(R.id.textview_question) as TextView
    
    var a = 0
    var b = 0
    var ans = 0
    var opType = ""
    var viewQText = ""
    var listOfAns = mutableListOf()

    if (cat==1) { // Multiplication
        var a = rand(5,80)
        var b = rand(2,50)
        val ans = a * b
        var opType = " X "
        viewQText = ("" + a + " " + opType + " " + b + "?");
        val listOfAns = mutableListOf(ans,ans-a,ans-b,ans+a)
    }
        if (cat==2) { // Addition
            a = rand(5,250)
            b = rand(2,140)
            ans = a + b
            opType = " + "
            viewQText = ("" + a + " " + opType + " " + b + "?");
            listOfAns = mutableListOf(ans,ans+rand(2,4),ans+rand(5,10),ans-rand(2,10))
         }
        if (cat==3) { // Subtraction
            a = rand(80,150)
            b = rand(2,79)
            ans = a - b
            opType = " - "
            viewQText = ("" + a + " " + opType + " " + b + "?");
            listOfAns = mutableListOf(ans,ans+rand(2,5),ans+rand(6,10),ans-rand(2,10))
            }
        if (cat==4) { // Division
            safedivision = rand(3,16)
            b = rand(2,20)
            a = b * safedivision
            ans = a / b
            opType = " / "
            viewQText = ("" + a + " " + opType + " " + b + "?");
            listOfAns = mutableListOf(ans,ans+rand(2,5),ans+rand(6,10),ans-rand(2,10))
        }



                viewQ.text = viewQText
                
                listOfAns.shuffle()
                btn1.text=("" + listOfAns.elementAt(0))
                btn2.text=("" + listOfAns.elementAt(1))
                btn3.text=("" + listOfAns.elementAt(2))
                btn4.text=("" + listOfAns.elementAt(3))
                return ans
                listOfAns.clear()
  }

While doing this, I have realized that you have a lot of style and coding problems. First, you mustn't call findViewById method too much, it's expensive. You must declare your Views as global variables and initialize them in your onCreate method. Second, you don't use listOfAns, maybe you have other plans for it, you will later use, I don't know, but all I know is, your code runs till the line with return keyword, which means listofAns.clear() will not be executed. And it would be better if you used when instead of if-else

Upvotes: 1

Priyanka
Priyanka

Reputation: 1875

You can keep variable ans at the top of the function as below -

 var ans = 0

and replace val ans from the all if conditions with ans. Before the last } add return statement

return ans

Upvotes: 3

Related Questions