- Function Basics
- Arguments
- Default Arguments
- Splats...
- Wrapping Up
Default Arguments
In some languages, such as Ruby, it is possible to assign default values to arguments. This means that should you not need to pass in some arguments, for whatever reason, then reasonable default values can be used in their place.
Let’s revisit our calculator example again here. Let’s write it so that the tax rate is set to a default value should one not be passed in:
Example: (source: default_args.coffee)
calculateTotal = (sub_total, rate = 0.05) -> tax = sub_total * rate sub_total + tax console.log calculateTotal 100, 0.0625 console.log calculateTotal 100
Example: (source: default_args.js)
(function() { var calculateTotal; calculateTotal = function(sub_total, rate) { var tax; if (rate == null) rate = 0.05; tax = sub_total * rate; return sub_total + tax; }; console.log(calculateTotal(100, 0.0625)); console.log(calculateTotal(100)); }).call(this);
Output: (source: default_args.coffee)
106.25 105
When defining our function we told CoffeeScript to set the default value of the tax_rate argument equal to 0.05. When we first call the calculateTotal function we pass in a tax_rate argument of 0.0625 and the second time we omit the tax_rate argument altogether, and the code does the appropriate thing and uses 0.05 in its place.
We can take default arguments a step further and have them refer to other arguments. Take this example:
Example: (source: default_args_referring.coffee)
href = (text, url = text) -> html = "<a href='#{url}'>#{text}</a>" return html console.log href("Click Here", "http://www.example.com") console.log href("http://www.example.com")
Example: (source: default_args_referring.js)
(function() { var href; href = function(text, url) { var html; if (url == null) url = text; html = "<a href='" + url + "'>" + text + "</a>"; return html; }; console.log(href("Click Here", "http://www.example.com")); console.log(href("http://www.example.com")); }).call(this);
Output: (source: default_args_referring.coffee)
<a href='http://www.example.com'>Click Here</a> <a href='http://www.example.com'>http://www.example.com</a>
Should no one pass in the url argument in our example we will set it equal to the text argument that was passed in.
It is also possible to use functions as default values in the argument list. Since the default value will only be called if there is no argument passed in there is no concern in terms of performance.
Example: (source: default_args_with_function.coffee)
defaultRate = -> 0.05 calculateTotal = (sub_total, rate = defaultRate()) -> tax = sub_total * rate sub_total + tax console.log calculateTotal 100, 0.0625 console.log calculateTotal 100
Example: (source: default_args_with_function.js)
(function() { var calculateTotal, defaultRate; defaultRate = function() { return 0.05; }; calculateTotal = function(sub_total, rate) { var tax; if (rate == null) rate = defaultRate(); tax = sub_total * rate; return sub_total + tax; }; console.log(calculateTotal(100, 0.0625)); console.log(calculateTotal(100)); }).call(this);
Output: (source: default_args_with_function.coffee)
106.25 105