Diego Vente Seminario
Diego Vente Seminario

Reputation: 97

How to return a variable in a function in kotlin

I created a function that recieves input and compare it to a list, when find a match it return the match, in this case this match is the attribute of a class that i created.

I understand that the problem is with the return statement, so in the beginning of the function I declare the return as "Any", further more than that I'm kinda lost.

The error is this: A 'return' expression required in a function with a block body ('{...}')

   class Class1(var self: String)       
   var test_class = Class1("")       

   fun giver(){       
       test_class.self = "Anything"       
   }

   class Funciones(){       
           fun match_finder(texto: String): Any{       
               var lista = listOf<String>(test_class.self)       
               var lista_de_listas = listOf<String>("test_class.self")       
               var count = -1       
               for (i in lista_de_listas){       
                   count = count + 1       
                   if (texto == i){       
                       lista_de_listas = lista       
                       var variable = lista_de_listas[count]       
                       return variable        

                   }       


               }       

           }       
       }           


   fun main(){       
       giver()       
       var x = "test_class.self"       
       var funcion = Funciones()       
       var y = funcion.match_finder(x)       
       println(y)       

   }

Upvotes: 1

Views: 4006

Answers (2)

user2340612
user2340612

Reputation: 10704

To explain you what the problem is, let's consider the following code:

class MyClass {
    fun doSomething(): String {
        val numbers = listOf(1, 2, 3)
        for (number in numbers) {
            if (number % 2 == 0) {
                return "There is at least one even number in the list"
            }
        }
    }
}

If you try compiling it you'll get the same error message as in your question: A 'return' expression required in a function with a block body ('{...}'). Why is that?

Well, we defined a function doSomething returning a String (it could be any other type) but we're returning a result only if the list of numbers contains at least one even number. What should it return if there's no even number? The compiler doesn't know that (how could it know?), so it prompts us that message. We can fix the code by returning a value or by throwing an exception:

class MyClass {
    fun doSomething(): String {
        val numbers = listOf(1, 2, 3)
        for (number in numbers) {
            if (number % 2 == 0) {
                return "There is at least one even number in the list"
            }
        }

        // return something if the list doesn't contain any even number
        return "There is no even number in the list"
    }
}

The same logic applies to your original code: what should the function return if there is no i such that texto == i?

Please also note that the solution you proposed may be syntactically correct - meaning it compiles correctly - but will probably do something unexpected. The for loop is useless since the if/else statement will always cause the function to return during the first iteration, so the value "There is no match" could be returned even if a match actually exists later in the list.

Upvotes: 1

Diego Vente Seminario
Diego Vente Seminario

Reputation: 97

I searched online, if someone has the same problem, the correct code is as follows:

   class Funciones(){       
       fun match_finder(texto: String): Any{       
           var lista = listOf<String>(test_class.self)       
           var lista_de_listas = listOf<String>("test_class.self")       
           var count = -1
           var variable = " "
           for (i in lista_de_listas){       
               count = count + 1       
               if (texto == i){       
                   lista_de_listas = lista       
                   var variable = lista_de_listas[count]       
                   return variable        

               } else {
                   return "There is no match"
                  }      


           }  
        return variable     
       }       
   }   

Upvotes: 0

Related Questions