Introduction
Functions help you organize your code into small, reusable pieces that do one job.
func functionName(parameters) -> ReturnType { // code to run }
().func greet() { print("Hello!") }
func add(a: Int, b: Int) -> Int { return a + b }
func sayHello(to name: String) { print("Hello, \(name)!") }
func multiply(x: Int, y: Int) -> Int { return x * y } let result = multiply(x: 4, y: 5) print("4 times 5 is \(result)")
-> followed by the return type.return to send back a value from the function.func.func, the correct keyword for Swift functions.greet that takes no parameters and returns nothing?func name() {}.func add(a: Int, b: Int) -> Int {
return a + b
}
print(add(a: 3, b: 4))add takes two integers and returns their sum.add(a: 3, b: 4) returns 3 + 4 = 7, which is printed.func multiply(a: Int, b Int) -> Int {
return a * b
}b Int is missing a colon.describePerson that takes a String parameter name and an Int parameter age, and returns a String describing the person like "Name is Age years old." Which is the correct function declaration?Pass the test and this pattern moves unknown