I think we’ve gone over the basics of variables in Swift enough. It is time to talk about functions in Swift. Now, we have talked about these to some extent before, and I took it as a given that people would understand a bit. But now I think it is time to actually start diving in and explaining more specifics.
Functions are blocks of code that perform a task. You can then call these functions at different places, which makes fixing bugs much easier than the alternative. If you just copied and pasted the code everywhere, and you found a bug, you would have to change each one to fix that bug. With functions, you just change it one place, and everything that calls it gets the new fixed version of that block of code. The entire app itself is wrapped within a function called “main”, but you don’t normally interact with it, unless there’s a crash. Functions usually take arguments, and very often return data back to where it was called from.
[Read more…]