Functions

A function is a set of instructions that perform a given action. They describe functionalities such as performing calculations, manipulating objects, using other functions, and more. Parameters can be used to make functions reusable. For example, a function to move objects can take the destination coordinates as parameters. When we call a function we will pass arguments for these parameters, and the function will perform its routine, being able to make use of these values. The syntax of a function in CoffeeScript is formed by a word, an equal symbol, a list of parameters between parentheses, a hyphen and a symbol of "greater than" forming an arrow, and the body of the function.

Here's an example of CoffeeScript code. If you come from JavaScript, you might find it strange to not come across any curly brackets to enclose the body of the function. In CoffeeScript, multiline blocks of instructions can be delimited by indentation:

doubleIfOdd = (x) ->
if x % 2 == 0
x * 2
else
x
print doubleIfOdd 4
Download prototype from Asset_A6462_A08_A01_Example_DoubleIfOdd.framer.zip.

You can also call the function without parentheses when the function has parameters. In the following code, you will see the result provided by a doubleIfOdd function in the Framer Studio print console (you can instead use console.log in order to see the output by the browser console).

>> 8

A function without parameters will be executed using parentheses, as you can see in the given example:

helloWorld = ->
"Hello world"
print helloWorld()
Download prototype from Asset_A6462_A08_A02_Example_HelloWorld.framer.zip.

As you can see, the helloWorld function returns a "Hello world" string that we will print using the print console.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.144.115.16