0

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?

2
  • 2
    Remove return type parentheses from fun AddVectorToPoint(i: Int): Point() line. It must like this. Commented Apr 27, 2021 at 13:13
  • 1
    Also, it's usual to start a function name with a lower-case letter; see the Kotlin coding convensions. Commented Apr 27, 2021 at 14:54

1 Answer 1

2

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)
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.