Pointers in BCX

FUNCTION and SUB Pointers

Example 1:


 GLOBAL Func(INTEGER) AS FUNCTION INTEGER

 CALL SetFunc(x0)
 ? Func(2)
 CALL SetFunc(x1)
 ? Func(2)
 getchar()

 SUB SetFunc(f(n AS INTEGER) AS FUNCTION INTEGER)
  Func = f
 END SUB

 '-----------------------------------------------

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

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

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

 SET(PTR Funcs2[])() AS CHAR PTR
  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

 SET(PTR Funcs3[])(INTEGER) AS INTEGER
  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
  ? Funcs[i]()
 NEXT

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

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

Example 3:


 DIM y(s$,j$) AS SUB

 y = zz

 SUB zz(a$, j$)
 END SUB

See also Dimensioning Pointer variables

BCX Console Sample Programs using Pointers.

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