📜  C++中内联函数与常规函数之间的区别

📅  最后修改于: 2021-05-30 11:08:03             🧑  作者: Mango

内联函数 是一个在调用时由编译器内联扩展的函数。在函数调用期间,执行了许多开销任务,例如保存寄存器,将参数推入堆栈并返回到调用函数。这些开销对于小型函数而言既费时又低效。在C++中,内联函数用于解决这些间接费用。它由 编译器在被调用时,从而避免了开销成本。在关键字之前使用称为“内联”的关键字 函数声明。

句法:

inline return_type function_name(parameters) 
{
// Insert your Function code here
}

例子:

C++
// C++ program to deonstrate inline function
  
// Importing input output stream files
#include 
  
using namespace std;
  
// Method 1
// Inline function
// To multiply two integer numbers
inline int multiplication(int x, int y)
{
  
    // Returning the producus of two numbers
    return x * y;
}
  
// Method 2
// Main driver method/function
// Execution begins here
int main()
{
    // Print and display the multiplication resultant number
    // with usage of above created multiplication() inline()
    cout << "Muliplication ( 20 , 30 ):"
         << multiplication(20, 30) << endl;
  
    return 0;
}


C++
// Importing input output streams
#include 
  
using namespace std;
  
// Method/Function
// To get square of integer number been passed
int square(int s)
{
    // Returning the square of number
    return s * s;
}
  
// Method
int main()
{
    // Calling the squaer() method/function
    // over randoM value N
      
    // Say N = 5
    // Display message only
    cout << "Enter number to compute its square : 5 " << endl;
  
    // Calling the above square() in main()
    // and print/display on the console
    cout << "Square is : " << square(5) << endl;
    
  return 0;
}


输出
Muliplication ( 20 , 30 ):600

现在来看下一个函数,它的名字表明Normal 函数基本上只是C++中的一个普通函数。它促进代码重用并使程序模块化。在函数调用期间,执行了许多开销任务,例如保存寄存器,将参数推入堆栈并返回到调用函数。

句法:

return_type_name function_name( parameters) 
{ 
 // Insert your Function code here 
}

例子:

C++

// Importing input output streams
#include 
  
using namespace std;
  
// Method/Function
// To get square of integer number been passed
int square(int s)
{
    // Returning the square of number
    return s * s;
}
  
// Method
int main()
{
    // Calling the squaer() method/function
    // over randoM value N
      
    // Say N = 5
    // Display message only
    cout << "Enter number to compute its square : 5 " << endl;
  
    // Calling the above square() in main()
    // and print/display on the console
    cout << "Square is : " << square(5) << endl;
    
  return 0;
}
输出
Enter number to compute its square : 5 
Square is : 25

C++中内联函数和普通函数之间的区别

S.No.              

Inline Function 

Normal Function  

1. It is expanded inline when it is invoked.  It is a function that provides modularity to the program.  
2. It is generally used to increase the execution time of the program. It is generally used to improve the reusability of code and make it more maintainable.  
3. It is basically a function that is used when functions are small and called very often.   It is basically a group of statements that performs a particular task.  It is used when functions are big.
4. It requires ‘inline keyword in its declaration.   It does not require any keyword in its declaration.  
5. It generally executes much faster as compared to normal functions.   It generally executes slower than inline function for small size function.  
6. In this, the body of functions is copied to every context where it is used that in turn reduces the time of searching the body in the storage device or hard disk. In this, the body of the function is stored in the storage device and when that particular function is called every time, then CPU has to load the body from hard disk to RAM for execution.  
7. The compiler always places a copy of the code of that function at each point where the function is called at compile time.  It does not provide such a type of functionality.  
8. It generally includes only 2–3-line codes.   When the number of line codes is real massive I.e., normal functions contain much code as per need.  
9. It is a little harder to understand and test as compared to normal function.   It is much easier to understand and test as compared to the inline function. 
10. Functions that are present inside a class are implicitly inline.   Functions that are present outside class are considered normal functions.  
11. Too many inline functions affect file size after compilation as it duplicates the same code.   Too many normal functions do not affect file size after compilation. 
要从最佳影片策划和实践问题去学习,检查了C++基础课程为基础,以先进的C++和C++ STL课程基础加上STL。要完成从学习语言到DS Algo等的更多准备工作,请参阅“完整面试准备课程”