function Summary Declares a function with a return type of the external type. Syntax function name([[type1] param1 [, [type2] param2 ... [, [...] [type3] param3]]]) { statement1; statement2; statement3; } Parameters name The name of the function. This can be any valid identifier. Optional for function expressions. typeN The type to restrict the parameter to. paramN The name for the parameter. This can be any valid identifier. Optionally, a rest parameter can be created if the parameter is A) the last parameter and B) is prefixed with ... statementN The statement(s) to execute when the function is called. Description The function keyword will declare a function with a return type that is the external type. For more information, see the documentation on functions. Arguments Object Functions declared in JS++ do not automatically declare an arguments object (as in JavaScript) — even when using the function keyword. Instead, the arguments object must be declared manually. This can be done with variadic parameters: 12345678import System; function foo(...arguments) { Console.log(arguments[0]); Console.log(arguments[1]);} foo("just", "like", "JavaScript", true); 'this' for Free Functions In free functions, the this keyword used inside function retains JavaScript semantics. The following table lists how the this keyword can change for function: Description Example 'this' Value Method Call someObject.someMethod(); someObject Free Function Call foo(); global (undefined in ES5+ strict mode) Instantiating Constructor Function new Foo(); The instantiated object eval eval("this.foo"); Value of this where eval was called Using Function.prototype.apply foo.apply(thisObj, [ arg1, arg2, argN ]); thisObj Using Function.prototype.call foo.call(thisObj, arg1, arg2, argN); thisObj Please note that the above table does not apply when the return type for your function is int, string, or any return type that is not function. 'this' inside Classes The this keyword - when used inside function - has different semantics in the context of classes. By default, it refers to the class instance rather than retaining JavaScript semantics. However, via casting, JavaScript semantics can be preserved. For more information, refer to the this documentation. Properties Functions declared with function support arbitrary properties. However, functions declared with an internal return type do not support arbitrary properties. Thus, prototypical inheritance is possible on functions declared with function but not for other internal functions. Type Refinement Functions that can convert to external and are assigned to var in a variable declaration will have their types refined due to this being a common pattern in JavaScript. 12var foo = function() {};var bar = function(int x) {}; In the above snippet, foo's type is refined to function() and bar's type is refined to function(int). This enables compile-time analysis on the functions. If you want the variable to retain the external type, cast the function expression to external: 12var foo = (external) function() {};var bar = (external) function(int x) {}; See Also Function Declarations external keyword external type var return this Scoping Closures Function Overloading External Links Understanding JavaScript's this keyword Share HTML | BBCode | Direct Link