-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvararg_functions.nut
More file actions
59 lines (40 loc) · 1.33 KB
/
vararg_functions.nut
File metadata and controls
59 lines (40 loc) · 1.33 KB
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
// Variable Arguments Script in Squirrel 2.2
// Tested with Raw Squirrel - Video tutorial: https://youtu.be/GnNkZliWOQk
/*
Key notes:
vargc - Holds the length for the pseudo-array (variable argument)
vargv[i] - Access the individual index where i is the index to look at/for
Variable Arguments in Squirrel are denoted as 3 dots (...), which should be at the last in the argument/parameters
VALID: function DoSmth(name, age, ...)
INVALID: function DoSmth(..., name, age)
Squirrel cannot tell at runtime how long (in theory) the vararg could be, this is why it is declared at the end
~ TheE7Player
*/
function Add(...)
{
local total = 0;
for(local i = 0; i< vargc; i++) { total += vargv[i]; }
::print("Add sums up to: " + total + "\n")
}
function Sub(...)
{
local total = 0;
for(local i = 0; i< vargc; i++) { total -= vargv[i]; }
::print("Sub sums up to: " + total + "\n")
}
function printl(...)
{
local string = "";
for(local i = 0; i < vargc; i++) { string += vargv[i]; }
string += "\n";
::print(string)
}
Add(1,2,3,4,5,6,7,8,9,10)
Sub(1,2,3,4,5,6,7,8,9,10)
printl("Hello: ", "James", "! ( 5 + 3 ) * 20 would be: ", (5+3)*20, "!")
/*
OUTPUT:
Add sums up to: 50
Sub sums up to: -50
Hello: James! ( 5 + 3 ) * 20 would be: 160!
*/