written in response to this question on Twitter, but hopefully this is helpful to someone, somewhere, in some small way.
Rather than cover what a function is and how it works, this example dives right into examples to illustrate the basics. If you need to do some research first, just remember: StackOverflow and Google are your best friends. Just take your time, and whenever you're ready this example will be right here waiting for you!
Let's get started!
Remember algrebra, as in 3 * x = 6
? Let's create a function that is capable of calculating a known value, 3
against an unknown value, x
:
function calculate(x) {
// we'll use the variable `result` to capture
// the calculated value
var result = 3 * x;
}
// Pretty simple, right? We should get 15:
console.log(calculate(5))
//=> undefined
What?!?
Undefined?! Oh noooo! We expected to see 15
but we got undefined
!
This is because result
is trapped inside the function! In order to see the result of the function, we'll need to show it the way out by returning it.
In other words, we need to return
the value:
function calculate(x) {
var result = 3 * x;
return result;
}
console.log(calculate(5))
//=> 15
Whoohooo!
Ahhh. Good times. Anyway, what do you say we shorten this up a bit and get rid of that result
variable? Sometimes you need a variable, sometimes you don't.
function calculate(x) {
return 3 * x;
}
console.log(calculate(5))
//=> 15
As useful as our calculate
function is, I think we can do better. Let's go above and beyond the (e.g. "my") average node.js module and make create a function to do something truly extraorinary! Let's make it... do something!
Ready?! Let's create a couple of functions to convert from one temperature scale to another.
First, from Fahrenheit to Celsius:
function toCelsius(number) {
return ((number - 32) * 5) / 9;
}
console.log(toCelsius(32));
//=> 0
and now from... what? oh sorry, I know you can read:
function toFahrenheit(number) {
return ((number * 9) / 5) + 32;
}
console.log(toFahrenheit(32));
//=> 89.6
Now, for fun (and illustrative purposes), let's try wrapping our two functions in a third function that will return either Fahrenheit or Celsius, depending on the value passed as the second argument (e.g. c
or f
):
function temperature(number, type) {
// provide a default value for the scale we
// want to output e.g. Celsius or Fahrenheit
type = type || 'f';
if (type === 'f') {
return toFahrenheit(number);
} else {
return toCelsius(number);
}
}
console.log(temperature(32, 'c'));
// => 0
Last, let's try to make the output slightly more informative to the user by appending the scale, c
or f
, to the calculated value:
function temperature(number, type) {
type = type || 'f';
if (type === 'f') {
// Here, we're adding a number to a string.
// This works because the number is first
// coerced to a string, which means we won't
// be able to operate on the number any further.
// This isn't usually a good thing to do, but
// it works well for this example.
return toFahrenheit(number) + type;
} else {
return toCelsius(number) + type;
}
}
// assume the number is Fahrenheit and convert it to Celsius
console.log(temperature(32, 'c'));
//=> 0c
// assume the number is Celsius and convert it to Fahrenheit
console.log(temperature(32, 'f'));
//=> 89.6f
Hope this helps!
Very nice! Much appreciated. This is a great first app to build if you're new to programming (like myself)!