Join John Nastos for an in-depth discussion in this video Writing user-defined functions, part of Learn Swift 2: The Basics.
- View Offline
- [Voiceover] Now that you've seen some examples of calling existing functions it's time to understand how to write your own. A function definition starts with the keyword func which is short for function. That's followed by the function name. I'll call this function sayHello. The name is followed by a set of parentheses and inside that parentheses go the list of parameters, or arguments, that the function takes. I'll come back to this section but leave it as a function taking no arguments for now. After the parentheses, if the function is going to return a value you must specify that here.
It uses an arrow made out of a dash and an angle bracket followed by the type that the function will return. In this case I want to return a string. Finally there are curly brackets that show the scope of the function. And inside my function I'll return the value Hello. To call this function, I just have to call it by name with parentheses, just as I did with print and the other examples earlier. Let's go back and add a parameter to the function.
I want my function to print out a statement saying hello to a person. So I'm going to want to pass in the person's name to say hello to. Inside the parentheses I'll have a variable called name that will store the argument passed in. And in this case it's a string. Then in my return value I'll include that variable in the string. Then later on when I call the function I now have to pass in an argument.
So I'll pass in the name Bob. Let's say I wanted to have a custom greeting instead of just the word Hello. I'll write a new function called customGreeting. This function will start out with the same parameter that I had before, name, which is a string. But this time I'll have a second parameter called greeting and it'll also be a string. Then I'll return a string type and use curly brackets to define the scope of the function.
Inside the function I'll return greeting and then name. Now when I call this function I'll have to use both parameters. First one, Bob, and then the second one is a named parameter. greeting: and I'll use Hi. There's an interesting caveat here. In the last video I talked about using a named parameter in a variation of the print function that allowed me to specify a separator.
When writing your own functions the default behavior of Swift is to make the first argument unnamed, like in this case the first parameter is name and notice that I'm not saying name: when I call the function. And then all the subsequent parameters are usually named. So in this case my second parameter is preceded by greeting:. If you wanted to write a function and override that behavior and not have a parameter be named by default you can include an underscore before the variable name.
For example, I could write another function called sayHelloToFullName and pass two parameters. The first called firstName, which is a string. And then the second I'll precede with an underscore telling it not to use a named parameter but I still need a variable name for it inside the function. I'll call it lastName. And it'll return a string. Then I can say Hello firstName lastName and when I call that function you notice that I don't actually have to use the name for the second parameter because of that underscore that I included.
First, learn how to install Xcode, the IDE designed for Apple developers, and use Swift playgrounds, an innovative coding environment that displays results as you code. Author John Nastos then dives deep into Swift syntax: the variables and constants, data types, arrays, loops, classes, and functions that make your apps run. Next, discover how to build an engaging app interface with storyboards and Interface Builder, connect the interface and code, and use table views to present lists. John shows how to store and retrieve data, and interact with Objective-C, the original app development language that still integrates beautifully with Swift. Using the exercise files provided with the course, you'll assemble a complete working app with Swift while gaining the skills to go create the next one on your own.
- Downloading and installing Xcode
- Using Swift playgrounds
- Understanding Swift syntax
- Debugging Swift code
- Controlling flow with expressions, conditional statements, and loops
- Using and writing Swift functions
- Creating classes, structs, and enums
- Using storyboards in Interface Builder
- Building an app with table views
- Storing and retrieving data
- Interacting with Objective-C