Lecture 2 - Algorithms to Functions Flashcards

1
Q

What kind of statement is used for a single condition?

A

If statement

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

What statement is used for multiple conditions?

A

If else statement

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

What statements are used in nested conditions?

A

Multiple if else statements

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

What function is used in Multibranch condition?

A

Switch( )

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

What kind of loop is used when iterations are known from outset?

A

For loop

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

What kind of loop is used when iterations are unknown at outset?

A

While loop, repeat loop

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

What is vectorisation?

A

Implicit Iteration
e.g.
bob

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

What is the apply function for?

A

The apply function can be used to apply the same function for example to some large dataset by iterating down a certain column to calculate the same summary statistic

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

Vectorised Conditional Statement

A

Acts upon a vector like data structure conditional tests upon each element

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

What is a function?

A

Compartmentalised and reusable blocks of code

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

What is an argument?

A

Information entering a function.
Good practice to have names.
Can be defined in any order

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

How do you set a default for an argument?

A

Assign a value to the argument when the function is defined.

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

What is the principle of least privilege?

A

By encapsulating in a function user does not make changes to objects outwith the function.

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

What is the DRY principle?

A

Don’t repeat yourself

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

What are the two different methods to make arguments available to functions?

A
  • Pass by value

- Pass by reference

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

What is pass by value?

A

A copy is made of each object being sent into the function.
Memory address of object in the function environment differs from object address in calling environment.
Changes made to the object while inside the function does not change the objects value in the calling environment.

17
Q

What is pass by reference?

A

The address of the object is sent into the function.

Any changes made to the object in the function is reflected in the calling environment

18
Q

Pros and cons of pass by value

A

Pro:
Safer - changes made inside the function are not reflected outside the function.
Con:
Inefficient - copy of information needs to be made to store information in two memory locations.

19
Q

Pros and cons of pass by reference

A

Pro -
More efficient - doesn’t need to make copy of info to store in different locations
Con -
More dangerous

20
Q

Pure functions

A

Do not have side effects.

Always map same input into to same output and otherwise leave calling environment unaltered.

21
Q

3 examples of impure functions

A

library( ) - if a function loads a package, it remains loaded.
setwd( ) - persistent effect after function finishes
par( ) - graphical parameter changes persist.

22
Q

How to make impure functions pure?

A

using the function on.exit( ) will restore previous state when function finishes

23
Q

Are the arguments for standard functions in R pass by value or pass by reference?

A

Pass by value