Dr Christian Hicks wrote:
I have been learning to program in C over the last few weeks and have put some examples together. These include how to call Pascal from C and how to call C from Pascal. ... snip ...
What you are looking for is not a language dependency, but a run-time system dependency. This sort of interaction cannot be understood in general, so you would be better off understanding the basic mechanisms used in various systems. Move to a different machine or even compiler and the rules may be entirely different.
Most popular systems are built around the concept of a stack today, and usually (but not always) use this to pass parameters. Parameters can be passed in two basic forms, by value, or by reference. In C there is no reference, instead a pointer to the "referenced" parameter is passed by value. In Pascal, a parameter passed by reference is characterised by the VAR attribute, i.e. its value can be varied by the called function/procedure, and the default parameter passing mechanism is by value.
Parameters, or pointers (references) can also be passed in registers. This is less regular and requires more detailed knowledge of the actual system, so I am only mentioning it in passing.
The next fundamental thing to know about a parameter passing mechanism is the order. Some systems pass them in the order mentioned in the function header, and some in the reverse direction. Pascal *tends* to use the order mentioned, and C *tends* to use the reverse order, so that the first C parameter will normally be at the top of the stack when the call is made. The purpose of this is to ease the so-called varargs parameter lists of C, which do not occur in Pascal.
Another fundamental variation is "who is responsible for removing parameters from the stack". In Pascal a function is always called with a fixed list of parameters, so the code to remove it need be only in one place, and this is most efficiently done in the called function. In C, only the caller actually knows how many parameters were passed, so the caller is usually responsible for removal. Notice usually, these are not hard and fast rules, just tendencies.
A further kettle of worms is 'what is the mechanism for returning function values'. In C the caller can simply ignore the returned value, thus discarding it. In Pascal such failure is an error. Thus C tends to return values in registers, where they can be simply overwritten at any time, and Pascal oftern returns them on the stack, where they must be specifically removed to avoid fouling the stack.
Let me repeat - there is nothing dogmatic about any of these mechanisms. The implementor can do whatever s/he wishes, as long as the language rules are followed.
AFAIK the only language to specify general means of calling routines written in other languages is Ada. C++ has provisions for calling C routines. However these abilities only apply to the use of specified compilers and libraries.
A hardware platform may also specify specific calling protocols and mechanisms, which can ease creation of compilers etc. that provide interoperability. The IBM 360 and successors, and the HP3000, had such specifications.