I am currently learning D, and see that it allows one to define pointers in much the same way as in C++, however, it also allows pass by reference via the ref keyword. With regards to function parameters, I have the following questions:
Is it possible to define function paremeters in D entirely without using pointers? If so, what are the best practices for doing so, in general? My understanding is that the purpose of the ref keyword is to do away with having to define function input parameters as pointers.
Are there any cases where using pointers rather than the "ref" keyword in functions is significantly more advantageous even if the answer to question 1 is "yes"?
For example, the following two codes produce identical results. Is one better than the other, besides it being more convenient to use the "ref" keyword?
Code 1 (Uses pointers):
import std.stdio;
struct X {
int s;
int y;
}
pure void my_func(X *x) {
x.s = 10;
x.y = 90;
}
void main() {
X cat = X(1,2);
X *cat_ptr = &cat;
writeln(cat.y);
my_func(cat_ptr);
writeln(cat.y);
}
Code 2 (Uses "ref" keyword):
import std.stdio;
struct X {
int s;
int y;
}
pure void my_func(ref X x) {
x.s = 10;
x.y = 90;
}
void main() {
X cat = X(1,2);
writeln(cat.y);
my_func(cat);
writeln(cat.y);
}
I mostly come from a Java background, so my understanding of these concepts is not great.