In Swift 5.8 we can specify funcs. With InOut
we have output arguments—changes are retained at the caller. We can return tuples and optionals.
An extension may add funcs to a class
. Even a type like String
or Int
can have methods added with an extension block.
Here we introduce a simple function: computeArea
. It receives two arguments, the values "x" and "y." It returns an Int
.
ComputeArea
returns the product of the two parameters. The expression is computed and the value is returned.computeArea
in a variable we declare with var
. We then use print()
to display it.func computeArea(x: Int, y: Int) -> Int { return x * y } // Call the example func. var result = computeArea(x: 10, y: 3) print(result)30
A parameter can have two names in Swift. The first name specified is optional—this is an external parameter name.
func
. It can make function calls more readable.printMessage
" func
uses an external parameter name. We "print a message about" something.func printMessage(about value: String) {
// The identifier "about" is used for external labels.
// The identifier "value" is used in the func.
print("Your message is about \(value)")
}
// Test our external parameter name.
printMessage(about: "Swift")
printMessage(about: "turtles")
printMessage(about: "cats")Your message is about Swift
Your message is about turtles
Your message is about cats
A parameter can have a default value. We use an assignment expression in the formal parameter list. Here the value parameter is 100 when not specified in the caller.
func printValue(value: Int = 100) {
// Write value to the console.
print("Value is \(value)")
}
// The default parameter value is used.
printValue()
printValue(value: 200)Value is 100
Value is 200
Sometimes we want a method that receives any number of arguments. This is a variadic func
. We use an ellipsis to indicate a variadic method.
for-in
loop to iterate the array.func sumStringLengths(values: String...) -> Int { // Loop over string array and sum String lengths. var sum = 0 for value in values { sum += value.count } return sum } // Call the method with 2 arguments. let result = sumStringLengths(values: "bird", "cat") print(result)7
In Swift, arguments are constant—we cannot reassign an argument in the func
body. This helps prevent mistakes in coding. To fix, consider the inout keyword.
func printExample(argument: Int) {
// This will not compile.
argument = 10
}
printExample(20)main.swift:3:14: Cannot assign to 'let' value 'argument'
This program uses a static
func
. We introduce the Music class
, and provide a static
play()
method. Play()
changes the static
field named "count" when called.
static
field can exist in this program.class Music { static var count = 0 static func play() { print("Music.play called: \(count)") // Increment static var. count += 1 } } // Call static method. Music.play() Music.play()Music.play called: 0 Music.play called: 1
A key part of a modern programming language is its function call syntax. In Swift we find powerful, new features like multiple return values and optional tuples.