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 trialdexter foo
8,233 Pointswhy 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
1,807 PointsBasically, 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
someConstant(2)