Passing function by reference or by value?

c++, stl

Solution

Typically, pass-by-reference and pass-by-value only refer to passing variables, not functions. (Template functions introduce some complications, but that is for another discussion.) Here you are passing `myfn` as a pointer to a function. There is no difference if you use `&myfn` instead. The compiler will implicitly convert `myfn` to a pointer to the function with that name.

Problem

``` #include <iostream> #include <algorithm> using namespace std; bool myfn(int i, int j) { return i<j; } int main () { int myints[] = {3,7,2,5,6,4,9}; // using function myfn as comp: cout << "The smallest element is " << *min_element(myints,myints+7,myfn) << endl; cout << "The largest element is " << *max_element(myints,myints+7,myfn) << endl; return 0; } ``` considering the above code , is there any difference if we pass`myfn` or `&myfn` to `min_element`? when we tried to pass a functor which one would be a more standard way?

Original source

Related problems