How to declare a function
Functionsallow you to package up lines of code that you can use (and often reuse) in your programs.
Sometimes they takeparameterslike the pizza button from the beginning of this lesson.reheatPizza()
had one parameter: the number of slices.
function reheatPizza(numSlices) {
// code that figures out reheat settings!
}
ThereverseString()
function that you saw also had one parameter: the string to be reversed.
function reverseString(reverseMe) {
// code to reverse a string!
}
In both cases, the parameter is listed as a variable after the function name, inside the parentheses. And, if there were multiple parameters, you would just separate them with commas.
function doubleGreeting(name, otherName) {
// code to greet two people!
}
But, you can also have functions that don't have any parameters. Instead, they just package up some code and perform some task. In this case, you would just leave the parentheses empty. Take this one for example. Here's a simple function that just prints out"Hello!"
.
// accepts no parameters! parentheses are empty
function sayHello() {
var message = "Hello!"
console.log(message);
}
If you tried pasting any of the functions above into the JavaScript console, you probably didn't notice much happen. In fact, you probably sawundefined
returned back to you.undefined
is the default return value on the console when nothing is_explicitly_returned using the specialreturn
keyword.
Return statements
In thesayHello()
function above, a value isprintedto the console withconsole.log
, but not explicitly returned with areturn statement. You can write a return statement by using thereturn
keyword followed by the expression or value that you want to return.
// declares the sayHello function
function sayHello() {
var message = "Hello!"
return message; // returns value instead of printing it
}
How to run a function
Now, to get your function todo something, you have toinvoke or call the function using the function name, followed by parentheses with anyargumentsthat are passed into it. Functions are like machines. You can build the machine, but it won't do anything unless you also turn it on. Here's how you would call thesayHello()
function from before, and then use the return value to print to the console:
// declares the sayHello function
function sayHello() {
var message = "Hello!"
return message; // returns value instead of printing it
}
// function returns "Hello!" and console.log prints the return value
console.log(sayHello());
Prints:"Hello!"
Parameters vs. Arguments
At first, it can be a bit tricky to know when something is either a parameter or an argument. The key difference is in where they show up in the code. Aparameter is always going to be avariable_name and appears in the function declaration. On the other hand, anargumentis always going to be a_value(i.e. any of the JavaScript data types - a number, a string, a boolean, etc.) and will always appear in the code when the function is called or invoked.