func (function)
Try it yourself →Defines your own command.
Syntax
func name(_ p: Type) -> ReturnType { return ... }Description
Defined with func; -> specifies the return type. The _ before a parameter omits its label. return gives back the result.
Example
func square(_ x: Int) -> Int {
return x * x
}
print(square(5))Expected output
25
When to use?
Preventing code repetition, breaking the program into parts.