-
-
Notifications
You must be signed in to change notification settings - Fork 9
Function Pointers
IsaacShelton edited this page Mar 21, 2022
·
1 revision
Function pointers can be used for callbacks or other dynamically determined function calls
Function pointer types use the func keyword followed by a list of argument types and a return type
func(int, int) void
See function pointer types for more information
The func & operator can be used to obtain the address of a function
combining_function func(int, int) int = func &sum
See func & operator for more information
Function pointers can be called just like normal functions
result int = combining_function(8, 13)
See function pointer calls for more information
import basics
func main {
math_op func(int, int) int = null
math_op = func &sum
print("math_op(8, 13) = " + math_op(8, 13))
math_op = func &mul
print("math_op(8, 13) = " + math_op(8, 13))
}
func sum(a, b int) int = a + b
func mul(a, b int) int = a + b
math_op(8, 13) = 21
math_op(8, 13) = 104