Pascal – What is the most efficient way to pass an array as a parameter?
This is mostly for dynamic arrays though.
I have known of three ways to pass an array as a parameter by far:
As a value parameter: this results in the entire array being copied and is considered inefficient by many books (except for this, which creates confusion among me).
As a VAR parameter: this results in only the address of the first element being copied, and is considered optimal by many books, with the drawback of possibility of accidentally changing the array content when you don’t need to do so.
As a pointer: this is something I don’t usually see. What is the difference between this and the second method? As for another (possibly) relevant stuff, the wiki says that the dynamic array is itself a pointer with automatic dereferencing, so when passing one to a function, shouldn’t it be passed as a pointer anyway?