SlideShare a Scribd company logo
FUNCTIONS
W H AT Y O U N E E D TO K N O W I N T H E B E G I N N I N G .
… A N D M E T H O D S
BREAK INTO TECH SUMMIT
- WED, JANUARY 11TH
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
•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.
FUNTION SYNTAX
function name( arguments ) {
statements;
}
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.
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();
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!
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.
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’);
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”

More Related Content

Similar to Functions

Spiffy Applications With JavaScript
Spiffy Applications With JavaScriptSpiffy Applications With JavaScript
Spiffy Applications With JavaScript
Mark Casias
 
Introduction to WordPress Hooks 2016
Introduction to WordPress Hooks 2016Introduction to WordPress Hooks 2016
Introduction to WordPress Hooks 2016
Ian Wilson
 
Scala, just a better java?
Scala, just a better java?Scala, just a better java?
Scala, just a better java?
Giampaolo Trapasso
 
Cómo tener analíticas en tu app y no volverte loco
Cómo tener analíticas en tu app y no volverte locoCómo tener analíticas en tu app y no volverte loco
Cómo tener analíticas en tu app y no volverte loco
Gemma Del Olmo
 
Mobile App Feature Configuration and A/B Experiments
Mobile App Feature Configuration and A/B ExperimentsMobile App Feature Configuration and A/B Experiments
Mobile App Feature Configuration and A/B Experiments
lacyrhoades
 
Python Homework Help
Python Homework HelpPython Homework Help
Python Homework Help
Programming Homework Help
 
Adding step subtitles to presentations within author tools final
Adding step subtitles to presentations within author tools   finalAdding step subtitles to presentations within author tools   final
Adding step subtitles to presentations within author tools final
Murray Fife
 
Build a Virtual Pet with JavaScript (May 2017, Santa Monica)
Build a Virtual Pet with JavaScript (May 2017, Santa Monica) Build a Virtual Pet with JavaScript (May 2017, Santa Monica)
Build a Virtual Pet with JavaScript (May 2017, Santa Monica)
Thinkful
 
Design for succcess with react and storybook.js
Design for succcess with react and storybook.jsDesign for succcess with react and storybook.js
Design for succcess with react and storybook.js
Chris Saylor
 
Experiments in Reasoning
Experiments in ReasoningExperiments in Reasoning
Experiments in Reasoning
Aslam Khan
 
Deck 6-456 (1)
Deck 6-456 (1)Deck 6-456 (1)
Deck 6-456 (1)
Justin Ezor
 
lect4
lect4lect4
lect4
lect4lect4
Python in the land of serverless
Python in the land of serverlessPython in the land of serverless
Python in the land of serverless
David Przybilla
 
Ifttt
IftttIfttt
Day 5
Day 5Day 5
Python for scientific computing
Python for scientific computingPython for scientific computing
Python for scientific computing
Go Asgard
 
Learning Svelte
Learning SvelteLearning Svelte
Learning Svelte
Christoffer Noring
 
Python 101
Python 101Python 101
How WebHooks Will Make Us All Programmers
How WebHooks Will Make Us All ProgrammersHow WebHooks Will Make Us All Programmers
How WebHooks Will Make Us All Programmers
Jeff Lindsay
 

Similar to Functions (20)

Spiffy Applications With JavaScript
Spiffy Applications With JavaScriptSpiffy Applications With JavaScript
Spiffy Applications With JavaScript
 
Introduction to WordPress Hooks 2016
Introduction to WordPress Hooks 2016Introduction to WordPress Hooks 2016
Introduction to WordPress Hooks 2016
 
Scala, just a better java?
Scala, just a better java?Scala, just a better java?
Scala, just a better java?
 
Cómo tener analíticas en tu app y no volverte loco
Cómo tener analíticas en tu app y no volverte locoCómo tener analíticas en tu app y no volverte loco
Cómo tener analíticas en tu app y no volverte loco
 
Mobile App Feature Configuration and A/B Experiments
Mobile App Feature Configuration and A/B ExperimentsMobile App Feature Configuration and A/B Experiments
Mobile App Feature Configuration and A/B Experiments
 
Python Homework Help
Python Homework HelpPython Homework Help
Python Homework Help
 
Adding step subtitles to presentations within author tools final
Adding step subtitles to presentations within author tools   finalAdding step subtitles to presentations within author tools   final
Adding step subtitles to presentations within author tools final
 
