Inline Functions 是一个在调用时由编译器内联扩展的函数。在函数调用期间,会执行许多开销任务,例如保存寄存器、将参数压入堆栈以及返回调用函数。对于 small-size 函数来说,这些开销既耗时又低效。在C++中,内联函数就是用来解决这些开销成本的。它通过以下方式线性扩展 编译器在调用时进行编译,从而避免了开销。名为“的关键字排队'在之前使用 函数声明。
用法:
inline return_type function_name(parameters) 
{
// Insert your Function code here
}
例子:
C++
// C++ program to demonstrate inline function
// Importing input output stream files
#include <iostream>
using namespace std;
// Method 1
// Inline function
// To multiply two integer numbers
inline int multiplication(int x, int y)
{
    // Returning the product 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 << "Multiplication ( 20 , 30 ):"
         << multiplication(20, 30) << endl;
    return 0;
}输出
Multiplication ( 20 , 30 ):600
现在详细讨论下一个函数,顾名思义, Normal Function 本质上只是一个常见的 function in C++ 。它促进代码重用并使程序模块化。在函数调用期间,会执行许多开销任务,例如保存寄存器、将参数压入堆栈以及返回调用函数。
用法:
return_type_name function_name( parameters) 
{ 
 // Insert your Function code here 
}
例子:
C++
// Importing input output streams
#include <iostream>
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++中内联函数和普通函数的区别
| 编号 | 内联函数 | 正常函数 | 
|---|---|---|
| 1. | 当它被调用时,它会被内联扩展。 | 它是一个为程序提供模块化的函数。 | 
| 2. | 一般用于增加程序的执行时间。 | 一般用于提高代码的可重用性,使其更易于维护。 | 
| 3. | 它本质上是一个在函数较小且调用频繁时使用的函数。 | 它本质上是执行特定任务的一组语句。当函数很大时使用它。 | 
| 4. | 这个需要 '排队'其声明中的关键字。 | 它的声明中不需要任何关键字。 | 
| 5. | 与普通函数相比,它的执行速度通常要快得多。 | 对于小尺寸函数,它通常比内联函数执行得慢。 | 
| 6. | 在这种情况下,函数体被复制到使用它的每个上下文,从而减少了在存储设备或硬盘中搜索函数体的时间。 | 在这种情况下,函数体存储在存储设备中,每次调用该特定函数时,CPU 都必须将函数体从硬盘加载到 RAM 中执行。 | 
| 7. | 编译器始终在编译时调用该函数的每个点处放置该函数代码的副本。 | 它不提供此类函数。 | 
| 8. | 它一般只包含2-3行代码。 | 当行代码数量非常大时,即普通函数根据需要包含大量代码。 | 
| 9. | 与正常函数相比,理解和测试有点困难。 | 与内联函数相比,它更容易理解和测试。 | 
| 10. | 类中存在的函数是隐式内联的。 | 存在于类之外的函数被视为普通函数。 | 
| 11. | 太多内联函数会影响编译后的文件大小,因为它会重复相同的代码。 | 普通函数过多不会影响编译后的文件大小。 | 
相关用法
- C++ Inline和Macro的区别用法及代码示例
- C++内联函数用法及代码示例
- C++ Integer转String用法及代码示例
- C++ Iswctype()用法及代码示例
- C++ INT_MAX用法及代码示例
- C++ INT_MIN用法及代码示例
- C++ Implementing of strtok()用法及代码示例
- C++ Iterators和Pointers的区别用法及代码示例
- C++ cos()用法及代码示例
- C++ sin()用法及代码示例
- C++ asin()用法及代码示例
- C++ atan()用法及代码示例
- C++ atan2()用法及代码示例
- C++ acos()用法及代码示例
- C++ tan()用法及代码示例
- C++ sinh()用法及代码示例
- C++ ceil()用法及代码示例
- C++ tanh()用法及代码示例
- C++ fmod()用法及代码示例
- C++ acosh()用法及代码示例
- C++ asinh()用法及代码示例
- C++ floor()用法及代码示例
- C++ atanh()用法及代码示例
- C++ log()用法及代码示例
- C++ trunc()用法及代码示例
注:本文由纯净天空筛选整理自madhurihammad大神的英文原创作品 Difference Between Inline and Normal Function in C++。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。
