i cannot able to process method

3478 views swift
5

func mymethod(getno:Int)->(myval :Int,defaults :Int){

return (getno,8)
 }
  print(mymethod(getno: 2))

the out put for the above program is my val is 2 default is 8

ok i can understand above program

but what i have doubt in this method

“func makeIncrementer() -> ((Int) -> Int) {
func addOne(number: Int) -> Int {
    return 1 + number
}
return addOne
   }
    var increment = makeIncrementer()
    increment(7)

i cannot able to understand this line (var increment=makeIncrementer()) what s happening there ?

can any one explain me brief?

answered question

1 Answer

9

makeIncrementer returns a function that accepts an Int and return an Int , so this

var increment = makeIncrementer()

makes increment variable to be equal to

func addOne(number: Int) -> Int {
  return 1 + number
}

so increment(7) = addOne(7)

You may read Functions chapter in Swift book

posted this

Have an answer?

JD

Please login first before posting an answer.