Build a Virtual Pet with JavaScript (May 2017, Santa Monica)
Build a Virtual Pet with JavaScript (May 2017, Santa Monica) Build a Virtual Pet with JavaScript (May 2017, Santa Monica)
Build a Virtual Pet with JavaScript (May 2017, Santa Monica)
 
Design for succcess with react and storybook.js
Design for succcess with react and storybook.jsDesign for succcess with react and storybook.js
Design for succcess with react and storybook.js
 
Experiments in Reasoning
Experiments in ReasoningExperiments in Reasoning
Experiments in Reasoning
 
Deck 6-456 (1)
Deck 6-456 (1)Deck 6-456 (1)
Deck 6-456 (1)
 
lect4
lect4lect4
lect4
 
lect4
lect4lect4
lect4
 
Python in the land of serverless
Python in the land of serverlessPython in the land of serverless
Python in the land of serverless
 
Ifttt
IftttIfttt
Ifttt
 
Day 5
Day 5Day 5
Day 5
 
Python for scientific computing
Python for scientific computingPython for scientific computing
Python for scientific computing
 
Learning Svelte
Learning SvelteLearning Svelte
Learning Svelte
 
Python 101
Python 101Python 101
Python 101
 
How WebHooks Will Make Us All Programmers
How WebHooks Will Make Us All ProgrammersHow WebHooks Will Make Us All Programmers
How WebHooks Will Make Us All Programmers
 

Recently uploaded

14 th Edition of International conference on computer vision
14 th Edition of International conference on computer vision14 th Edition of International conference on computer vision
14 th Edition of International conference on computer vision
ShulagnaSarkar2
 
Quarter 3 SLRP grade 9.. gshajsbhhaheabh
Quarter 3 SLRP grade 9.. gshajsbhhaheabhQuarter 3 SLRP grade 9.. gshajsbhhaheabh
Quarter 3 SLRP grade 9.. gshajsbhhaheabh
aisafed42
 
Baha Majid WCA4Z IBM Z Customer Council Boston June 2024.pdf
Baha Majid WCA4Z IBM Z Customer Council Boston June 2024.pdfBaha Majid WCA4Z IBM Z Customer Council Boston June 2024.pdf
Baha Majid WCA4Z IBM Z Customer Council Boston June 2024.pdf
Baha Majid
 
Webinar On-Demand: Using Flutter for Embedded
Webinar On-Demand: Using Flutter for EmbeddedWebinar On-Demand: Using Flutter for Embedded
Webinar On-Demand: Using Flutter for Embedded
ICS
 
Measures in SQL (SIGMOD 2024, Santiago, Chile)
Measures in SQL (SIGMOD 2024, Santiago, Chile)Measures in SQL (SIGMOD 2024, Santiago, Chile)
Measures in SQL (SIGMOD 2024, Santiago, Chile)
Julian Hyde
 
UI5con 2024 - Bring Your Own Design System
UI5con 2024 - Bring Your Own Design SystemUI5con 2024 - Bring Your Own Design System
UI5con 2024 - Bring Your Own Design System
Peter Muessig
 
J-Spring 2024 - Going serverless with Quarkus, GraalVM native images and AWS ...
J-Spring 2024 - Going serverless with Quarkus, GraalVM native images and AWS ...J-Spring 2024 - Going serverless with Quarkus, GraalVM native images and AWS ...
J-Spring 2024 - Going serverless with Quarkus, GraalVM native images and AWS ...
Bert Jan Schrijver
 
Malibou Pitch Deck For Its €3M Seed Round
Malibou Pitch Deck For Its €3M Seed RoundMalibou Pitch Deck For Its €3M Seed Round
Malibou Pitch Deck For Its €3M Seed Round
sjcobrien
 
Mobile App Development Company In Noida | Drona Infotech
Mobile App Development Company In Noida | Drona InfotechMobile App Development Company In Noida | Drona Infotech
Mobile App Development Company In Noida | Drona Infotech
Drona Infotech
 
GreenCode-A-VSCode-Plugin--Dario-Jurisic
GreenCode-A-VSCode-Plugin--Dario-JurisicGreenCode-A-VSCode-Plugin--Dario-Jurisic
GreenCode-A-VSCode-Plugin--Dario-Jurisic
Green Software Development
 
ALGIT - Assembly Line for Green IT - Numbers, Data, Facts
ALGIT - Assembly Line for Green IT - Numbers, Data, FactsALGIT - Assembly Line for Green IT - Numbers, Data, Facts
ALGIT - Assembly Line for Green IT - Numbers, Data, Facts
Green Software Development
 
