4

Swift closure is confusing me when using in variable. Consider the following example

let divide = {(val1: Int, val2: Int) -> Int in 
   return val1 / val2 
}
let result = divide(200, 20)
println(result)

Here divide is a variable but it can takes parameters. I know from other language, only function can take parameters. So, what is the difference between variable and function? What is the advantage when use clousure in swift variable?

Ozgur Vatansever
  • 49,246
  • 17
  • 84
  • 119
Alamin
  • 877
  • 2
  • 12
  • 17
  • possible duplicate of http://stackoverflow.com/questions/24108667/what-is-the-difference-between-functions-and-closures – Fonix Dec 03 '15 at 04:54

3 Answers3

2

Simply Closure is an block of code (anonymous functions) that u can use it as an object and pass it around between viewcontroller, its good if u want to do something after the function is complete or after pressed a button (can achieve the same with protocol/delegate, unwind, ...), it also can take params like a function and return value

The great thing is that it can let u access non-local variable (when u pass it to another viewcontroller) then u can do stuff with it

Tj3n
  • 9,837
  • 2
  • 24
  • 35
0

in your example, it doesnt really highlight the difference between closures and functions. closures and functions are very similar. like how a function can take in parameters, or use variables outside of the function if they are defined in the class, a closure can do the same thing, but from within its own local scope.

psuedo code example:

class aaa {

  var a = 5;

  func foo (b:Int) -> Int {
     return a+b;  //a not defined in local scope, but we can still use it
  }
}

now with a closure its the same thing, but you can use local variables in the same way a variable defined at the class level is used

class bbb {

  var b = 1;
  var closure;

  func foo (c:Int) -> Int {
     var d = b+c; //d is defined locally
     closure = (val1:Int) -> Int { Int in
       return val1 + b * d; //we can use b and d here! they are captured in the block now, and will remain with the block until executed
     }
  }

  func bar () {

    b = 3;

    closure(5); //now we execute closure, now the variable d in foo() is being used but in a completely different scope, and even if we changed the value of b before running it, the value of b would be the original value captured in the closure when it was made aka 1 and not 3
  }
}

so now if you ran foo then bar, you can see how closures differ to functions

so the real beauty is in the capturing of variables locally, to be used at a later time in possibly a completely different scope, where as functions can only use the parameters you give them and/or the variables defined in the class

Fonix
  • 11,447
  • 3
  • 45
  • 74
0

A variable holds/references a value; a function is a value. Thus variables can hold/reference numbers, strings, instances and, in some languages, functions. Languages that allow variables to hold/reference functions are languages with 'first class functions'

The advantage of closures is that they 'capture' values in their 'lexical scope'. For example, say you need a callback function that executes when some computation completes. You want the callback to dismiss a view controller and log some information:

class MyViewController : ViewController {
  func takeAction (message:String) {
    // do some stuff, then
    doComputation { 
      self.dismissViewController()
      NSLog ("took action: \(message)")
    }
  }
}   

In the above, the argument to doComputation is a closure; it captures the values bound to self and message from the closure's lexical environment.

GoZoner
  • 67,920
  • 20
  • 95
  • 145