Functions

A function is a group of statements that together perform a task.

Function defination
Defining a Function starts with keyword func. The form of a function definition in Silk is as follows:
func function_name( parameter )
{
   body
}

func:keyword func, any function should start with func
function_name:the name of the function, the name should be unique, and should not be the same as any other functions/variables and keywords.
parameter:A parameter is like a placeholder. When a function is invoked, you pass a value to the parameter. This value is referred to as actual parameter or argument. parameter is optional.
body:The body contains a collection of statements that define what the function does.

Calling function
To use a function, you will need to call that function.
You can call a defined function in main() function or any other functions, the function should 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 is to use arguments, it must declare variables that accept the values of the arguments. These variables are called the formal parameters.
While calling a function, there are 2 ways in which arguments can be passed to a function:
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 argument.
Silk will pass Integer, Float, String, Boolean, Handle and null by 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 will not copy the object and will improve the 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 Value
We 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 variable
The function name can be used as variable. The type of the variable is HANDLE, it can be passed as argument, and can be used as 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