answersLogoWhite

0

Inline functions in c plus plus with prgram example?

Updated: 8/18/2019
User Avatar

Wiki User

12y ago

Best Answer

#include<iostream>

using namespace std;

inline int max(int a,int b)

{

return (a>b)?a:b;

}

int main()

{

int i1=3,i2=5;

cout<<endl<<"Inline function says max is "<<max(i1,i2);

return 0;

}

/*

Usually when a function is called, the compiler goes to the particular piece of code and executes it. But in the case of inline functions, the code from the body of the function is effectively pasted at the point of call. inline functions are used when the body of the function is only a line or so.*/

User Avatar

Wiki User

12y ago
This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: Inline functions in c plus plus with prgram example?
Write your answer...
Submit
Still have questions?
magnify glass
imp
Related questions

Automatic inline in c plus plus?

The C++ compiler will implicitly (automatically) mark functions for inline expansion whenever you define a function within its own declaration. If functions are declared and defined separately (even in the same file) then they are not implicitly marked for inline expansion. To enable inline expansion for these definitions, you must explicitly mark the definition (not the declaration).


When will you make a function inline in c plus plus?

yes,we can make function inline


What is an example program in c plus plus to square a number using the concept of an inline function?

... double squareOf_Number(double Number){return (Number*Number);}...int main(){...double Number = 0;...printf("Enter a number: ");cin >> Number;...printf("Square of %f is %f\n", Number, squareOf_Number(Number));...}Or you can include #include and use the function pow(double a, double b) which returns a^b.


Can there be friend functions in c plus plus?

Yes, there can be friend functions in C++.


What are the rules for inline functions in c plus plus?

When you mark function as inline compiler puts the whole body of function in those places it is called, similar idea as in macros. If you do not mark function as inlinecompiler inside still decides which functions should be inline and which not. Inline function is less performance costly especially if function is called very often. Why it is lest performance costly? Because to invoke function you need to prepare parameters, put them to stack, make jump and etc. and all those steps are eliminated if function is inline.Example (very basic):inline int sum(int a, int b) {return a + b;}int c, d;c = sum(2, 3); /* compiler will change to 2 + 3 */d = sum(2, 5); /* this one will be changed to 2 + 5 */Full inline functions are allowed in ANSI/ISO C99.


How do you create functions with multiple parameters in C plus plus?

Random example, function with two parameters: int main (int argc, char **argv) {...}


What is outline function in c plus plus language?

Outline is the opposite of inline. An inline expanded function is any function or class method where the declaration also provides the definition (the implementation). This is known as implicit inline expansion. Where the definition is kept separate from the declaration, you may use the inline keyword to specifiy that the function should be inline. This is known as explicit inline expansion. Inline expanded functions (whether implied or explicit) does NOT mean the function will in fact be inline expanded. It is merely a suggestion to the compiler. If the compiler's optimisers determine that there is no advantage to be gained by inline expanding a particular function, then that function becomes an outline function. Inline expansion simply means that the body of the function is inserted in place of the function call. Function calls are expensive in terms of memory and performance, so by eliminating the function call completely, your code performs faster and uses less memory. However, functions that are called many times throughout your code will result in a much larger code size, and large code runs slower than small code. Thus the benefit of eliminating a function call has to be weighed against the increased code size. Although some compilers do allow you to override the compiler's inline expansion optimisers, this is strictly non-standard. The best judge of what to expand and what not to expand is best left in the hands of the compiler, and indiscriminate use of the inline keyword should generally be avoided.


In c plus plus can a function be called from more than one place within a program?

Yes. In fact, that's one of the main reasons for having functions in the first place; so you don't have to keep typing the same or similar statements over and over. You write the code once, test and debug it, then call it whenever and wherever you need that code. Useful functions are best placed in a library that can be shared amongst many programs that require those functions. The standard library is a good example of this: you simply include the parts you need and call the functions as and when required, as many times as required. Sometimes you might write functions that are only ever called from one place in your program. This is also a good thing if it helps make your code more legible or easier to read. That is, a self explanatory function call is far more useful than commenting a series of complex statements. Such functions are also ideal candidates for inline expansion, allowing the compiler to remove the function call altogether, thus improving performance. You can also inline expand functions that are called many times, however these functions must be kept as simple as possible. Class accessors (getters) that simply return copies of member variables are always good candidates for inline expansion.


What is the difference between macro and inline function in c plus plus?

Macros are not actually part of the C++ language; they are nothing more than a simple text-replacement system intended to simplify your coding. Macros do not adhere to C++ type safety and cannot be debugged because macros are preprocessed, prior to compilation. Your compiler can only see the preprocessed code, not the original source code, and therefore cannot debug macros because the macros no longer exist at that point. Inline functions are functions that can be debugged like any other function, but the compiler is able to eliminate the overhead of function calla by replacing those calls with inline expanded code. This is not unlike a macro, which is by definition inline expanded, but retains the built-in type safety and debugging capabilities of the C++ language itself. Typically, if you can use an inline function (or C++ is general) then that is always the preferred option. But if a macro can achieve more than can be achieved with C++ alone, or can otherwise simplify the equivalent C++ code, then use a macro. Just keep in mind that macros are not type-safe and cannot be debugged by the C++ compiler.


Advantage and disadvantage of inline function in c plus plus?

The advantage of an inline function is that it eliminates the function calls completely, thus improving the performance wherever those calls appear. The disadvantage is that it increases code size, which can be detrimental to performance. For this reason, declaring a function for inline expansion is merely a hint to the compiler. If the increased code size would be detrimental, the compiler is free to ignore the inline declaration and retain the function call instead. While the programmer is free to manually expand their own functions, this only serves to increase maintenance should the function ever need to be changed, and could lead to errors should those changes not be propagated correctly. The advantage of having a function, even if it is only ever called once (or from within a loop), is to simplify your code and make it easier to read and maintain. If the function is simple, or is called seldom, then it is a good candidate for expansion, but its almost always better to let the compiler decide which functions should be inline expanded.


Why functions are not used in c plus plus?

Of course they are used. Both stand-alone and class-member functions are used in C++.


What is a Generic Function in c plus plus?

Predefined functions are built-in functions that perform standard operations and that generally do not depend on any classes. A predefined void function is simply a predefined function that has no return value. An example of a predefined void function is the abort() function which typically has the following signature: void abort(void);