Illustrate the reserved word inline with two examples.
Question
Illustrate the reserved word inline with two examples.
Solution 1
The reserved word inline is used in C++ programming language. It is used to suggest to the compiler that it should attempt to generate code (inline code) for a particular function that is faster than the code it would generate for a normal function call, but this is only a suggestion and not a command. The compiler can ignore this suggestion.
Here are two examples of how to use inline in C++:
Example 1:
inline int Max(int x, int y)
{
return (x > y)? x : y;
}
In this example, Max is an inline function. When this function is called, the code of the function is inserted at the point of the function call, and the values of the arguments are inserted in that code.
Example 2:
class Test
{
public:
inline void display()
{
cout << "Inline function in C++" << endl;
}
};
In this example, display is an inline member function of the class Test. When this function is called, the code of the function is inserted at the point of the function call.
Solution 2
The inline keyword is a hint given to the compiler that the programmer thinks a particular function will execute faster if the compiler inserts the complete body of the function in every context where that function is used, though it's up to the compiler to replace invocations with expressions or not. This is typically used with small, frequently called functions. The keyword is specific to C++.
Here are two examples:
Example 1:
inline int Max(int x, int y)
{
return (x > y)? x : y;
}
In this example, the function Max is declared with the keyword inline. If the compiler chooses to inline this function, every time Max is called, the call will be replaced with (x > y)? x : y.
Example 2:
inline void display()
{
cout<<"Inline Function"<<endl;
}
In this example, the function display is declared as inline. If the compiler chooses to inline this function, every time display is called, the call will be replaced with cout<<"Inline Function"<<endl;.
Remember, the decision to actually replace the function call with the body of the function is ultimately up to the compiler. The inline keyword is just a suggestion.
Similar Questions
1. _________are words that a programming language has set aside for its own use ?Control words Control structures Reserved words Reserved keys
__is a style in spoken English that is done privately and may include non-verbal messages
Mark the option which is closest to the meaning of the word given below. IMPLIED Ops: A. Indirect B. Indicate C. Open D. Exact
Explain ambiguous and unambiguous context free grammar with example.
There are certain predefined words as part of C programming language that have a special meaning and purpose. They are called reserved words or keywords.The user (programmer) cannot redefine these keywords, i.e.,the user cannot change their spellings or add new ones to the C programming language.All reserved words in C are formed using only the lowercase letters.Keywords have special meaning and purpose, so they cannot be used as variable names.C language has 32 reserved words as per ANSI standards. They are as given below:auto break case char const continue default do double else enum externfloat for goto ifint long register returnshort signed sizeof staticstruct switch typedef union unsigned void volatile whileSelect the correct statements from the given statements.ANSI C has 32 reserved words.Reserved words have predefined meaning in the system and they are known to the C compilerKeywords can be used as variable names.The meaning of keywords can be redefined.
Upgrade your grade with Knowee
Get personalized homework help. Review tough concepts in more detail, or go deeper into your topic by exploring other relevant questions.