Well java doesn't allow passing by reference, so I guess the C++ creator and Sun saw something wrong with passing by reference. I created an example in C to show yall that I believe passing by reference is OK only if the functions are privately used by the specific program, but if the function is used a lot, I believe it's best for compatability to pass by value. Also, I find myself trapped with a memory leak when passing by value using a pointer (I move the pointer away from malloc'ed material):
Code:
#include <stdio.h>
#include <stdlib.h>
// Change the string in reference
void changeString1 (char **s);
// Change the string through value & return
char *changeString2 (const char *s);
int main (void)
{
// Create a string you want to change in a function
char *s = malloc (255 * sizeof (*s));
// Change string by reference
changeString1 (&s);
puts (s);
// Change string by value
strcpy (s, changeString2 (s));
puts (s);
return 0;
}
// Change the string in reference
void changeString1 (char **s) // The string that we use to find what we return
{
// Change the string
strcpy ((*s), "Birds sing");
}
// Change the string through value & return
char *changeString2 (const char *s) // The string that we use to find what we return
{
// Create a string to return
char *sc = malloc (255 * sizeof (*sc));
// Use the input value to discover what to set the return string as
if (!strcmp (s, "Birds sing"))
strcpy (sc, "Yes that's correct");
else
strcpy (sc, "Birds sing");
return sc;
}