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

📅  最后修改于: 2021-09-13 02:39:22             🧑  作者: Mango

内联函数 是一个在调用时由编译器内联扩展的函数。在函数调用过程中,会执行很多开销任务,例如保存寄存器、将参数压入堆栈以及返回调用函数。这些开销对于小型函数来说既耗时又低效。在 C++ 中,内联函数用于解决这些开销成本。它由 在调用时编译器,因此避免了开销成本。称为“ inline ”的关键字用于 函数声明。

句法:

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

例子:

C++
// C++ program to demonstrate 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 square() 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 square() 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 等的准备工作,请参阅完整的面试准备课程