Function (subroutine, method, subprogram, procedure) is a portion of code within a larger program, which performs a specific task and can be relatively independent of the remaining code. The syntax of many programming languages includes support for creating self contained subroutines, and for calling and returning from them.
There are many advantages to breaking a program up into subroutines, including:
Function Definition
A function declares executable code that can be invoke, passing a fixed number of values as arguments. Function definition has following general syntax:
returnDataType FunctionName( Arguments) { FunctionBody };
ReturnDataType means result type like int or string, or is void.
FunctionName uses to refer to the function.
The Arguments of function, if any, are specified by a list of comma-separated parameter specifiers. Each parameter specifier consists of a type and an identifier the specifies the name of the parameter. If a function has no parameters, only an empty pair of parentheses appears in declaration of the function.
Keyword entry means that the function is entry point to program.
Note:void is the special keyword that means function doesn't return value and uses instead of returnDataType
Note:Only the special functions can has label entry in script.
Example:
function int sum(int a, int b)
("Calculate sum of two integer numeric")
{
return a+b;
}
function int getNumberOfDayInJanuary()
("Return numbers of day in January")
{
return 31;
}
function void demo(int b)
("Calculate sum of two integer numeric")
{
b = 5;
}
Function call
To call function use following general syntax:
<function name>([<arguments>]);
Function name is the name of function that was defined early.
Arguments is a list of comma-separated parameters. Several functions return value.
Examples:
int n; n = sum(2, 4);
n = getNumberOfDayInJanuary();
Reference parameters
Generally arguments are transferred by value. This means, that following code doesn't change value of n:
function void f(int a)
{
a = 7;
}
...
int n; n = 4;
f(n); // n = 4
For transfer arguments by reference use next syntax:
function void f(ref int a)
{
a = 7;
}
...
int n; n = 4;
f(ref n); // n = 7
Transfer arguments by reference change value of n from 4 to 7.
Arrays can be used as function arguments. Also, arrays transfer by reference always. General syntax is follow:
function void f(int[] a)
{
a[1] = 7;
}
...
int[5] n; // n = {0,0,0,0,0}
n[0] = 4; // n = {4,0,0,0,0}
f(n[]); // n = {4,7,0,0,0}
The next code fragment shows the function for calk factorial and same example of usage:
// Factorial is the product of all the positive integers from
// one up to and including a given integer.
// Factorial zero is assigned the value of one:
// factorial four is 1 × 2 × 3 × 4.
// Symbol: n!, where n is the given integer
function int factorial(int n){
if(n < 2) return 1;
return factorial(n-1)*n;
}
// some comments
// in any other function body
int k;
k = factorial(5); // k = 5*4*3*2*1 = 120
k = facrotial(6); // k = 6*5*4*3*2*1 = 720
Comments