”;
A function is a block of reusable code that can be called anywhere in your program. This eliminates the need of writing the same code again and again. It helps programmers in writing modular codes.
Functions allow a programmer to divide a big program into a number of small and manageable functions.
In general, using JavaScript, we can define two types of functions – named functions, the regular functions with function name body and, Function expressions. Using function expressions, we can assign functions to variables.
//named function function sayHello(){ return("Hello there"); } //function expressions var message = function sayHello(){ return("Hello there"); }
Functions in CoffeeScript
The syntax of function in CoffeeScript is simpler as compared to JavaScript. In CoffeeScript, we define only function expressions.
The function keyword is eliminated in CoffeeScript. To define a function here, we have to use a thin arrow (->).
Behind the scenes, the CoffeeScript compiler converts the arrow in to the function definition in JavaScript as shown below.
(function() {});
It is not mandatory to use the return keyword in CoffeeScript. Every function in CoffeeScript returns the last statement in the function automatically.
-
If we want to return to the calling function or return a value before we reach the end of the function, then we can use the return keyword.
-
In addition to in-line functions (functions that are in single line), we can also define multiline functions in CoffeeScript. Since the curly braces are eliminated, we can do it by maintaining proper indentations.
Defining a Function
Following is the syntax of defining a function in CoffeeScript.
function_name = -> function_body
Example
Given below is an example of a function in CoffeeScript. In here, we have created a function named greet. This function automatically returns the statement in it. Save it in a file with the name function_example.coffee
greet = -> "This is an example of a function"
Compile it by executing the following command in the command prompt.
c:>coffee -c function_example.coffee
On compiling, it generates the following JavaScript code. Here you can observe that the CoffeeScript compiler automatically returned the string value in the function named greet().
// Generated by CoffeeScript 1.10.0 (function() { var greet; greet = function() { return "This is an example of a function"; }; }).call(this);
Multi-line Functions
We can also define a function with multiple lines by maintaining indentations instead of curly braces. But we have to be consistent with the indentation we follow for a line throughout a function.
greet = -> console.log "Hello how are you"
On compiling, the above CoffeeScript gives you the following JavaScript code. The CoffeeScript compiler grabs the body of the function that we have separated using indentations and placed within the curly braces.
// Generated by CoffeeScript 1.10.0 (function() { var greet; greet = function() { return console.log("Hello how are you"); }; }).call(this);
Functions with Arguments
We can also specify arguments in a function using parenthesis as shown below.
add =(a,b) -> c=a+b console.log "Sum of the two numbers is: "+c
On compiling the above CoffeeScript file, it will generate the following JavaScript.
// Generated by CoffeeScript 1.10.0 (function() { var add; add = function(a, b) { var c; c = a + b; return console.log("Sum of the two numbers is: " + c); }; }).call(this);
Invoking a Function
After defining a function, we need to invoke that function. You can simply invoke a function by placing parenthesis after its name as shown in the following example.
add = -> a=20;b=30 c=a+b console.log "Sum of the two numbers is: "+c add()
On compiling, the above example gives you the following JavaScript
// Generated by CoffeeScript 1.10.0 (function() { var add; add = function() { var a, b, c; a = 20; b = 30; c = a + b; return console.log("Sum of the two numbers is: " + c); }; add(); }).call(this);
On executing the above CoffeeScript code, it generates the following output.
Sum of the two numbers is: 50
Invoking Functions with Arguments
In the same way, we can invoke a function with arguments by passing them to it as shown below.
my_function argument_1,argument_2 or my_function (argument_1,argument_2)
Note − While invoking a function by passing arguments to it, the usage of parenthesis is optional.
In the following example, we have created a function named add() that accepts two parameters and we have invoked it.
add =(a,b) -> c=a+b console.log "Sum of the two numbers is: "+c add 10,20
On compiling, the above example gives you the following JavaScript.
// Generated by CoffeeScript 1.10.0 (function() { var add; add = function(a, b) { var c; c = a + b; return console.log("Sum of the two numbers is: " + c); }; add(10, 20); }).call(this);
On executing, the above CoffeeScript code it generates the following output.
Sum of the two numbers is: 30
Default Arguments
CoffeeScript supports default arguments too. We can assign default values to the arguments of a function, as shown in the following example.
add =(a = 1, b = 2) -> c=a+b console.log "Sum of the two numbers is: "+c add 10,20 #Calling the function with default arguments add()
On compiling, the above CoffeeScript generates the following JavaScript file.
// Generated by CoffeeScript 1.10.0 (function() { var add; add = function(a, b) { var c; if (a == null) { a = 1; } if (b == null) { b = 2; } c = a + b; return console.log("Sum of the two numbers is: " + c); }; add(10, 20); add() }).call(this);
On executing the above CoffeeScript code, it generates the following output.
Sum of the two numbers is: 30 Sum of the two numbers is: 3
”;