Passing by reference VS Passing by value Flashcards

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

What is passing by value?

A

The value created for the subroutine is a copy of the original value.

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

What is passing by reference?

A

A pointer that contains the memory address of the original variable is created

This means that any change made to the variable in the subroutine also affects the original variable.

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

Features of passing by value

A

The copied value has its own separate memory

Seen more in newer programming languages

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

1 positive and 1 negative of passing by value

A

+ The copied variable and the original can be given the same identifier

  • Creates inconsistency between the two variables
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

When would you pass by reference?

A

If the original value needs editing

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

When would you pass by value?

A

If it’s local and merely referenced

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

Features of passing by reference?

A

Uses a pre-existing memory location to store the memory address of the value.

Any edits directly affect the variable

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

1 Positive and 1 negative of passing by reference

A

+ Requires less space than passing by value

  • Entails implicit dependency, which is considered bad practice.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

How is implicit dependency triggered in passing by reference?

A

If you pass a local variable by reference and then assign to it, the result is a direct change to the caller’s variable, not to what its pointing to.

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