3. Follow Me
Twitter: @christopherhuie
LinkedIn: Chris Huie
Facebook: Chris Huie
Instagram: @huiestyle
Codepen: @ChrisHuie
Check Out My Meetups
Meetup: Learn to code Phoenix
Meetup: Learn data science Phoenix
Chris Huie
Developer Evangelist at Galvanize
Email: christopher.huie@gmail.com
Phone: 602-750-6878
4. â˘Functions are pretty much a set of instructions.
Thereâs really not much more to it than that.
â˘They are fantastic for keeping your code DRY
(Donât Repeat Yourself).
â˘Theyâre also used all over the Javascript syntax
in combination with other things, so they are
important to learn.
6. NAMED VS ANONYMOUS FUNCTIONS
function() {
return âsomethingâ + â interestingâ;
}
Named
function alertSomething() {
return alert(âsomethingâ);
}
alertSomething();
Anonymous
They do the same thing, but theyâre just referred to differently inside the code.
7. A FUNCTION IS LIKE A RECIPEâŚ
⢠You probably make sandwiches all the time and, if you donât, you should. Theyâre
delicious.
⢠Making a sandwich consists of, for the most part, taking two pieces of bread, piling
things on the bottom piece of bread, then putting on the top piece.
⢠For the scope of this conversation, weâre going to assume that all sandwiches are
composed of two things: bread and some combination of ingredients.
⢠NOTE: This is partially pseudo-code.function
makePB&JSandwich() {
add PB&J
add top bread piece
return sandwich
}
makePB&JSandwich();
8. SANDWICHESâŚPART 2
⢠While that first function that we saw would be somewhat useful, letâs try to make
something a little more dynamic or, rather, useful in more than one instance.
⢠Letâs make a function that letâs you create a sandwich with mayo and any kind of meat
that you want. The key here is, how do we tell the function what kind of meat to addâŚ
function
makeSandwich(meat) {
add mayo
add meat
add top bread piece
return sandwich
}
makeSandwich(âturkeyâ);
⢠Notice that we now have something inside the parentheses called âmeatâ. That is
called a parameter. That means that whatever you put inside the parentheses is going
to be what the âmeatâ reference next to âaddâ will turn into. The function call below
says âturkeyâ, so you just added mayo, turkey, added the top bread piece, and returned
the sandwich!
9. SANDWICHESâŚPART 3
⢠Letâs make a function that letâs you create a sandwich with any kind of spread that you
want, too. The key here is, how do we tell the function what kind of spread to addâŚ
function makeSandwich(meat,
spread) {
add spread
add meat
add top bread piece
return sandwich
}
makeSandwich(âturkeyâ, âmustardâ);
⢠Notice that we now have something inside the parentheses called âspreadâ. All you
have to do now is replace that with whatever spread you want, be it mayo, mustard, or
whatever. Is it really that easy, you ask? Pretty much.
10. REAL CODE EXAMPLES
⢠Ok, now that we gave you an idea of what happens in a function, letâs take a look at
what you might see in a more real life example. Letâs do an example with no
parameters. In this function, weâre going to fire an alert that says âsomethingâ.
function alertSomething() {
return alert(âsomethingâ);
}
alertSomething();
⢠What if we want to tell the function what exactly to alert? How do we do that?
function alertSomethingInteresting(str) {
return alert(str);
}
alertSomethingInteresting(âsomething interestingâ);
11. YOU MENTIONED âMETHODSââŚ
⢠SoâŚit depends on who you talk to, but functionally (pun intended), functions and
methods are the same, theyâre just in a different place. Methods are functions that are
attached to objects and functions are what theyâre called everywhere else.
var person = {};
person.first_name = âChrisâ;
person.last_name = âHuieâ;
person.full_name = function() {
return person.first_name + â â + person.last_name;
}
person.full_name(); // ==> âChris Huieâ
Editor's Notes
Because of the copy/paste formatting, for some reason, it doesnât work, but if you type it out, it works.