Enums On Steroids - let's look at sealed classes !
Enums On Steroids - let's look at sealed classes !Enums On Steroids - let's look at sealed classes !
Enums On Steroids - let's look at sealed classes !
Marcin Chrost
 
E-Invoicing Implementation: A Step-by-Step Guide for Saudi Arabian Companies
E-Invoicing Implementation: A Step-by-Step Guide for Saudi Arabian CompaniesE-Invoicing Implementation: A Step-by-Step Guide for Saudi Arabian Companies
E-Invoicing Implementation: A Step-by-Step Guide for Saudi Arabian Companies
Quickdice ERP
 
The Key to Digital Success_ A Comprehensive Guide to Continuous Testing Integ...
The Key to Digital Success_ A Comprehensive Guide to Continuous Testing Integ...The Key to Digital Success_ A Comprehensive Guide to Continuous Testing Integ...
The Key to Digital Success_ A Comprehensive Guide to Continuous Testing Integ...
kalichargn70th171
 
Why Apache Kafka Clusters Are Like Galaxies (And Other Cosmic Kafka Quandarie...
Why Apache Kafka Clusters Are Like Galaxies (And Other Cosmic Kafka Quandarie...Why Apache Kafka Clusters Are Like Galaxies (And Other Cosmic Kafka Quandarie...
Why Apache Kafka Clusters Are Like Galaxies (And Other Cosmic Kafka Quandarie...
Paul Brebner
 
Oracle 23c New Features For DBAs and Developers.pptx
Oracle 23c New Features For DBAs and Developers.pptxOracle 23c New Features For DBAs and Developers.pptx
Oracle 23c New Features For DBAs and Developers.pptx
Remote DBA Services
 
如何办理(hull学位证书)英国赫尔大学毕业证硕士文凭原版一模一样
如何办理(hull学位证书)英国赫尔大学毕业证硕士文凭原版一模一样如何办理(hull学位证书)英国赫尔大学毕业证硕士文凭原版一模一样
如何办理(hull学位证书)英国赫尔大学毕业证硕士文凭原版一模一样
gapen1
 
Energy consumption of Database Management - Florina Jonuzi
Energy consumption of Database Management - Florina JonuziEnergy consumption of Database Management - Florina Jonuzi
Energy consumption of Database Management - Florina Jonuzi
Green Software Development
 
8 Best Automated Android App Testing Tool and Framework in 2024.pdf
8 Best Automated Android App Testing Tool and Framework in 2024.pdf8 Best Automated Android App Testing Tool and Framework in 2024.pdf
8 Best Automated Android App Testing Tool and Framework in 2024.pdf
kalichargn70th171
 
Unveiling the Advantages of Agile Software Development.pdf
Unveiling the Advantages of Agile Software Development.pdfUnveiling the Advantages of Agile Software Development.pdf
Unveiling the Advantages of Agile Software Development.pdf
brainerhub1
 

Recently uploaded (20)

14 th Edition of International conference on computer vision
14 th Edition of International conference on computer vision14 th Edition of International conference on computer vision
14 th Edition of International conference on computer vision
 
Quarter 3 SLRP grade 9.. gshajsbhhaheabh
Quarter 3 SLRP grade 9.. gshajsbhhaheabhQuarter 3 SLRP grade 9.. gshajsbhhaheabh
Quarter 3 SLRP grade 9.. gshajsbhhaheabh
 
Baha Majid WCA4Z IBM Z Customer Council Boston June 2024.pdf
Baha Majid WCA4Z IBM Z Customer Council Boston June 2024.pdfBaha Majid WCA4Z IBM Z Customer Council Boston June 2024.pdf
Baha Majid WCA4Z IBM Z Customer Council Boston June 2024.pdf
 
Webinar On-Demand: Using Flutter for Embedded
Webinar On-Demand: Using Flutter for EmbeddedWebinar On-Demand: Using Flutter for Embedded
Webinar On-Demand: Using Flutter for Embedded
 
Measures in SQL (SIGMOD 2024, Santiago, Chile)
Measures in SQL (SIGMOD 2024, Santiago, Chile)Measures in SQL (SIGMOD 2024, Santiago, Chile)
Measures in SQL (SIGMOD 2024, Santiago, Chile)
 
UI5con 2024 - Bring Your Own Design System
UI5con 2024 - Bring Your Own Design SystemUI5con 2024 - Bring Your Own Design System
UI5con 2024 - Bring Your Own Design System
 
J-Spring 2024 - Going serverless with Quarkus, GraalVM native images and AWS ...
J-Spring 2024 - Going serverless with Quarkus, GraalVM native images and AWS ...J-Spring 2024 - Going serverless with Quarkus, GraalVM native images and AWS ...
J-Spring 2024 - Going serverless with Quarkus, GraalVM native images and AWS ...
 
Malibou Pitch Deck For Its €3M Seed Round
Malibou Pitch Deck For Its €3M Seed RoundMalibou Pitch Deck For Its €3M Seed Round
Malibou Pitch Deck For Its €3M Seed Round
 
Mobile App Development Company In Noida | Drona Infotech
Mobile App Development Company In Noida | Drona InfotechMobile App Development Company In Noida | Drona Infotech
Mobile App Development Company In Noida | Drona Infotech
 
GreenCode-A-VSCode-Plugin--Dario-Jurisic
GreenCode-A-VSCode-Plugin--Dario-JurisicGreenCode-A-VSCode-Plugin--Dario-Jurisic
GreenCode-A-VSCode-Plugin--Dario-Jurisic
 
ALGIT - Assembly Line for Green IT - Numbers, Data, Facts
ALGIT - Assembly Line for Green IT - Numbers, Data, FactsALGIT - Assembly Line for Green IT - Numbers, Data, Facts
ALGIT - Assembly Line for Green IT - Numbers, Data, Facts
 
Enums On Steroids - let's look at sealed classes !
Enums On Steroids - let's look at sealed classes !Enums On Steroids - let's look at sealed classes !
Enums On Steroids - let's look at sealed classes !
 
E-Invoicing Implementation: A Step-by-Step Guide for Saudi Arabian Companies
E-Invoicing Implementation: A Step-by-Step Guide for Saudi Arabian CompaniesE-Invoicing Implementation: A Step-by-Step Guide for Saudi Arabian Companies
E-Invoicing Implementation: A Step-by-Step Guide for Saudi Arabian Companies
 
The Key to Digital Success_ A Comprehensive Guide to Continuous Testing Integ...
The Key to Digital Success_ A Comprehensive Guide to Continuous Testing Integ...The Key to Digital Success_ A Comprehensive Guide to Continuous Testing Integ...
The Key to Digital Success_ A Comprehensive Guide to Continuous Testing Integ...
 
Why Apache Kafka Clusters Are Like Galaxies (And Other Cosmic Kafka Quandarie...
Why Apache Kafka Clusters Are Like Galaxies (And Other Cosmic Kafka Quandarie...Why Apache Kafka Clusters Are Like Galaxies (And Other Cosmic Kafka Quandarie...
Why Apache Kafka Clusters Are Like Galaxies (And Other Cosmic Kafka Quandarie...
 
Oracle 23c New Features For DBAs and Developers.pptx
Oracle 23c New Features For DBAs and Developers.pptxOracle 23c New Features For DBAs and Developers.pptx
Oracle 23c New Features For DBAs and Developers.pptx
 
如何办理(hull学位证书)英国赫尔大学毕业证硕士文凭原版一模一样
如何办理(hull学位证书)英国赫尔大学毕业证硕士文凭原版一模一样如何办理(hull学位证书)英国赫尔大学毕业证硕士文凭原版一模一样
如何办理(hull学位证书)英国赫尔大学毕业证硕士文凭原版一模一样
 
Energy consumption of Database Management - Florina Jonuzi
Energy consumption of Database Management - Florina JonuziEnergy consumption of Database Management - Florina Jonuzi
Energy consumption of Database Management - Florina Jonuzi
 
8 Best Automated Android App Testing Tool and Framework in 2024.pdf
8 Best Automated Android App Testing Tool and Framework in 2024.pdf8 Best Automated Android App Testing Tool and Framework in 2024.pdf
8 Best Automated Android App Testing Tool and Framework in 2024.pdf
 
Unveiling the Advantages of Agile Software Development.pdf
Unveiling the Advantages of Agile Software Development.pdfUnveiling the Advantages of Agile Software Development.pdf
Unveiling the Advantages of Agile Software Development.pdf
 

Functions

  • 1. FUNCTIONS W H AT Y O U N E E D TO K N O W I N T H E B E G I N N I N G . … A N D M E T H O D S
  • 2. BREAK INTO TECH SUMMIT - WED, JANUARY 11TH
  • 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.
  • 5. FUNTION SYNTAX function name( arguments ) { statements; }
  • 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

  1. Because of the copy/paste formatting, for some reason, it doesn’t work, but if you type it out, it works.