arc4random()
generates a random number
func syntax
func nameOfFunction() {
// body of function goes here
}function type
func sayHelloToStudent(student: String) {
print("Hello \(student)")
}multiple func paramters
greetStudent(student: String, school: String)
multiple func paramters example
func averageScore(firstScore: Double, secondScore: Double, thirdScore: Double) {
let totalScore = firstScore + secondScore + thirdScore
print(totalScore / 3)
}To return a value from a function
func calculateTip(priceOfMeal: Double) -> Double {
return priceOfMeal * 0.15
}
func isPastBedtime(hour: Int) -> Bool {
if hour > 9 {
return true
} else {
return false
}
}boolean
var name=True print(name)
if true do something
if true{print(“hi”}
if statement needs
boolean to check
else only executes
if condition false
else if
to add extra conditions
else statement always
last statement
user input
readline()
var password=
readline()
while code
while true{print(“hi”}
how to specify return value
->Int{}
return add
func add()->Int{
var number=1+1
return number
}end of year bonus
func endOfYearBonus(basePay: Double, bonus: Double,percentBonus: Double)->(Double){
let percentBonus=0.10
return basePay + bonus + (basePay * percentBonus)
}Internal and External Parameter Names
func addValues(value1 x: Int, value2 y: Int) -> Int {
// internally, use `x` and `y`
return x + y
}
// externally, use `value1` and `value2` addValues(value1: 5, value2: 10)
Omitting External Parameter Names example
func combineStrings(_ s1: String, _ s2: String) -> String {
return s1 + s2
}combineStrings(“We love”, “ Swift!”)