Welcome to the Treehouse Community
Looking to learn something new?
jon kelson5,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 . ?
Jennifer NordellTreehouse 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
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!
Greg Kaleka39,018 Points
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.
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