Pointers are memory addresses. They have difference uses. One of their use is to pass data objects to functions by memory address, instead of creating another copy of the object in memory. It can not only save memory, but also provide some speed advantage. In the following example, I shall create a vector of 100 million observation, and show the speed advantage of pointers over passing the vector by name to a function.
mata
mata clear
timer_clear()
a = range(0, 100000000, .25)
b = &a
timer_on(1)
real scalar findmean(a) {
return(mean(a))
}
findmean(a)
timer_off(1)
timer_on(2)
real scalar findmeanp(pointer scalar b) {
return(mean(*b))
}
findmeanp(b)
timer_off(2)
timer()
--------------------------------------------------------------
timer report
1. 11 / 1 = 10.982
2. 6.35 / 1 = 6.348
---------------------------------------------------------------
end
The output of -help m2_declarations##remarks8- tells us
“Arguments are passed to functions by address, not by value. If you change the value of an argument, you will change the caller’s argument.”
So it seems to me that arguments are in fact passed as pointers. On the other hand, your example demonstrates greater speed with the pointer argument. I’m at a loss to explain it.