11.8 Return values
-------------------
Values returned from functions are found in registers. The following
table shows which register is used for each returned type.
-----------------------------
|returned type|register used|
-----------------------------
| byte | AL |
| word | AX |
| dword | EAX |
| char | AL |
| int | AX |
| long | EAX |
| float | EAX |
-----------------------------
The simplest way of returning values from functions is to use the
command return() but one can also directly load the returned value into the
appropriate register. For example, the following two functions return the
same value:
byte proc_one ()
{
return (42);
}
byte proc_two ()
{
AL = 42;
}
Many DOS 0x21 interrupt functions use as an index of successful
execution the setting or unsetting of the 'carry' flag. It is possible to
use processor flags when returning from functions in other situation where
you need to have the status of a successful or unsuccessful execution of a
function. This makes it possible to make better use of the abilities of the
processor and thus to reduce code size and increase program speed.
In addition to flags, when returning from functions you can still
return various types via registers AL/AX/EAX. If a function was declared
to be of return type 'int' and 'CARRYFLAG', when it is used in 'IF' and
'WHILE' type comparison functions the 'carry' flag will be checked and
register AX will not be compared. Here is an example of using return of
flags from functions:
int CARRYFLAG FOPEN(); // declaration of a function
void proc()
{
IF ( FOPEN(name,0) ) Error ( "File not open" );
}
Examples of syntax permitted for using a flag as returned value:
IF ( ! FOPEN() )...
IF ( @ FOPEN() )...
IF ( ! @ FOPEN() )...
IF ( handl = FOPEN() )...
IF ( handl = @ FOPEN() )...
IF ( ! handl = FOPEN() )...
IF ( ! handl = @ FOPEN() )...
Here are some examples in which, even though a flag used as a return
value has been declared for a function, there is comparison of register AX:
IF ( FOPEN() == 5 )... // comparison performed
IF ( FOPEN() + 2 )... // the result of the function undergoes further
// calculation, resulting in a change to the flags