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 Swift Closures Functions as First Class Citizens Functions as Parameters

Chris Stromberg
PLUS
Chris Stromberg
Courses Plus Student 13,389 Points

How does the function displayString pass the parameter "I'm a function inside another function" into printString func?

I understand how the function printString works taking a string as a parameter and returning nothing.

I'm a little confused on how the func displayString takes a parameter named printStringFunc and is able to pass the string "I'm a function inside another function" as a parameter in the displayString func. I can't follow the logic.

3 Answers

Joseph Rimar
Joseph Rimar
17,101 Points

Hey Chris,

I'm not very familiar with Swift but passing functions as parameters is a concept heavily used in JavaScript. When you pass a function as a parameter you are telling the new function to execute the passed function inside of its arguments.

In the example from the video the printString function is called inside the displayString function. To explain this a little better here is the same example written in JavaScript. The language is similar enough without having to pass in types and return values and the concept is exactly the same:

function printString(string) {
    document.write("Here is my string: " + string);
}

function displayNewString(printStringFunc) {
    printStringFunc("I am a function inside another function.");
}

displayNewString(printString);
//Here is my string: I am a function inside another function.

Like in the video the printString function takes a string as a parameter. It then writes (or prints) the string we typed in ("Here is my string: "). It also writes the string we pass in as the parameter.

Next the displayNewString function takes a function as its parameter, in this case it's named "printStringFunc". The important and tricky thing (for me at least) to take here is that the parameter is simply a representation of the function to be called and the name you use when defining it is irrelevant. For instance we could use the following code to produce the exact same thing:

function printString(string) {
    document.write("Here is my string: " + string);
}

function displayNewString(callback) {
    callback("I am a function inside another function.");
}

displayNewString(printString)

When we pass "callback" as a parameter we are only passing the called functions definition, we do not execute it until later on inside of the new functions arguments.

Now we call the displayNewString function which executes the arguments of the function that is passed to it. In this example it calls the printString function which prints "Here is my string" and then adds (or concatenates) "I am a function inside another function" to the end of the first string.

This example is really just a fancy way of joining two strings together but the concept of passing functions a parameters to other functions is a core part of functional programing. In JavaScript this technique seems to be reserved for more "advanced" tutorials and it really is quite tricky to get the hang of. I highly recommend reading the article on callback/higher order functions here: http://javascriptissexy.com/understand-javascript-callback-functions-and-use-them/ It is written for JavaScript but again the syntax is very similar and the concept of passing functions as parameters is the same.

Good luck with your coding and I hope this was of at least some help.

Chris Stromberg
Chris Stromberg
Courses Plus Student 13,389 Points

Joseph,

Thank you for your explanation, it did help.

I understand that you can pass functions as parameters, however my confusion mainly lies with the following two lines of code.

printStringFunc("I am a function inside another function.")

callback("I am a function inside another function.")

If I understand you correctly, when we call the function func displayNewString(callback) { callback("I am a function inside another function."); } This function is simply creating a new parameter called callback, which is passed into the function that was passed into the printStringFunc as a parameter?!

Thanks for your help. Chris

Joseph Rimar
Joseph Rimar
17,101 Points

Chris,

In a way yes. When we define the displayString function as:

function displayNewString(callback) {
callback("I am a function inside another function");
} 

and then call it with displayNewString(printString) the interpreter effectively defines our displayNewString function for this instance as:

function displayNewString(printString) {
printString("I am a function inside another function");
}

The line "I am a function inside another function" becomes the string parameter we defined in the printString function.

Executing the printString function with:

printSting("I am a function inside another function");

actually produces the same result.

Anthony Attard
Anthony Attard
43,915 Points

Thanks! I was very confused, now I get it!

I still don't get it at all.

Can somebody write a code that doesn't contain 10 different versions of the word String or so, it is confusing when trying to understand.

james bunn
james bunn
2,377 Points

We are trying to understand how the string "I am a function inside another function" is passed into the "aString" parameter of the printString function. To answer, It may be helpful to understand the sequence of events that occur when calling displayString(printString). For simplicity purposes, I'll use the word text and string interchangeably.

  1. Execute displayString - All this function does is pass text to a function of type (String) -> Void. Thus, pass the text "I am a function inside another function" to the function "printString".

Like Chris mentioned above, you can TOTaLLY IGNore the functions internal parameter name "PrintStringFunc". JEEZ, how many times can we mention "Print" String" and "Func" before it gets confusing. For sake of learning, call the internal parameter name "x". Again, Just focus on the function type that is required by displayString (String) -> Void

2 Execute (printString) - I don't think anyone has any issue with this. It prints "Printing the String passed in". However, how do we connect the "aString" parameter to the text "I am a function inside another function"?

In the earlier examples in the video, we explicitly passed the text to the aString parameter.

Example printString("Hi, my name is Pasan). Pretty easy.

In this case, displayString has the text. And, its sole job is to pass the text to a function. By writing displayString(printString), we tell displayString to pass the text into printString; it listens.

PrintString has an internal parameter name "aString" which is assigned to whatever text is provided. "aString" can be anything. You can call it yourMammasBooty for all SWIFT cares. Again, for sake of learning, call it "y". This is just an internal way the function can reference the text in the body of the function. And printString does reference the text it is provided by using "aString" in the print statement.

In summary, the steps are

  1. Run displayString function - It passes text to a printString
  2. Run printString function - It accepts the text it was provided and executes.

Hope this helps.