From the course: Programming for Non-programmers: iOS 17 and Swift 5

Unlock this course with a free trial

Join today to access over 24,800 courses taught by industry experts.

Using methods

Using methods

- [Instructor] Now let's get some practice writing methods in a playground. If you didn't see how to create a playground before, you can do it from file, new and then playground. So I've hit the run button here and there's the output of my greeting. And let's create a method. Go down a few lines, and we'll create a method with the keyword func, and then we'll call this get new greeting. So we're using camel case there, so capital N and capital G, and then some parentheses and the data type it's going to return, so hyphen, then a greater than sign, it's going to be string, so capital S on that, and then put some curly braces. In here, we're going to return some kind of a string. Remember, a string should be in a set of double quotes. Now remember, we need to call the method at some point. So we've defined the definition and now we need to execute it. I'm going to execute it by setting the value of greeting equal to get new greeting. And I'll just use the code hinting there by pressing…

Contents