Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

iOS Swift Closures Functions as First Class Citizens Capturing Variables

dexter foo
dexter foo
8,233 Points

why does the constant work with integers but not the function which the constant is assigned to?

From the video, the function printerFunction is assigned to the constant printAndReturnIntegerFunc i.e. let printAndReturnIntegerFunc = printerFunction() i can get a return from calling the constant, printAndReturnIntegerFunc and assigning it an int. For example, printAndReturnIntegerFunc(3). However, this does not work for the printerFunction(). When i do this, printerFunction(3). i get an error. why is this so? shouldnt it return the same thing as the constant, printAndReturnIntegerFunc since it was assigned to it?

1 Answer

Luis Padron
Luis Padron
1,807 Points

Basically, what is happening when you assign the printerFunction to the constant, that printerFunction is going to be run. When the printerFunction is run, it returns another function called "printInteger"

So, you now have a constant that is equal to a function called printInteger so when you do the constant name and pass it a number it works fine because the function that was assigned to it takes a parameter.

You cant use the printerFunction by itself and pass it a parameter because that function itself does not take a parameter, only its inner function.

// this function returns a function that has an int parameter
// and a void return type.

func printerFunc() -> (Int) -> Void {

    func printInt(number: Int) {
        print("int: \(number)")

    return printInt

// this constant will call the printerFunc() and since that function
// returns a function, someConstant will = printInt, the inner function
// inside of printerFunc

let someConstant = printerFunc()

// we then can use someConstant to call printFunc, and pass it a parameter