-2

Swift provided the code below. How's it possible to call the squishEm function without parenthesis? Isn't that the syntax difference between a function and property?

func squishEm() {
// Iterate over graphics and squish each one.
for graphic in graphics {
    squishGraphic(graphic: graphic)
}
}

// Create and add Squish ’Em! button.
let squishButton = Button(name: "Squish ’Em!")
squishButton.onTap = squishEm
scene.button = squishButton
BBB
  • 43
  • 7
  • 3
    This is essentially the same question as your previous one. – vacawama Jun 30 '17 at 18:12
  • 1
    Possible duplicate of [How is it possible to call a function with parameters without passing in values? \[Swift Playground\]](https://stackoverflow.com/questions/44851319/how-is-it-possible-to-call-a-function-with-parameters-without-passing-in-values) –  Jun 30 '17 at 18:47

1 Answers1

8

You are not invoking squishEm. You are saying the onTap function for squishButton is the squishEm function. When the button is tapped, internally it invokes squishEm(), using the parens.

A function invocation has parenthesis. A reference to a function does not.

For further reading, I suggest reading the section "Function Types" in the Swift documentation for Functions.

i_am_jorf
  • 53,608
  • 15
  • 131
  • 222