In this episode of the Swift introduction series we’ll be looking at functions in Swift.
Swift is an object oriented language that builds upon Objective-C and C++, but it also blends functions and functional programming techniques. It’s a little bit like Scala, except that Swift will become massively successful.
Swift functions can be created and assigned to variables, as they can in
func keyword to introduce
As with other programming languages, variables defined within the block of a function live for the lifetime of that function:
Swift has positional, named and default arguments. These are specified within the parentheses and are comma separated. Unlike variables (which use type inference to determine what the right type is) function arguments must be typed.
Default arguments can be declared by placing a default value afterwards:
This error occurs because default arguments are implicitly named arguments. A named argument is one that must be called with a name instead of its position. Names are specified with the same label as the function name:
Named arguments don’t have to have a default value though. They can be declared as named arguments by placing a label in front of the argument. Named arguments are said to have an ‘‘external name’’, which is the name callers must use to supply the function, and an ‘‘internal name’’, which is used in the implementation of the body itself. This allows functions to be refactored without affecting their API:
As a special short-cut, it is possible to declare that an argument be named with the same name as the argument name using the # symbol.
Note that even though the arguments are named, they have an implied order. It is not possible to miss out the arguments or re-order them, as is possible in other languages with named arguments:
[Side note: Error reporting in Swift is really great. The ASCII graphics at the bottom highlight the erroneous expression (the bit in the parentheses) and the caret shows where the error was found. This is especially useful in a REPL where there isn’t a defined source file or line numbers.]
Named arguments are a throwback to Objective-C, and to allow Swift to interoperate with Objective-C classes (which we’ll look at in a future post). In a few cases they can make an API clearer, but in a lot of cases they add extra noise where none is really required.
Finally, return types and values. As with other languages, the
keyword is used to return from a function and pass a value back. As with
arguments, the return type of the function must be specified (it defaults
to no return type if not) except that instead of using the : syntax, it
invents a new -> syntax instead. This is one of the several annoyances
in the language where consistency goes out of the window because not enough
code has been written to find out these edge cases.
In next week’s episode, we’ll look at how to create classes in Swift. To subscribe to this series, add the Swift tag feed to your reader.