FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
kernelscript/examples/functions.ks at main · multikernel/kernelscript · GitHub
multikernel
/
kernelscript
Public
Notifications
You must be signed in to change notification settings
Fork
24
Star
508
Code
Issues
2
Pull requests
0
Actions
Projects
Security and quality
0
Insights
Additional navigation options
Code
Issues
Pull requests
Actions
Projects
Security and quality
Insights
Expand file tree
Breadcrumbs
kernelscript
/
examples
/
functions.ks
Copy path
More file actions
More file actions
Latest commit
History
History
History
62 lines (47 loc) · 1.37 KB
Breadcrumbs
kernelscript
/
examples
/
functions.ks
Copy path
File metadata and controls
62 lines (47 loc) · 1.37 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
include
"xdp.kh"
type IpAddress
=
u32
@helper
fn helper_function
(
value: u32
)
-
>
u32 {
return value
+
10
}
@helper
fn another_helper
()
-
>
u32 {
return
42
}
@xdp fn test_functions
(
ctx:
*
xdp_md
)
-
>
xdp_action {
var result
=
helper_function
(
5
)
var const_val
=
another_helper
()
return XDP_PASS
}
fn global_function
(
x: u32
)
-
>
u32 {
return x
*
2
}
fn add_numbers
(
a: i32, b: i32
)
-
>
i32 {
return a
+
b
}
fn multiply_numbers
(
a: i32, b: i32
)
-
>
i32 {
return a
*
b
}
fn subtract_numbers
(
a: i32, b: i32
)
-
>
i32 {
return a
-
b
}
fn process_with_callback
(
x: i32, y: i32, callback: fn
(
i32, i32
)
-
>
i32
)
-
>
i32 {
return callback
(
x, y
)
}
// Function pointer type declaration
type BinaryOp
=
fn
(
i32, i32
)
-
>
i32
fn main
()
-
>
i32 {
var result
=
global_function
(
21
)
// Assign functions to function pointers
var add_op: BinaryOp
=
add_numbers
var mul_op: BinaryOp
=
multiply_numbers
var sub_op: BinaryOp
=
subtract_numbers
// Call functions through function pointers
var sum
=
add_op
(
10
,
20
)
// Result: 30
var product
=
mul_op
(
5
,
6
)
// Result: 30
var difference
=
sub_op
(
15
,
7
)
// Result: 8
// Higher-order function with function pointer parameter
var callback_result
=
process_with_callback
(
4
,
7
, add_numbers
)
// Result: 11
var callback_result2
=
process_with_callback
(
4
,
7
, multiply_numbers
)
// Result: 28
return
0
}
Back
|
FazBrowse Home
|
New Git URL