G
G
Gleb2021-03-10 12:08:41
Swift
Gleb, 2021-03-10 12:08:41

What is the correct way to call a function?

I decided to drag the code into an external method and everything fell apart.
what is the right way to put f into the parabol parameter? or other ways to correctly call a function without errors.

func f(x: Double) -> Double {
    return x*x 
}

func parabol() -> Void {
    var : Double = 1e-6

    var x = [Double]()
    x.append(3)
    x.append(4)
    x.append(5)

// Cannot call value of non-function type '[Double]'
    var f = [Double]()
    f.append(f(x: x[0]))
    f.append(f(x: x[1]))
    f.append(f(x: x[2]))
}

and so
func parabol() -> Void {


// Type of expression is ambiguous without more context
        x.sort { (one, two) in return f(x: one) < f(x: two) }

}

Answer the question

In order to leave comments, you need to log in

1 answer(s)
I
iMaximus, 2021-03-10
@shevzoom

func f(x: Double) -> Double {
    return x*x
}

var x = [Double]()

func parabol() {
    //var g: Double = 1e-6
    x.append(3)
    x.append(4)
    x.append(5)

// Cannot call value of non-function type '[Double]'
    var y = [Double]()
    y.append(f(x: x[0]))
    y.append(f(x: x[1]))
    y.append(f(x: x[2]))
}

func parabolN() {
// Type of expression is ambiguous without more context
   x.sort { (one, two) in return f(x: one) < f(x: two)
   }
}

I don't know why, but it doesn't need to be written like that. Plus, you can't declare a function with the same name twice.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question