Chapter 56 - Subroutines and recursion Flashcards

1
Q

What is a subroutine?

A

» A name block of code which performs a specific task within a program

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

What are the 2 types of subroutines?

A

» Functions
» Procedures

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

What is the difference between a function and a procedure?

A

» The function returns a value but the procedure doesn’t
» if there are values already in the subroutine, you use a procedure

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

How can you call the subroutine from the main program and output it to the user?

A

» Simply write the subroutine name

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

What does the first line of a subroutine specify?

A

» The interface for that subroutine

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

What is modularity?

A

» Concept of breaking a large program or problem down into smaller chuncks

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

What is the benefit of modularity?

A

» Each module carries out a single specific task

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

What does a procedure do?

A

» Performs a set task and can take in a zero, 1 or 2 inputs

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

What happens when a parameter is passed by a value?

A

» The value created for the subroutine being called is a copy of the original
» Held in a separate memory location once passed
» It is now a local variable

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

What happens if you change the parameter inside the subroutine when you pass a value?

A

» It will not affect its value outside the subroutine

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

What does it mean if a parameter is passed by a reference?

A

» The address of the parameter is passed to the subroutine
» A pointer that contains the memory address of the original variable is created

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

What happens if you change the value in the parameter when passing by a reference?

A

» A change to this value within the called subroutine will also affect the value outside the subroutine
» It becomes a global variable

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

What does it mean if you say a scope of a variable?

A

» Refers to where a variable is visible or accessible from inside the main program

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

What is a global variable?

A

» A variable which is defined in the main program, outside any functions
» Has a global scope

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

What are some disadvantages of using a global variable?

A

» Needs to be preserved in memory all the time, this means memory space is kept throughout the running of the program
» Makes it less memory efficient

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

Why do we avoid using global variables?

A

» Increases complexity of the program - as they can be used by many difference parts of the program
» Make them harder to keep track of its value

» If you want to re-use code, you have to track and remember the value of the global variable

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

Why do we used global variables?

A

» To use a variable which can be accessed from any parts of a program
» When a value needs to stay the same throughout the program

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

What is a local variable?

A

» A variable which is defined in the procedure or function, and can be used only inside that function

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

What are the advantages of using a local variable?

A

» Local variables in difference subroutines can have the same identifier
» Changing the value of a local variable in a subroutine, will not affect its value outside the subroutine

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

What are the disadvantages of using a local variable?

A

» Can overwrite global variables with the same name
» Only exists during the execution of the program

21
Q

What the principle of encapsulation?

A

» The subroutine written according to this principle can be tested independently, and used in many different programs without the programmer needing to know what variable it uses

22
Q

What are the benefits of programming with subroutines?

A

» Relatively easy to understand and debug
» Can be tested independently
» It can be re-used
» Can use a modular approach

23
Q

When is a subroutine recursive?

A

» If its defined in terms of tiself

24
Q

What is recursion?

A

» When a subroutine calls itself from within its own subroutine

25
Q

What is sequence?

A

» Executing the instructions one after the other

26
Q

What is selection?

A

» A programming construct that allows a program to change direction depending on the outcome of a condition

27
Q

What is iteration?

A

» Programming construct which enables us to repeat sections of code

28
Q

What are 2 types of loops?

A

» Count controlled loops
» Condition controlled loop

29
Q

What are the 3 characteristics a recursive subroutine should have?

A

» Contain a stopping condition
» For any input value other than the stopping condition, the subroutine should call itself
» Stopping condition should be reachable within a finite number of times

30
Q

What are the problems if you don’t have the right characteristics of a recursive subroutine?

A

» May result in a stack overflow

31
Q

What is a programming paradigm?

A

» A style of coding

32
Q

How can we identify a global variable?

A

» Defined at the start of a program
» Exits throughout the prgoram
» Allows data to be shared throughout modules

33
Q

Why use local variables?

A

» Prevents variables being accidentally being overwritten

34
Q

Which variable is more memory efficient?

A

» Local

35
Q

Which variable is used when a value doesn’t tend to change throughout the run time of a program?

A

» Global

36
Q

Which variable is accessible throughout a program?

A

» Global

37
Q

Which variable increases complexity?

A

» Global

38
Q

Which variable allows data to be lost once a subroutine is no longer being used?

A

» Local

39
Q

Which variable is retained in memory permanently?

A

» Global

40
Q

What is the Recursive call?

A

» An instance of a subroutine calling itself

41
Q

What is a base case?

A

» A condition in a recursive ubroutine that when true, returns control to the subroutine that called it rather than running antoher recursive instance

42
Q

What are some advantages of recursion?

A

» Some problems are easier to think of recursively
» Recursive solutions can be more elegant

43
Q

What are some advantages of recursion?

A

» Some problems are easier to think of recursively
» Recursive solutions can be more elegant

44
Q

What are some disadvantages of recursion to iteration?

A

» It requires more memory in order to maintain the stack of calls
» If the space on the stack runs out, the program will run out
» Overheads of maintaing a stack means it will perform more slowly then iterative equivalent

45
Q

What are all parameter’s passed by in python?

A

» By value

46
Q

What is the difference between parameters and arguements?

A

» Parmaters are the variables whose values are taken in by a subroutine and the arguements are the values passed to them

47
Q

Describe the use of local variables

A

» Local scope - only affect the variable inside the subroutine - reduces complexity of code when co sharing
» Store identifer with the same name
» Less memory used then global

48
Q

State two features of global variables that distinguish them from local variables

A

» Can be accessed at any point in a program and are defined at the start of the program
» Continously held in memory, and the variable data does not get lose when the subroutine ends