Programming in Swift: Fundamentals

Oct 19 2021 · Swift 5.5, iOS 15, Xcode 13

Part 5: Functions & Named Types

37. Challenge: Functions

Episode complete

Play next episode

Next
About this episode

Leave a rating/review

See forum comments
Cinema mode Mark complete Download course materials
Previous episode: 36. Functions & Return Next episode: 38. Structures

Get immediate access to this and 4,000+ other videos and books.

Take your career further with a Kodeco Personal Plan. With unlimited access to over 40+ books and 4,000+ professional videos in a single subscription, it's simply the best investment you can make in your development career.

Learn more Already a subscriber? Sign in.

Notes: 37. Challenge: Functions

Update Notes: The student materials have been reviewed and are updated as of October 2021.

Heads up... You've reached locked video content where the transcript will be shown as obfuscated text.

All right, it's time for your next challenge. Now you can find the challenge in the 04 Challenge Functions page of the playground you've been using. Or you can download a new one from the resources for this video. Open it up and try to solve the challenge questions on your own, but then keep watching and you can compare your work to my work. Good luck. (playful music) All right, on to Challenge 1. So, first. Write a function named printFullName that takes two strings called firstName and lastName. Okay, I'll start with the func keyword. I'll name the function printFullName. And I'll add two string parameters inside the parentheses. So then in the body of the function, I'll print out those two parameters with a space in between. And so now I can use that function to print out my own name. There I am, Chris Belanger. So to finish up Challenge 1, I need to remove the argument labels from a function call. And all it requires is an underscore before each parameter name to say that I don't want any argument labels for this function. So then the function call needs to be changed to match. And there we go. Now, Challenge 2. And this one is similar to the first. So I still need to assemble a full name from a first and last name. So I'm just gonna go and copy and paste this function. There we go. Now I can't have two functions named the same in the same playground. Plus, this version should return the full name. So I'll rename this function. I'll call it calculateFullName. Okay, now to create the return value, I need to add the return type, String, to the function declaration. And then I need to add a return statement to the body to return the string instead of printing it. Now, the last thing to do is store my name in a new constant. And I can check out my full name in a sidebar.