Search icon CANCEL
Subscription
0
Cart icon
Your Cart (0 item)
Close icon
You have no products in your basket yet
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Conferences
Free Learning
Arrow right icon
Arrow up icon
GO TO TOP
Learning Swift

You're reading from   Learning Swift Build a solid foundation in Swift to develop smart and robust iOS and OS X applications

Arrow left icon
Product type Paperback
Published in Jun 2015
Publisher Packt
ISBN-13 9781784392505
Length 266 pages
Edition 1st Edition
Languages
Arrow right icon
Author (1):
Arrow left icon
Andrew J Wagner Andrew J Wagner
Author Profile Icon Andrew J Wagner
Andrew J Wagner
Arrow right icon
View More author details
Toc

Table of Contents (13) Chapters Close

Preface 1. Introducing Swift 2. Building Blocks – Variables, Collections, and Flow Control FREE CHAPTER 3. One Piece at a Time – Types, Scopes, and Projects 4. To Be or Not to Be – Optionals 5. A Modern Paradigm – Closures and Functional Programming 6. Make Swift Work for You – Protocols and Generics 7. Everything is Connected – Memory Management 8. Writing Code the Swift Way – Design Patterns and Techniques 9. Harnessing the Past – Understanding and Translating Objective-C 10. A Whole New World – Developing an App 11. What's Next? Resources, Advice, and Next Steps Index

Functions

All of the code we have explored so far is very linear down the file. Each line is processed one at a time and then, the program moves onto the next. This is one of the great things about programming: everything the program does can be predicted by mentally stepping through the program yourself, one line at a time.

However, as your program gets larger, you will notice that there are many places that reuse very similar or identical code that you cannot reuse using loops. Also, the more code you write, the harder it will become to reason in your head about what it is doing. Code comments can help with that, but there is an even better solution to both of these problems and they're called functions. A function is essentially a named collection of code that can be executed and reused by name.

There are various types of functions but each builds on the previous type.

Basic functions

The most basic type of function simply has a name with some static code to be executed later. Let&apos...

You have been reading a chapter from
Learning Swift
Published in: Jun 2015
Publisher: Packt
ISBN-13: 9781784392505
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $19.99/month. Cancel anytime
Banner background image