Functions Flashcards

(16 cards)

1
Q

What are the four parts of a function declaration in Go?

A

func keyword, function’s name, input parameters, return type.

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

How can you simulate optional parameters in Go?

A

Use structs to pass parameters to a function.

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

How do you declare variadic input parameters in Go?

A

Use … before the type in the parameter list.

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

What is the requirement for using variadic parameters in Go?

A

Variadic parameters must be the last parameter in the function.

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

How do you return multiple values in Go?

A

List types in parentheses, separated by commas.

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

How do you handle an error when returning multiple values in Go?

A

Include an error type as one of the return values.

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

What is the purpose of the _ (underscore) in Go when handling returned values?

A

It is used to ignore one or more returned values.

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

What is the function signature in Go?

A

The function signature includes the number of parameters, types of parameters, and the return type.

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

How can you assign a function to a variable in Go?

A

Declare a function variable and assign a function to it using the function’s signature.

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

What is an anonymous function in Go?

A

A function without a name, defined inline.

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

What is a closure in Go?

A

A function defined inside another function, which can access the outer function’s variables.

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

How do you pass a function as a parameter in Go?

A

Pass the function as an argument to another function.

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

How do you return a function from another function in Go?

A

Return a closure from the function.

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

What is the purpose of defer in Go?

A

It delays the execution of a function until the surrounding function exits, useful for resource cleanup.

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

How does Go handle function parameters?

A

Function parameters are passed by value, meaning a copy of the value is used. Maps and slices are exceptions, as they are passed by reference.

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

How does Go handle maps and slices when passed as function parameters?

A

Maps can modify key-value pairs, and slices can modify elements but cannot change their length.