Example of Calling Convention

The following program illustrates how to use the calling convention.

C++




// C++ Program to demonstrate the calling convention
#include <iostream>
  
// __cdecl calling convention
int __cdecl cdeclAdd(int a, int b)
{
    int c = a + b;
    return c;
}
  
// __stdcall calling convention
int __stdcall stdcallAdd(int a, int b)
{
    int c = a + b;
    return c;
}
  
// __fastcall calling convention
int __fastcall fastcallAdd(int a, int b, int c, int d)
{
    int e = a + b + c + d;
    return e;
}
  
// __thiscall calling convention
class Temp {
public:
    int __thiscall thiscallAdd(int a, int b)
    {
        int c = a + b;
        return c;
    }
};
  
// driver code
int main()
{
    int result;
    Temp obj;
  
    // Function calls and output
    result = cdeclAdd(1, 2);
    std::cout << "Result: " << result << std::endl;
  
    result = stdcallAdd(3, 4);
    std::cout << "Result: " << result << std::endl;
  
    result = fastcallAdd(7, 8, 9, 10);
    std::cout << "Result: " << result << std::endl;
  
    result = obj.thiscallAdd(5, 6);
    std::cout << "Result: " << result << std::endl;
}


Output

Result: 3
Result: 7
Result: 34
Result: 11

Here we have 4 functions: cdeclAdd(), stdcallAdd(), fastcallAdd(), and thiscallAdd() with calling conventions __cdecl, __stdcall, __fastcall, and __thiscall respectively. They are being called by the caller i.e. main(). Let’s understand each of them one by one.

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;
}


Output

Geeks

In the above code, main() is the caller and func() is the callee.

Similar Reads

Calling Conventions

...

Example of Calling Convention

The Calling Conventions in C/C++ are the guidelines that determine:...

__cdecl

The following program illustrates how to use the calling convention....

__stdcall

...

__fastcall

The __cdecl calling convention is the default calling convention in C/C++. In this calling convention:...

__thiscall

...

Advantages of Calling Conventions

...