C++ function calls those things

C++ function call

C++ Functions with Default Values ​​for Formal Parameters

Parameters with default values ​​must be given from right to left, given the following example:

int sum(int x;int y); //无默认值函数
int sum(int x;int y=0); //y有默认值
int sum(int x=0;int y=0); //x,y均匀默认值
int sum(int x=0;int y); //错误,不能x有默认值,而y没有默认值

The difference between function call procedure and default value:

int sum(int x = 0, int y = 0)
{
    
    
   return x + y;
}

int main()
{
    
    
   int a = 0;
   int b = 10;
   int ret = sum(a, b);
   /*
   mov ecx, dword ptr[ebp-8] //先将b移动入栈
   push ecx
   mov ecx, dword ptr[ebp-4]
   push ecx 
   call sum
   */
   cout << ret << endl;

   ret = sum(a);
   /* 
   push 0 //b默认为0
   mov ecx, dword ptr[ebp-4]
   push ecx
   call sum
   */

   ret = sum(); // 等价于sum(0,10);因为数字直接存放再内存中,所以直接可以调用。
   /*
   push 0 //b默认为0
   push 0
   call sum
   */
   return 0;
}

Features and advantages of functions with default values:

  1. When giving the default value, give it from right to left.
  2. In terms of call efficiency, using the default value or a given value directly will be more efficient than passing variables.
  3. Default values ​​can be given to formal parameters at the definition, and default values ​​can also be given to formal parameters at the declaration.
  4. When a default value is given to a formal parameter, whether it is defined or declared, the default value of the formal parameter can only appear once . (A function can be declared multiple times, but defined only once)
// C++带有默认值参数的声明规则,形参默认值只能出现一次,不管是函数定义还是实现
int sum2(int x=10, int y=20);//函数声明
int sum2(int x, int y) {
    
     return x + y; }//函数定义
int sum3(int x, int y = 20);
int sum3(int x = 10, int y);
int sum3(int x, int y) {
    
     return x + y; };
int sum4(int x, int y);
int sum4(int x = 10, int y = 20) {
    
     return x + y; }

More c++ function declarations and definitions can be viewed by clicking here.

inline function

What is the difference between an inline function and an ordinary function?

inline function: In the compilation process, there is no function call overhead, and the code of the function is directly expanded at the call point of the function.

Inline functions no longer generate corresponding function symbols.

But not all inlines will be processed into inline functions by the compiler (especially recursive functions will not be inlined, and if there are many codes, they may not be inlined either). If the function is used frequently, it can be considered as an inline function.

The debug version, inline will not work, it can only appear in the release version.

int sum(int x,int y){
    
    return x+y;}
inline int sum2(int x,int y){
    
    return x+y;} 

int main()
{
    
    
	int a=0,b=10;
	int res=sum(a,b);
	//这里是sum函数调用点,函数调用其实存在很多的过程,先将b,a一次压入函数栈中,
	//在调用add函数进行计算,再将结果返回出来。在这个过程中函数执行了空间的开辟和回收,
	//如果调用很多次就会有大量的空间开辟和回收开销,甚至这些开销远比功能的开销大,那么函数效率就很低。
	int res=sum2(a,b);//调用内联函数,会直接将转化为res=a+b;就没有调用函数开销。
	return 0;
}

function overloading

Three questions need to be understood:

  1. Why does C++ support function overloading, but C language does not support function overloading?
  2. What should be paid attention to in function overloading?
  3. How to call each other between C++ and C language code?

Question 1: Why does C++ support function overloading, but C language does not support function overloading?

This is because when the C++ code compiles the function to generate symbols, the function is composed of the function name + parameter list type . In C language, when the function is compiled to generate symbols, the function is composed of the function name , so it cannot be overloaded according to the parameter list type when linking.

In C++, the function overloading at the function call point is actually determined at compile time .

Problem 2: Function overloading

  1. A group of functions with the same function name but different numbers or types of parameter lists is called function overloading.
  2. For a group of functions to be called overloading, they must be in the same scope.
  3. How const and volatile affect parameter types.
  4. A group of functions with the same function name and the same parameter list, but only the return value type, is not enough for function overloading.

In fact, polymorphism is the use of the idea of ​​function overloading, polymorphism is divided into: static polymorphism (compile time): function overloading; dynamic polymorphism (run time).

Example of function overloading:

#include<iostream>
#include<string>
using namespace std;

// 下面三个同名的compare函数构成函数重载:
// 函数名相同,参数列表不同,在同一个作用域下
bool compare(int a,int b)
{
    
    
    cout << "compare_int_int" << endl;
    return a > b;
}

bool compare(double a,double b)
{
    
    
    cout << "compare_double_double" << endl;
    return a > b;
}

bool compare(char* a,char* b)
{
    
    
    cout << "compare_char*_char*" << endl;
    return strcmp(a, b);
}

// data存储在全局变量
int data = 10;

int main()
{
    
    
    // 在局部作用域下定义compare,后面调用三个compare就会默认使用该局部作用域下的声明,
    // 类型不匹配就会使用强制类型转化,类型转化不了就报错
    // 所以要在同一作用域下才会构成函数重载
    // bool compare(int a, int b);

    compare(10, 20);
    compare(10.0, 20.0);
    compare("aaa", "bbb");

    // data和全局变量data是可以重名的,因为作用域不同
    int data = 20;
    cout << data << endl;//20,优先使用该作用域下的值
    cout << ::data << endl;//10,::函数作用域,使用全局变量的data
    return 0;
}

Question 3: How do C/C++ codes call each other?

C++ calls functions in C files

  1. Introduce C files in C++.
  2. The function is declared.
extern "C" //让C++知道这个函数是C语言中定义的,否则就会出现无法解析外部命令,这是因为C/C++函数编译产生的函数符号不一样。
{
    
    
	int sum(int a,int b);
}

Calling C++ functions in C language:

  1. Introduce C++ files in C language.
  2. Modify the form of a function in C++.
#ifdef __cplusplus //__cplusplus是C++中自定义内容
extern "C" {
    
    
#endif  
	// 如果是C++文件,那就是用C的方式进行编译
	int sum(int a,int b)
	{
    
    
		return a+b;
	}
#ifdef __cplusplus
}
#endif

Guess you like

Origin blog.csdn.net/qq_45041871/article/details/132261559