S11 - 56 subroutines and recursion Flashcards

You may prefer our related Brainscape-certified flashcards:
1
Q

what is recursion?

A

Recursion is when a subroutine (often a function) calls itself from within its own subroutine

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

what three characteristics should a recursive subroutine have?

A

+ A stopping condition

+ the stopping condition should be reachable in a finite number of times

+ for any input value other than the stopping condition the subroutine should call itself (recursion)

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

what happens if a recursive subroutine calls itself too many times?

A

A recursive subroutine may call itself indefinitely and cause a stack overflow

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

what is a subroutine?

A

A subroutine is a named 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
5
Q

what are the two common types of subroutines?

A

functions and procedures

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

how is a subroutine called?

A

A subroutine is called by writing its name in a program statement.

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

some functions return a result and…

A

some do not

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

A procedure is called by…

A

writing its name but not assigning the result to a variable

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

describe what happens if a parameter is passed by value

A

If a parameter is passed by value its actual value is passed to the subroutine

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

describe what happens is a parameter is passed by reference

A

if a parameter is passed by reference the address (not the value) is passed to the subroutine

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

whats the difference between a parameter being passed a reference and a value

A

unlike value when a parameter is passed by reference the address is passed to the subroutine not the value.

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

what are global variables?

A

variables used in the main program by default are global variables

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

state an advantage of a global variable

A

Global variables can be used anywhere in the program, including within subroutines

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

how long do local variables last?

A

Local variables only exist during the execution of a subroutine.

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

state one disadvantage of local variables

A

Local variables cannot be accessed outside the subroutine

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

changing the local variables values has…

A

no effect on any variable outside the subroutine, even if they have the same name

16
Q

state one advantage of a local variable

A

It ensures that each subroutine is completely self-contained and independent of any global variables