The function print, min, max, and abs are a few examples of global functions. Check out a complete list of Swift’s global functions.
print("I'm a global function!")
var initialPrice = 50
var bestOffer = 45
var finalPrice = min(bestOffer, initialPrice)
Anatomy of a function
func functionName (_ parameterName: parameterType) -> returnType {
statements to execute
return object
}
func sumOfStrings(_ aBunchOfStrings: [String]) -> Int {
let array = aBunchOfStrings
var sum = 0
for string in array {
if Int(string) != nil {
let int intToAdd = Int(string)!
sum += intToAdd
}
}
return sum
}
func reverse(_ string: String) -> String {
var reversedString = ""
for character in string.characters {
reversedString = "\(character)" + reversedString
}
return reversedString
}
func warmUp(temperature: Int) -> Int {
return temperature + 10
}
func firstChar(word: String) -> Character {
return word[word.startIndex]
}
func concatenate(firstString: String, secondString secondString){
return firstString + secondString
}
func warmUp(temperature: Int) -> Int {
return temperature + 10
}
func firstCharacter(word: String) -> Character {
return word[word.startIndex]
}
func concatenate(firstString: String, secondString: String)-> String {
return firstString + secondString
}