C++ - Difference Between Functors and Functions Last Updated : 17 Jan, 2023 Comments Improve Suggest changes Like Article Like Report In C++, we have multiple options to operate over the data like in the case where we can use functions and functors. Although both seem to be similar in a few ways but have multiple differences between them. Let's check the differences between functions and functors in C++. What are functions? Functions are pieces of code that can be called from any other part of a program. They are a way of organizing code so that it is reusable, and can be used to perform a specific set of operations. Functions can be defined in any programming language and are typically used to encapsulate a set of instructions that can be called multiple times. Example: C++ #include <iostream> using namespace std; // Function definition int add(int a, int b) { int c = a + b; return c; } // Driver code int main() { int a = 5, b = 6; int c = add(a, b); cout << c; return 0; } Output11What are functors? Functors, also known as function objects, are objects that behave like functions. They are a type of object that can be treated like a function and can be used to encapsulate a set of instructions. They are similar to functions in that they can be used to encapsulate a set of operations, but they are different in that they can store states and can be used in more complex ways. Example: C++ #include <iostream> using namespace std; // Functor definition struct Add { // Data members int a; // Constructor Add(int a) : a(a) { } // Function call operator int operator()(int b) { return a + b; } }; // Driver code int main() { Add add(5); int c = add(6); cout << c; return 0; } Output11Difference between functions and functorsFunctorsFunctionsFunctors are objects that can be called like functionsFunctions are blocks of code that can be called by their nameThey can be created by defining the function call operator (operator())They are created by declaring a function with a specific name and syntaxThey can store state and retain data between function callsFunctions do not store state and do not retain data between callsThey can be overloaded and customized to perform different tasksFunctions cannot be overloaded and must have a fixed signatureThey can be passed as arguments to other functions or returned as resultsFunctions can only be passed as arguments or returned as resultsThey can have different types of arguments and return valuesFunctions must have fixed types of arguments and return valuesThey are slower to execute than functions due to their added complexityFunctions are faster to execute due to their simplicity Comment More infoAdvertise with us Next Article C++ - Difference Between Functors and Functions I ishankhandelwals Follow Improve Article Tags : Technical Scripter C++ Technical Scripter 2022 STL CPP-Functions +1 More Practice Tags : CPPSTL Similar Reads Difference between Program and Function 1. Program : Program, as name suggest, are set or collection of instructions used by computer to execute specific task and these are created using particular programming languages such as C++, Python, Ruby, etc. 2. Function : Function, as name suggests, is basic concept in computer programming that 2 min read Difference between Relation and Function A relation in mathematics is simply a connection or a rule that links one set of things (called the domain) to another set of things (called the range). You can think of it as a collection of pairs that shows how items from one group are connected to items in another group.For example, Imagine you h 6 min read Difference Between Friend Function and Virtual Function in C++ A friend class can access private and protected members of other classes in which it is declared as friend. It is sometimes useful to allow a particular class to access private members of other classes. Just likely, a friend function is a function that is declared outside the scope of a class. This 3 min read Swift - Difference Between Function and Method Some folks use function and method interchangeably and they think function and method are the same in swift. But, function and method both are different things and both have their advantages. In the code, Both function and method can be used again but methods are one of these: classes, structs, and 4 min read Difference between virtual function and inline function in C++ Virtual function: Virtual function is a member function which is declared within a base class and is redefined by a derived class. Inline function: Inline function is a normal function which is defined by the keyword inline, it is a short function which is expanded by the compiler and its arguments 2 min read Like