Pointers in BCX

FUNCTION and SUB Pointers

Example 1:


 
 GLOBAL Func(DUMMY AS INTEGER) AS FUNCTION INTEGER
 
 SetFunc(x0)
 ? Func(2)
 
 SetFunc(x1)
 ? Func(2)
 
 
 SUB SetFunc (f AS Func_TYPE)
   Func = f
 END SUB
 
 '-----------------------------------------------

 FUNCTION x0(x AS INTEGER) AS INTEGER
   FUNCTION = x * x
 END FUNCTION
 
 
 FUNCTION x1(x AS INTEGER) AS INTEGER
   FUNCTION = x * x * 2
 END FUNCTION

Result:


 4
 8

Example 2:


 
 SET Funcs[] AS FARPROC
   x0,
   x1,
   x2,
   x3,
   x4
 END SET
 
 FUNCTION x0
   FUNCTION = 0
 END FUNCTION
 
 FUNCTION x1
   FUNCTION = 1
 END FUNCTION
 
 FUNCTION x2
   FUNCTION = 2
 END FUNCTION
 
 FUNCTION x3
   FUNCTION = 3
 END FUNCTION
 
 FUNCTION x4
   FUNCTION = 4
 END FUNCTION
 
 '------------------------

 DIM i
 
 FOR i = 0 TO 4
   ? Funcs[i]()
 NEXT

Result:


 0
 1
 2
 3
 4

Example 3:

 
 SET Funcs2[] AS FARPROC
   s0$,
   s1$,
   s2$,
   s3$,
   s4$
 END SET
 
 FUNCTION s0$
   FUNCTION = "0"
 END FUNCTION
 
 FUNCTION s1$
   FUNCTION = "1"
 END FUNCTION
 
 FUNCTION s2$
   FUNCTION = "2"
 END FUNCTION
 
 FUNCTION s3$
   FUNCTION = "3"
 END FUNCTION
 
 FUNCTION s4$
   FUNCTION = "4"
 END FUNCTION
 
 '-----------------------

 DIM i
 
 FOR i = 0 TO 4
   ? Funcs2$[i]()
 NEXT

Result:


 0
 1
 2
 3
 4

Example 4:

 
 GLOBAL PF_1_ARG(DUMMY AS INTEGER) AS FUNCTION INTEGER
 
 SET Funcs3[] AS PF_1_ARG_TYPE
   n0,
   n1,
   n2,
   n3,
   n4
 END SET
 
 FUNCTION n0(n AS INTEGER)
   FUNCTION = n * 0
 END FUNCTION
 
 FUNCTION n1(n AS INTEGER)
   FUNCTION = n * 1
 END FUNCTION
 
 FUNCTION n2(n AS INTEGER)
   FUNCTION = n * 2
 END FUNCTION
 
 FUNCTION n3(n AS INTEGER)
   FUNCTION = n * 3
 END FUNCTION
 
 FUNCTION n4(n AS INTEGER)
   FUNCTION = n * 4
 END FUNCTION
 
 '------------------------------

 DIM i
 
 FOR i = 0 TO 4
   ? Funcs3[i](i)
 NEXT

Result:


 0
 1
 4
 9
 16

Example 5:


 DIM y(s$,j$) AS SUB
 
 y = xx : CALL xx ( "hello"   , " world" )  '  It is OKAY to use the CALL  keyword

 y = zz : zz ( "goodbye" , " cruel world" )  '  Using the CALL keyword is not required

 SUB xx (a$, j$)
   PRINT
   PRINT UCASE$(a$),UCASE$(j$), ":-)"
 END SUB
 
 SUB zz (a$, j$)
   PRINT
   PRINT MCASE$(a$),MCASE$(j$)," :-("
 END SUB

Result:


 HELLO WORLD:-)

 Goodbye Cruel World :-(

See also Dimensioning Pointer variables

BCX Console Sample Programs using Pointers.

S71.bas, S83.bas, S90.bas, S124.bas, S150.bas.