Advantages of Calling Conventions
The following are the advantages of calling conventions in C/C++:
- The calling convention helps in the standardization of the compiler’s way of function invoking and parameter passing.
- Standardization leads to interoperability between programming languages.
- Calling conventions implements efficient methods to invoke functions according to the requirements.
Calling Conventions in C/C++
In C/C++ programming, a calling convention is a set of rules that specify how a function will be called. You might have seen keywords like __cdecl or __stdcall when you get linking errors. For example:
error LNK2019: unresolved external symbol "void __cdecl A(void)" (?A@@YAXXZ) referenced in function _main
Here, we see __cdecl being used. It is one of the calling conventions in C/C++. You can also see code like this in 3rd party libraries.
For example:
extern __m128i __cdecl _mm256_mask_cvtepi32_epi16(__m128i, __mmask8, __m256i);
What are caller and callee?
When we call a function, a stack frame is allocated for that function, arguments are passed to the function, and after the function does its work, the allocated stack frame is deallocated and control is passed to the calling function.
The function that calls the subroutine is called the caller. The function that gets called(i.e. subroutine) by the caller is called the callee.
C++
// C++ Program to illustrate the caller and callee #include <iostream> // callee void func() { std::cout << "Geeks" ; } // caller int main() { // function call func(); return 0; } |
Geeks
In the above code, main() is the caller and func() is the callee.