For my programming languages class, I am trying to understand how pass-by-name and pass-by-value-result work. I realize these aren't used hardly at all in mainstream languages, but I am wanting to get a feel for how they work. As an example (language agnostic):
void swap(int a, int b){
int t;
t = a;
a = b;
b = t;
}
void main() {
int val = 1, list[5] = {1, 2, 3, 4, 5}
swap(val, list[val]);
}
What would the values of val and list be after swap is called for both pass-by-value-result and pass-by-name.
An explanation would be great too.
From what I deduced, it got value-result: val=2, list={1,1,3,4,5} and name: val=3, list={1,2,1,4,5}. I'm very unsure about those results.
Also does it change the way both of these methods work when an array is passed as opposed to a single int? Thanks for any help in advance.
If you mean pass by name as pass by reference then the result will be the same as by value.
This is because you are only switching references between val and list position val. You are not passing an array reference. Think of a var as a pointer to a block of memory that contains a value.