Declaring Near and Far Functions

You cannot declare functions as __huge. The rules for using the __near and __far keywords for functions are similar to those for using them with data:

The keyword always modifies the function or pointer immediately to its right.

If the item immediately to the right of the keyword is a function, the keyword determines whether the function is called using a near (16-bit) or far (32-bit) address. For example,

char __far fun();

defines fun as a function with a 32-bit address that returns a char. The function may be located in near memory or far memory, but it is called with the full 32-bit address. The __far keyword applies to the function, not to the return type.

If the item immediately to the right of the keyword is a pointer to a function, the keyword determines whether the function will be called using a near (16-bit) or far (32-bit) address. For example,

char (__far *pfun)();

defines pfun as a far pointer (32 bits) to a function returning type char.

Function declarations must match function definitions.

The __huge keyword does not apply to functions. That is, a function cannot be huge (larger than 64K). A function can return a huge data pointer to the calling function.

The __based keyword can be used to modify a function declaration, and can be used in combination with the __near and __far keywords. Based functions are described in “Using Based Addressing for Functions”. A function can return a based pointer unless it is a pointer based on __self (see “Using Based Pointers and Data”).

The example below declares fun1 as a far function returning type char:

char __far fun1(void); /* small model */

char __far fun(void)

{

.

.

.

}

Here, the fun2 function is a near function that returns a far pointer to type char:

char __far * __near fun2(); /* large model */

char __far * __near fun()

{

.

.

.

}

The example below declares pfun as a far pointer to a function that has an int return type, assigns the address of printf to pfun, and prints “Hello world” twice.

/* Compile in medium, large, or huge model */

#include <stdio.h>

int (__far *pfun)( char *, ... );

void main()

{

pfun = printf;

pfun( “Hello world\n” );

(*pfun)( “Hello world\n” );

}