Functions
A function is a group of statements that together perform a task.
Function definition
A function definition starts with the keyword func. The basic form of a function definition in Silk is as follows:
func function_name( parameter )
{
body
}
func:the keyword used to start a function definition
function_name:the name of the function. It should be unique and
must not be the same as any other function, variable, or keyword.
parameter:A parameter is like a placeholder. When a function is
invoked, you pass a value to the parameter. This value is called an
actual parameter or argument. The parameter list is optional.
body:The body contains the statements that define what the function does.
Calling a function
To use a function, you need to call it.
You can call a defined function in the main() function or in any other function, but it must be defined before you call it.
function sample:
//define a function to perform a task which can return the greater number
func max(num1, num2)
{
result=0;
if (num1 > num2)
result = num1;
else
result = num2;
return result;
}
main()
{
ret=max(99,101);//call the function
print("the max number is ", ret);//print the result
}
Function arguments
If a function uses arguments, it must declare variables that
receive the values of those arguments. These variables are called
formal parameters.
When calling a function, arguments can be passed in two ways:
Call by value: This method copies the actual value of an argument into the formal parameter of the function.
In this case, changes made to the parameter inside the function have no effect on the original argument.
Silk passes Integer, Float, String, Boolean, Handle, and null values using call by value.
Call by value sample:
func test_param(str)
{
str="I'm cat";
print(str);
}
main()
{
str="I'm dog";
print(str);
test_param(str);
print(str);//str is not changed.
}
Result:
I'm dog
I'm cat
I'm dog
Call by reference: This method copies the address of an argument into the formal parameter.
This means that changes made to the parameter affect the argument.
Silk will
pass Array, Dictionary and Class object by using call by reference.
Call by reference avoids copying the object and can improve
performance.
Call by reference sample:
func test_param(array)
{
array[0]=99;
}
main()
{
arr=[1,2];
print(arr[0]);
test_param(arr);
print(arr[0]);//the value in arr was changed
}
Result:
1
99
If
you need to prevent the value in object from being changed, you can
copy the object by using built-in function _copy before passing it:
func test_param(array)
{
array[0]=99;
print(array[0]);
}
main()
{
arr=[1,2];
print(arr[0]);
arr2=_copy(arr);
test_param(arr2);
print(arr[0]);//we passed the new object arr2, so the original object arr will not be changed
return;
}
Result:
1
99
1
Default Parameter ValueWe
can set the default value for the parameter when we define a function.
If a parameter is set with a default argument, all subsequent
parameters must have a default argument supplied as well.
If we call the function without argument, it uses the default value:
func test_param(x,y=100)
{
z=x+y;
printf("%d+%d=%d\n",x,y,z);
}
main()
{
test_param(1,2);
test_param(1);
}
Result:
1+2=3
1+100=101
Function variableThe
function name can also be used as a variable. The type of the variable is
HANDLE, it can be passed as an argument, and it can be used as a callback
function:
func test(x)
{
printf("%d\n",x);
}
func test2(f)
{
f(200);
}
main()
{
f=test;//the function name is assigned to f as a variable
print(_type(f));
f(100);//call the function, it's equtal to test(100);
test2(f);//pass the function variable as argument
}
Result:
HANDLE
100
200