Michael Kimin Choo
Michael Kimin Choo

Reputation: 165

Pass by value result

I'm self teaching myself some parameter passing implementation models and in my programming languages book it asks me to write a program to produce different behavior depending on whether pass by reference or pass by value result is used in its parameter passing. What are some leading questions that would help me understand and get to this answer?

I know pass by reference passes the location of the variable and is modified directly by the function while pass by value result copies the value in then copies it back out. I just can't think of a situation where the result would be different (maybe I'm misunderstanding pass by value result?).

Upvotes: 0

Views: 538

Answers (1)

Sid
Sid

Reputation: 29

// Correct implementation of a function addToMyself() as the name suggests
void addToMyself(int &a, int b) {
    a += b;
}

// Incorrect implementation
void addToMyself(int a, int b) {
    a += b;
}

// Tweaked implentation with pass by value
int addTwo(int a, int b) {
    return a+b;
}
// and use 
a = addTwo(a, b)

Upvotes: 1

Related Questions