Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the meaning of the "()" in the return type of function calcDecrement?

Tags:

function

swift

I just started learning Swift and came across function types and how to return them. I just want to know what the () means in the return value of function calcDecrement? I know the function returns a reference to the inner function decrementer so is the () return type for a function?

func calcDecrement(forDecrement total: Int) -> () -> Int {
    var overallDecrement = 0
    func decrementer() -> Int {
        overallDecrement -= total
        return overallDecrement
    }
    return decrementer
}

let decrem = calcDecrement(forDecrement: 30)
print(decrem())
like image 879
Sriram Sridhar Avatar asked Oct 28 '25 18:10

Sriram Sridhar


1 Answers

() -> Int is the return type and this represents a function that takes no arguments and returns an Int as the result.

decrementer matches this exactly, so it is good as a function to return.

If the return type took an Int as a parameter, then the return type would be (Int) -> Int.

See Apple’s documentation at The Swift Programming Language for more details about the type of a function.

like image 120
Gary Makin Avatar answered Oct 30 '25 09:10

Gary Makin