Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

iOS

jon kelson
jon kelson
5,149 Points

Statement and function

Hi in swift what's the difference between a statement such as a switch and a function . Is the switch a type of function . ?

2 Answers

Jennifer Nordell
seal-mask
STAFF
.a{fill-rule:evenodd;}techdegree
Jennifer Nordell
Treehouse Teacher

No, switch is a statement. A function is a piece of code that runs a series of operations (including statements). Let's put it this way.

Let's say that every work day you eat lunch at the same restaurant. You know how to get there, but there are steps you take. You get in your car, drive over, park the car, walk in, order your food, sit down and eat, pay your bill, get back in the car and drive back to work. But you don't want to rewrite that code every day. So instead you write a function that does all that for you every time you call it :smile:

Switch, if / else are statements. So let's say you call your goToLunch function. But today is Friday! It's BBQ burger day. In your function you could have a statement that says if it's friday order the BBQ burger special.

Hope that makes sense! :smile:

Greg Kaleka
Greg Kaleka
39,018 Points

Hi Jon,

switch is not a function, but a statement. You can read the Swift Language Guide section on Statements to get a full explanation of what these are.

More specifically, switch statements are "control flow" statements, like if statements.

Under the hood, these statements are probably implemented using functional programming, but I haven't really dug into the source code all that much (remember, it's open source, so you can totally do that!). You don't really need to concern yourself with that low-level of detail, though :smile: