Functions Flashcards

1
Q

What is a function?

A

A sequence of instructions that performs a specific task.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

Write a function to get the area of a room with a given length and width.

A
func area(width: Int, length: Int) -> Int {
    return width * length
}
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

When you write a function such as func area(width: Int, length: Int), what is the name of the stuff inside the parentheses?

A

Parameters

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

Is there a limit to the number of parameters?

A

No

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

When you provide a value for parameters such as area(width: 10, length: 12), what are the values called?

A

Arguments

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

If you have a function such as func area(width: Int, length: Int) -> Int, what is the value after the arrow called?

A

Return type

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

What’s the return type of a function that doesn’t explicitly declare a return type?

A

Void

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

TF: A function contains a random assortment of code that executes a variety of tasks

A

False. One specific task.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

What keyword do we use to output a value from a function

A

RETURN

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

What keyword do we use to create a function?

A

Func

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q
What is wrong with the function declaration below?
func someFunction(Int, Int) -> Int {
    		return 1
	}
A

Function parameters require names

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q
Why will this code not compile?
func someFunction(a: Int) -> Bool {
    	return a
	}
A

The value we are returning does not match the return type.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q
For the following function, what are havingValue and value? 
func remove(havingValue value: String) {
print(value)
}
A

havingValue is external name, value is local name

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

Will external name or local name appear when you call the remove function?

A

External name which is havingValue

remove(havingValue: “A”)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q
If we set up the function as below, what happens? 
func carpetCost(havingArea area: Int, carpetColor color: String = "Tan") -> Int
A

When you try to call the carpetCost function, you get 2 options, one with the default, and one where you can change the color

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

If we want to set up a function but don’t want parameters, how can we avoid them?

A
Use _ 
func coordinates(_ location: String)