# 521-introduction to C + + (function parameters, inlining, function overloading, C and C + + calling each other)

Posted by basim on Sun, 05 Sep 2021 22:44:55 +0200

## Functions with default values for formal parameters

1. The default value can only be given from right to left
2. Call efficiency
3. Define the default values that can be given to formal parameters, and declare the default values that can also be given to formal parameters
4. When a formal parameter is given a default value, whether given at the definition or declaration, the default value of each formal parameter can only appear once!!!

It works. Because the compiler is from top to bottom, the default is from right to left, b=20, a=10

Let's look at the following example

If only parameter a is passed in, then b is the default value of 20
If you add default values to both a and b, you can call without passing parameters.

We give parameters from left to right. In fact, stack pressing is from right to left, so we give parameters from right to left when giving default values

```//sum(, 20); This is wrong
int sum(int a, int b = 20);
int sum(int a = 10, int b);
int main()
{
int a = 10;
int b = 20;

int ret = sum(a, b);
/*
mov eax, dword ptr[ebp-8]
push eax

push 28H
mov ecx, dword ptr[ebp-4]
push ecx
call sum
*/
cout << "ret:" << ret << endl;

/*
push 14H
mov ecx, dword ptr[ebp-4]
push ecx
call sum
*/
ret = sum(a);

/*
push 14H
push 0Ah
call sum
*/
ret = sum();//sum(20, 50);

return 0;
}

int sum(int a, int b)
{
return a + b;
}
```

## Inline inline function

The difference between inline functions and ordinary functions???
Inline inline function:

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

The inline function no longer generates the corresponding function symbol

Inline simply suggests that the compiler handle this function as an inline function

But not all inline functions are processed by the compiler as inline functions - such as recursion
The compiler does not execute the instructions of the code and does not know how many times it calls itself recursively. Moreover, if the amount of code of this function is huge, even if you write inline, the compiler will not process inline

In the debug version, inline does not work because debugging is required; Inline can only appear in the release version
g++ -c main.cpp -O2
objdump -t main.o (check the symbols. Inline functions have no symbols)

Do not use inline functions

```int sum(int x, int y)// *.o   sum_int_int  .text
{
return x + y;
}
int main()
{
int a = 10;
int b = 20;

int ret = sum(a, b);
//Here are standard function call process parameters, stack pressing, function stack frame opening and fallback process
//There is the cost of function calls
//The x+y mov add mov loop makes 1000000 x+y function calls, which is expensive

return 0;
}
```

Using inline functions

```inline int sum(int x, int y)//The inline function no longer generates the corresponding function symbol
{
return x + y;
}
int main()
{
int a = 10;
int b = 20;

int ret = sum(a,b);
return 0;
}
```

1. A group of functions with the same function name and different number or type of parameter list, then this group of functions is called - function overload.
2. A group of functions must be in the same scope before they can be called overloads.
3. How does const or volatile affect the parameter type.
4. A group of functions with the same function name and parameter list, but different return values? It's not called overloading

When C + + code generates function symbols, it is composed of function name + parameter list type!
When C code generates function symbols, it is determined by the function name!

3. How do C + + and C language codes call each other?

Let's look at the following code and provide three groups of functions. The function names are the same, but their parameter lists are different.

```bool compare(int a, int b)//compare_int_int
{
cout << "compare_int_int" << endl;
return a > b;
}
bool compare(double a, double b)//compare_double_double
{
cout << "compare_double_double" << endl;
return a > b;
}
bool compare(const char *a, const char *b)//compare_const char*_const char*
{
cout << "compare_char*_char*" << endl;
return strcmp(a, b) > 0;
}
int main()
{
compare(10, 20);//call compare_int_int
compare(10.0, 20.0);//double -> int
compare("aaa", "bbb");//const char* => int

return 0;
}
```

This is a typical function overload call.
The overloaded function name is the same. In the compiler compilation process, the appropriate overloaded version is selected according to the arguments passed in when the function is called.
A group of functions with the same function name and different number or type of parameter list, then this group of functions is called - function overload.

What's the reason for this? Why doesn't the second compare call double and overload the function of double? Why call a function overload with int and int?
And when we look at the second compilation result, it shows an error. In other words, the third compare call does not call the function overload with char*,char *, but also calls the function overload of int and int?
Why do you call this function all at once?

In our C/C + +, the scope is divided. For example, we define a global variable in the global scope and a local variable in the local scope of the function. The names of the two variables can be the same.

When we use this variable in the local scope of the function, we give priority to calling the variable defined by the current nearest scope.
If you want to call a variable with the same name in the global scope in the local scope, it can be written as follows:

Let's go back to the code compare just discussed. There are three function definitions globally. Now, we declare the function locally in the main function. If we call it, the function declaration is enough. When the three compare functions of the main function are called, the compiler sees a function declaration in the nearest scope, that is, the local scope of the main function, So everyone called the compare function.
2. A group of functions must be in the same scope before they can be called overloads.

3. How does const or volatile affect the parameter type?

The two func functions have the same scope and function name. See whether their parameter lists are the same or different?

For the compiler, the two func functions are the same.
Let's check it out

For compilers as like as two peas, the two functions of the function are integer int, and the symbols of the functions are exactly the same.
Let's add a pointer to see the effect. It seems that it can run

If we move the pointer to the front of const

Wrong again.
How does const or volatile affect the parameter type?

A group of functions with the same function name and parameter list, but different return values? It's not called overloading

## C and C + + calling each other?

C + + cannot be called directly! What should I do?
Extend the C + + source code to extern "C"
C + + calling C code: you can't call it directly! What should I do?
Expand the declaration of C function in extern "C"

Let's write a. c file

We call this sum function in another. cpp file

The compiler generates symbols (function plus parameter list) according to the C + + standard. When linking, it is found that there is an undefined symbol, which can not be found in other obj or. o files.

The sum function in the. C file generates symbols according to C rules and function names.

C + + calling C code: you can't call it directly! What should I do?
Expand the declaration of C function in extern "C"

Tell the compiler that this function is generated under the rules of C language, so when you generate symbols, do not follow the way of C + +, but generate symbols according to the rules of C language.

C + + cannot be called directly! What should I do? Extend the C + + source code to extern "C"

Implementing sum code in C + + file

Calling the sum function of the C++ file in the C file

Because in C files, the compiler generates symbols according to the rules of C language, it naturally can not match the function symbols in C + + files.
How?

The sum function in C + + file is implemented by externally extending extern C

In the C + + file, tell the compiler to generate the symbol of this function in the way of C language
In this way, call this function in the C file and you can find it.

```//__ FILE__// This is the file name that prints the current code directly
//__ LINE__// This is the number of lines directly printing the current code
//The above two are built-in macros of the compiler
//As long as it is a C + + compiler, it is built-in__ cplusplus is the macro name
#ifdef __cplusplus / / this macro
extern "C" {
#endif
int sum(int a, int b) // sum  .text
{
return a + b;
}
#ifdef __cplusplus
}
#endif
//Such writing is, if defined__ cplusplus macro (built-in macro of C + + compiler) executes the following external C to generate function symbols according to C rules
//If you compile this code with the C compiler, there is no definition__ cplusplus is a macro, so the following extern C is not included. The sum function is also a symbol for generating C rules
```

The result of this writing is: whether it is generated by C compiler or C + + compiler, it generates function symbols according to C rules! Can be called directly to other C projects!

Topics: C C++