Functions
In a ‘function,’ you place code that you want to use several times. You write the code once and then run it as often as you want.
Important question: Can CoffeeScript actually make coffee?
Sure it can.
# First we create the function
makeCoffee = ->
print "Grind beans."
print "Boil water until it steams."
print "Add water to grounds."
print "Wait a while."
print "Filter grounds from coffee."
print "Pour in a cup."
print "Done! Here you go: ☕️"
Note: Yes, you can use emojis, but only inside strings. If we could use them in variable names I would have written make☕️ = ->
.
# And then we ‘call’ the function
makeCoffee()
» "Grind beans."
» "Boil water until it steams."
» "Add water to grounds."
» "Wait a while."
» "Filter grounds from coffee."
» "Pour in a cup."
» "Done! Here you go: ☕️"
Next to a number, Boolean value, or text string, a variable can also contain a function; makeCoffee
is a variable that contains a function.
That little ->
arrow, a.k.a. the ‘dash rocket,’ creates a function, so the first line says:
In the variable makeCoffee I’m putting a function, and it contains this:
Which you follow with the code that should run; in this example, it’s only print
statements.
Ah, by the way, did you want milk with that?
Functions with arguments
With an argument you can provide a function with extra info.
makeCoffee = (withMilk) ->
print "Grind beans, add water, wait, filter…"
print "Pour coffee in a cup."
if withMilk is yes
print "Add a dash of milk."
print "Done! Here you go: ☕️"
You give a function the ability to receive this extra info by adding an ‘argument’ (sometimes also called a ‘parameter’) in parentheses ()
before the dash rocket.
The name you picked, withMilk
, becomes a variable that you can use inside the function.
When using (a.k.a. ‘calling’) the function you pass it the value you want to use (yes
, in this case) by placing it after the function’s name.
makeCoffee yes
» "Grind beans, add water, wait, filter…"
» "Pour coffee in a cup."
» "Add a dash of milk."
» "Done! Here you go: ☕️"
That’s not obligatory, though: you can omit it, and withMilk
will then simply be empty. But remember to add parentheses then, so that the function gets called. (It’s obvious that you’re calling it when you add a value.)
makeCoffee()
» "Grind beans, add water, wait, filter…"
» "Pour coffee in a cup."
» "Done! Here you go: ☕️"
And sugar?
makeCoffee = (withMilk, sugarAmount) ->
print "Grind beans, add water, wait, filter…"
print "Pour coffee in a cup."
if withMilk is yes
print "Add a dash of milk."
if sugarAmount is 1
print "Add a spoon of sugar."
else if sugarAmount > 1
print "Add #{sugarAmount} spoons of sugar."
print "Done! Here you go: ☕️"
makeCoffee yes,2
» "Grind beans, add water, wait, filter…"
» "Pour coffee in a cup."
» "Add a dash of milk."
» "Add 2 spoons of sugar."
» "Done! Here you go: ☕️"
Functions can have multiple arguments, but when calling the function, you have to follow the order in which they were defined.
So if you only want sugar, you will have to add a value for withMilk
as well:
makeCoffee()
# Black as the night
makeCoffee 2
# Still black, ‘withMilk’ should be a Boolean
makeCoffee yes
# With milk
makeCoffee yes,5
# With milk, and 5 spoons of sugar
makeCoffee no,5
# 5 spoons of sugar, and only sugar