Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

iOS Closures in Swift First Class Functions Functions as Data

Shaan Marok
PLUS
Shaan Marok
Courses Plus Student 1,015 Points

Assigning functions to variables in Swift

When we assign functions to variables in Swift, are the labels always omitted or is there a way to include them?

Example, for argument labels a and b

func sum(a: Int, b: Int) -> Int {
    return a + b
}

let addTwoNumbers: (Int, Int) -> Int = sum
addTwoNumbers(1, 2)
Jeff McDivitt
Jeff McDivitt
23,970 Points

They are included you omit them with an underscore before the the variable

func add(_ a: Int, _ b: Int) -> Int {
    return a + b
}

let myAdd = add(12, 13)

1 Answer

Vansh Agarwal
PLUS
Vansh Agarwal
Courses Plus Student 3,965 Points

Yes, they are always omitted when you assign that function to a constant or variable. So if you call the function using the constant or variable name, there will be no argument labels as its type( the function signature ) does not include any argument labels. However if you call the function using the function name directly, there will be argument labels( if you have made labels ).