132

While reading the documentation for boost::test, I came across the term "free function". What I understand is that a free function is any function that doesn't return anything (Its return type is void). But after reading further it seems that free functions also don't take any arguments. But I am not sure. These all are my assumptions. So could anybody define free function?

1 Answer 1

166

The term free function in C++ simply refers to non-member functions. Every function that is not a member function is a free function.

struct X {
    void f() {}               // not a free function
};
void g() {}                   // free function
int h(int, int) { return 1; } // also a free function
2
  • let's say we have our main function in a different file and inside it we need to call a free function , so what should I do to have free functions in some other file that I will include it later in my main file ?? I mean should I make a hpp file where my free function are implemented there ( as static inline functions maybe ) ?
    – Joy
    May 4, 2012 at 8:34
  • 5
    Usually you would declare them in a header file and implement them in a separate source file (with some exceptions like template functions). It would be better to open a new question on that specific topic though so people can answer you with more detail etc. May 4, 2012 at 13:23

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.