0
votes

My code:

class Point(xA: Int)
{
    val x = xA
}

fun AddVectorToPoint(i: Int): Point()
{
    val x: Int = 1 + i
    val obj = Point(x)
    return obj
}

fun main()
{
    val points = mutableListOf(Point(0))
    points.add(AddVectorToPoint(2))
}

But when I try to compile this, I get "test.kt:6:36: error: expecting a top level declaration > fun AddVectorToPoint(i: Int): Point() {" What am I doing wrong?

1
Remove return type parentheses from fun AddVectorToPoint(i: Int): Point() line. It must like this. - Furkan
Also, it's usual to start a function name with a lower-case letter; see the Kotlin coding convensions. - gidds

1 Answers

2
votes

Remove the parantheses after the return type

fun AddVectorToPoint(i: Int): Point
{
    val x: Int = 1 + i
    val obj = Point(x)
    return obj
}

Or to make it more concise:

fun AddVectorToPoint(i: Int) = Point(1 + i)