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
Mikkel Storch
1,955 PointsCall the function
Hello I never quite understood the meaning of "call the function" for example "Call the greeting function and pass it the String "Jerry"". What is it that they mean by "call"? Thanks
2 Answers
Dani G
Courses Plus Student 229 PointsThis is easy. You create a simple function, like the function below. (It just takes 1 parameter - "name" which is of type String and it doesn't really return anything)
func enterName (name: String) { println("My name is (name)") }
When you/or anyone say "Call a function" it simple means to execute it. By default, when you create a function, it doesn't execute itself. In order to do so, you should CALL it like this:
enterName("Jerry")
This function that I wrote, takes a single parameter : name (which is of type String). And when you hear someone saying "Pass it the string 'Jerry'" it simply means that the parameter is of Type String and expect to enter a String value (or a text put in quotes to say it a simple language).
When you call the function and enter the String parameter you will see "My name is Jerry" (if you try this in the playground).
So, basically, you enter the name of the function along with the required parameter (if you have entered any) and this is what a calling a function is. I hope my explanations isn't confusing you more.
marcosorno
1,129 PointsI am having issues with this exact same part of the Swift Function and Optionals. I tried entering in this code sample and it gave me an error. I ended up doing a println and it passed me to the next level but this can be correct. I called a println and not a function. The code looked like this. Am I wrong?
func greeting(#person : String) { println("Hello Jerry") }
greeting(person: "Jerry")
Mikkel Storch
1,955 PointsYes, that should be correct!
Mikkel Storch
1,955 PointsMikkel Storch
1,955 PointsThanks a lot Dani, it was very helpful!