Skip to content

Function Pointers

IsaacShelton edited this page Mar 21, 2022 · 1 revision

Function Pointers

Function pointers can be used for callbacks or other dynamically determined function calls

Function Pointer Types

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

Obtaining Function Pointers

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

Calling Functions by Pointer

Function pointers can be called just like normal functions

result int = combining_function(8, 13)

See function pointer calls for more information

Usage Example

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

Clone this wiki locally