All Exams Test series for 1 year @ ₹349 only
Question

In C++, ________ functions are not actually called, rather, their codes are expanded

The correct answer is
inline

C++ Inline Functions Explained

In C++, when you call a function, the program typically performs several steps: it saves the current location, jumps to the function's code, executes it, and then returns to the saved location. This process is known as a function call overhead.

However, the question describes a special type of function where this overhead is avoided because the function's code isn't executed via a standard call. Instead, the compiler directly inserts the function's code at the location where the function is invoked. These are known as inline functions.

Understanding Inline Functions

  • Keyword: The $inline$ keyword is used as a suggestion to the compiler.
  • Mechanism: When you declare a function as $inline$ (either by placing the keyword before the function definition or by defining the function within the class definition), you are asking the compiler to replace the function call with the actual code of the function body itself.
  • Benefit: This process, called code expansion, eliminates the overhead associated with traditional function calls. It can lead to performance improvements, especially for small, simple functions that are called frequently.
  • Compiler's Role: It's important to note that $inline$ is just a hint. The compiler ultimately decides whether to honor the request based on factors like function size and complexity. It might ignore the hint or even inline functions not explicitly marked as such.

Analyzing Other Options

Let's look at why the other options are not the correct fit for the description:

  • Friend Functions: $friend$ functions are non-member functions that are granted special access to a class's private and protected members. They are called like regular functions and do not involve code expansion.
  • Virtual Functions: $virtual$ functions are used in inheritance and polymorphism. They allow derived classes to provide their specific implementation of a function. Function calls to virtual functions are resolved at runtime (dynamic dispatch), which introduces its own kind of overhead, rather than code expansion.
  • Static Functions: $static$ member functions belong to the class itself, not to any specific object. They can be called without creating an object of the class. However, they are still invoked using a standard function call mechanism and their code is not expanded inline by default.

Therefore, the functions in C++ whose codes are expanded rather than being called are inline functions.

Was this answer helpful?

Important Questions from Programming in C

  1. Consider the following C program:
    #include <stdio.h>
    int g(int n) {
    return (n+10);
    }
    int f(int n) {
    return g(n*2);
    }
    int main() {
    int sum, n;
    sum=0;
    for (n=1; n<3; n++)
    sum + = g(f(n));
    printf ("%d", sum);
    return 0;
    }
    The output of the given C program is ____________ (Answer in integer)

  2. Determine what's true for x, if we define int *x[10] ?
  3. In bitwise operator, each right shift operator divides by ________.
  4. In Switch-case, the ________ is not allowed to be case labels.
  5. What is the output of the following code ? 

    my recursive<-_function(n) 

    }{ 

    if(n = O) return

     print(n) 

    my_recursive(n-1); 

    }{ my_recursive_function(10)

Need Expert Advice?

Start Your Preparation with Prepp Mobile App

Download the app from Google Play & App Store
Download the app from Google Play & App Store
Prepp Mobile App