Virtual functions have many advantages, but here are a couple of their disadvantages:
A function must return a result. For it to be useful it must be part of an expression, i.e., not called as a standalone routine. For example, suppose you have a function called 'sum' that adds two numbers and returns the result.
The following code would make no sense because you don't keep the result:
sum (a, b) ;
It would be better to use it as:
result = sum (a,b) ;
The only disadvantage to a function is that it incurs a function call. However, simple function calls can be optimised away by the compiler if the function can be inline expanded. Usually the function needs to be simple, one or two statements at most, or one that is called infrequently. If the function is implemented at the point of declaration then it is automatically a candidate for inline expansion, otherwise you must precede the function's implementation with the inline keyword. However, declaring a function inline is no guarantee it will be inline expanded, it is merely a hint to the compiler that the function is a candidate for inline expansion. Declaring every function inline will only result in a much slower compilation and if the increased code size would be detrimental to overall performance then the compiler may decide the expense of a function call far outweighs any lost performance from inline expansion. Some compilers may allow you to force an inline expansion, but there are still certain functions that cannot be inline expanded, such as recursive functions with a depth greater than n recursions, where n is usually limited to 16. All in all it's generally best to let the compiler decide as its inline optimisers are highly advanced.
The main advantage of a function is that it helps streamline your code, making it more readable and much easier to manage. Unlike a goto statement, which branches off never to return (known as spaghetti code), a function call always returns to the caller, and can return a value if necessary (as well as returning additional values through output parameters). If you have a complex statement or series of statements that are called often then turning those statements into a function means you only have to call the function rather than continually retype the same statements over and over. Not only do you eliminate the chance of a typo, you reduce code maintenance because the code is all in one place. If you need to modify the code, you only have to do it once, rather than for every occurrence (which may lead to errors if you forget to update all occurrences).
The only disadvantage to a function is in actually calling the function. If the function can't be inline expanded, the overhead in making the call may be detrimental to performance and memory consumption (depending on the number and type of parameters it accepts). However, even for non-inline expanded functions, the advantage of having common code in one place rather than in several more than offsets any disadvantages incurred by making the call. Updating one function is much easier than trying to locate and edit all instances of common code. Of course, if the compiler can generate inline expanded code for a function, then so much the better. The only disadvantage is increased code size, but that's hardly a problem when the advantage is better performance.
Only that it's easy to get carried away writing new function signatures that may never be used in your programs. Although it can increase the flexibility of your function, there's little point in writing overloads you'll never use. Although the compiler can ignore them, you'll have wasted time writing them in the first place. Other than that, there are no disadvantages.
Note that if the only difference is the type of parameter rather than the number of parameters, function templates are a much better option. The compiler generates the overloads for you, on an as-required basis, and you've only got one function to maintain.
The only disadvantage to inline functions is the increased code size, which can be detrimental to performance, even with the reduced number of function calls. However, declaring a function inline is no guarantee it will be inline expanded; the compiler is free to ignore the declaration if the increased code size would prove detrimental to code performance. The only functions that are guaranteed to be inline expanded are those declared in macros, hence non-trivial macros are generally frowned upon.
Disadvantages of using functions in c:
1)The execution time of function is higher.
2)Recursion is the biggest problem associated with functions.
using function unneccessarily will slow your program down...
disadvantages
All virtual functions (including pure-virtual functions) are represented in italics. All non-virtual functions are represented normally. There is no differentiation between pure and non-pure virtual functions, however some people append "=0" to distinguish the pure-virtual functions.
Of course they are used. Both stand-alone and class-member functions are used in C++.
Turn on the intellisense feature.
It depends which language you are using. Java member functions are virtual by default but C++ member functions are not. Java takes the viewpoint that if any member function is declared virtual then all member functions should be declared virtual, so they may as well be virtual by default. However, C++ takes the view that a member function should only be declared virtual if there's a specific reason to declare it virtual. Not all functions are meant to be overridden. Indeed, not all classes are meant to act as base classes. So all member functions are non-virtual by default. Purists will argue that the C++ method is the correct method. After all, there's no point in having a virtual-table if it's never going to be used. Java places the onus on the programmer to eliminate an unused virtual-table, whereas C++ simply doesn't provide one unless you explicitly declare one. However, the real reason C++ uses non-virtual methods by default is because it has to maintain compatibility with a C struct. A C struct is not a class so it has no methods (and therefore no virtual methods). It is a "plain-old-data" or POD structure. In C++, however, a struct is a class. As such, by default, it has a compiler-generated default constructor, default copy and move constructors, default copy and move assignment operators and a default destructor. It also has public access by default. However, because the compiler-generated methods are all trivial member-wise implementations, a C++ struct is backwardly compatible with a POD. Thus C code can use a C++ struct just as if it were a C struct, because both use POD structures by default. If C++ used virtual member functions by default, a struct would not be a POD by default, it would be a base class by default.
No, it does not. But Microsoft Visual Studio 2008 allows you to connect to a virtual machine and run your projects "sandboxed".
All virtual functions (including pure-virtual functions) are represented in italics. All non-virtual functions are represented normally. There is no differentiation between pure and non-pure virtual functions, however some people append "=0" to distinguish the pure-virtual functions.
Yes, there can be friend functions in C++.
Dynamic binding is achieved via virtual functions and the virtual table that is associated with every class that declares or inherits a virtual function. The virtual table (or v-table) maps every virtual function (including pure-virtual functions) to a function pointer that points to the most-derived overload. This makes it possible to invoke specific behaviour even when the runtime type of the object is unknown to the caller.
No, they are functions. Operators are -> or ++or /=
Of course they are used. Both stand-alone and class-member functions are used in C++.
In C++, methods are simply class member functions.
One.
C++ built-in functions are those functions that are provided for you as part of the language itself, and includes all of the C standard library functions (all of which were inherited from C) and is expanded upon by the C++ standard template library. C++ implementors may provide additional functions that are platform-specific, however these are not considered built-in functions becuase C++ is a cross-platform language. These are best described as 3rd party functions. The functions you yourself write are known as user-defined functions.
It depends which language you are using. Java member functions are virtual by default but C++ member functions are not. Java takes the viewpoint that if any member function is declared virtual then all member functions should be declared virtual, so they may as well be virtual by default. However, C++ takes the view that a member function should only be declared virtual if there's a specific reason to declare it virtual. Not all functions are meant to be overridden. Indeed, not all classes are meant to act as base classes. So all member functions are non-virtual by default. Purists will argue that the C++ method is the correct method. After all, there's no point in having a virtual-table if it's never going to be used. Java places the onus on the programmer to eliminate an unused virtual-table, whereas C++ simply doesn't provide one unless you explicitly declare one. However, the real reason C++ uses non-virtual methods by default is because it has to maintain compatibility with a C struct. A C struct is not a class so it has no methods (and therefore no virtual methods). It is a "plain-old-data" or POD structure. In C++, however, a struct is a class. As such, by default, it has a compiler-generated default constructor, default copy and move constructors, default copy and move assignment operators and a default destructor. It also has public access by default. However, because the compiler-generated methods are all trivial member-wise implementations, a C++ struct is backwardly compatible with a POD. Thus C code can use a C++ struct just as if it were a C struct, because both use POD structures by default. If C++ used virtual member functions by default, a struct would not be a POD by default, it would be a base class by default.
It should work without any special action.
Turn on the intellisense feature.
No, it does not. But Microsoft Visual Studio 2008 allows you to connect to a virtual machine and run your projects "sandboxed".