What is a function?
A sequence of instructions that performs a specific task.
Write a function to get the area of a room with a given length and width.
func area(width: Int, length: Int) -> Int {
return width * length
}When you write a function such as func area(width: Int, length: Int), what is the name of the stuff inside the parentheses?
Parameters
Is there a limit to the number of parameters?
No
When you provide a value for parameters such as area(width: 10, length: 12), what are the values called?
Arguments
If you have a function such as func area(width: Int, length: Int) -> Int, what is the value after the arrow called?
Return type
What’s the return type of a function that doesn’t explicitly declare a return type?
Void
TF: A function contains a random assortment of code that executes a variety of tasks
False. One specific task.
What keyword do we use to output a value from a function
RETURN
What keyword do we use to create a function?
Func
What is wrong with the function declaration below?
func someFunction(Int, Int) -> Int {
return 1
}Function parameters require names
Why will this code not compile?
func someFunction(a: Int) -> Bool {
return a
}The value we are returning does not match the return type.
For the following function, what are havingValue and value?
func remove(havingValue value: String) {
print(value)
}havingValue is external name, value is local name
Will external name or local name appear when you call the remove function?
External name which is havingValue
remove(havingValue: “A”)
If we set up the function as below, what happens? func carpetCost(havingArea area: Int, carpetColor color: String = "Tan") -> Int
When you try to call the carpetCost function, you get 2 options, one with the default, and one where you can change the color
If we want to set up a function but don’t want parameters, how can we avoid them?
Use _ func coordinates(_ location: String)