Loading....
Coupon Accepted Successfully!

 

Calling Convention

Calling convention means how parameters are pushed on the stack when a function is invoked. Table 14.4 describes the calling convention method of a few languages.

Table: Calling Conventions
 

Language

Order

Parameter Passed

Basic

In order (left to right)

By address

Fortran

In order (left to right)

By address

C

In reverse order (right to left)

By value and address

C++

In reverse order (right to left)

By value, address, and reference

Pascal

In order (left to right)

As values


In C/C++, parameters are passed from right to left; whereas in other languages such as basic, FORTRAN calling convention is in order from left to right. The following program explains the calling convention of C++:

 

14.16 Write a program to demonstrate calling convention of C++.

#include<iostream.h>

#include<constream.h>

void main()

{

clrscr();

void show (int,int);

int x=2,y=3;

show (x,y);

}

void show (int x, int y)

{

cout<<x;

cout<<endl;

cout<<y;

}

OUTPUT
2
3

Explanation:
In this program, integer variables x and y are initialized to two and three, respectively. The show() function show() is invoked, and x and y are passed as per the statement show (x, y). The parameters are passed from right to left. The variable y is passed first followed byx. The values of variables with their position in the stack are displayed in Figure.
 

Fig: Stack with arguments

 

The value of the rightmost variable is pushed first followed by other variables in sequence.





Test Your Skills Now!
Take a Quiz now
Reviewer Name