I think passing by reference doesnt make much sense here.
One reason to pass by reference would be to use the parameter as a return value (so modify the input), but then a default value doesn't make much sense.
The other reason to pass by reference might be some performance improvement, to not copy the whole data. This isn't critical für cv::Mat since it's only a header and the data field is copied by reference/pointer anyways. If you have to call that function very often, then you might have a performance improvement by not copying the header, but in this case you don't want to have a default value which creates a new cv::Mat all the time.
So my solution to your problem would be to pass by value:
cv::Mat function(cv::Mat matrix = cv::Mat::eye(2,3, CV_32F))
{
return matrix;
}
int main()
{
std::cout << function() << "\n\n" ;
std::cout << function(cv::Mat::zeros(2,3, CV_32F)) << std::endl;
return 0;
}
gives me this terminal output as wanted/expected:
[1, 0, 0;
0, 1, 0]
[0, 0, 0;
0, 0, 0]