Functions and methods are conceptually largely the same thing, and in terms of the Sling syntax, both are declared using the "func" keyword and can be referred to by using the "function" data type. A function is declared using the following syntax:

func <functionName> [(parameter1Name as DataType, parameter2Name as DataType, ..)]
	[modifiers] [as <DataType>] [custom modifiers]
{
	// function body goes here
}

The data type of the return value of a function or method is specified by the "as" clause that follows the parameters. If the "as" clause is not specified, then the function is assumed to be "void" (returns no value). It is not necessary to explicitly declare the method as void. Likewise, if the function takes no parameters, there is no need to type the parenthesis as part of the declaration. Therefore, to declare a function that takes no parameters and returns no value, the following syntax is valid:

func doSomething
{
}

When placed inside a class or interface, a function is by default considered public, and there is no need to type "public" explicitly. To declare a private or protected function, use the appropriate modifiers:

func doSomethingPrivate private
{
}

func doSomethingProtected protected
{
}

Twitter Facebook LinkedIn Youtube Slideshare Github