SlideShare a Scribd company logo
1 of 11
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 JavaScriptMark Casias
 
Introduction to WordPress Hooks 2016
Introduction to WordPress Hooks 2016Introduction to WordPress Hooks 2016
Introduction to WordPress Hooks 2016Ian 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 locoGemma 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 Experimentslacyrhoades
 
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 finalMurray 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.jsChris Saylor
 
Experiments in Reasoning
Experiments in ReasoningExperiments in Reasoning
Experiments in ReasoningAslam Khan
 
Deck 6-456 (1)
Deck 6-456 (1)Deck 6-456 (1)
Deck 6-456 (1)Justin Ezor
 
Python in the land of serverless
Python in the land of serverlessPython in the land of serverless
Python in the land of serverlessDavid Przybilla
 
Python for scientific computing
Python for scientific computingPython for scientific computing
Python for scientific computingGo Asgard
 
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 ProgrammersJeff 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

Alluxio Monthly Webinar | Cloud-Native Model Training on Distributed Data
Alluxio Monthly Webinar | Cloud-Native Model Training on Distributed DataAlluxio Monthly Webinar | Cloud-Native Model Training on Distributed Data
Alluxio Monthly Webinar | Cloud-Native Model Training on Distributed DataAlluxio, Inc.
 
Unit 1.1 Excite Part 1, class 9, cbse...
Unit 1.1 Excite Part 1, class 9, cbse...Unit 1.1 Excite Part 1, class 9, cbse...
Unit 1.1 Excite Part 1, class 9, cbse...aditisharan08
 
Optimizing AI for immediate response in Smart CCTV
Optimizing AI for immediate response in Smart CCTVOptimizing AI for immediate response in Smart CCTV
Optimizing AI for immediate response in Smart CCTVshikhaohhpro
 
chapter--4-software-project-planning.ppt
chapter--4-software-project-planning.pptchapter--4-software-project-planning.ppt
chapter--4-software-project-planning.pptkotipi9215
 
BATTLEFIELD ORM: TIPS, TACTICS AND STRATEGIES FOR CONQUERING YOUR DATABASE
BATTLEFIELD ORM: TIPS, TACTICS AND STRATEGIES FOR CONQUERING YOUR DATABASEBATTLEFIELD ORM: TIPS, TACTICS AND STRATEGIES FOR CONQUERING YOUR DATABASE
BATTLEFIELD ORM: TIPS, TACTICS AND STRATEGIES FOR CONQUERING YOUR DATABASEOrtus Solutions, Corp
 
Project Based Learning (A.I).pptx detail explanation
Project Based Learning (A.I).pptx detail explanationProject Based Learning (A.I).pptx detail explanation
Project Based Learning (A.I).pptx detail explanationkaushalgiri8080
 
Asset Management Software - Infographic
Asset Management Software - InfographicAsset Management Software - Infographic
Asset Management Software - InfographicHr365.us smith
 
The Essentials of Digital Experience Monitoring_ A Comprehensive Guide.pdf
The Essentials of Digital Experience Monitoring_ A Comprehensive Guide.pdfThe Essentials of Digital Experience Monitoring_ A Comprehensive Guide.pdf
The Essentials of Digital Experience Monitoring_ A Comprehensive Guide.pdfkalichargn70th171
 
Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...
Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...
Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...harshavardhanraghave
 
Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...
Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...
Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...MyIntelliSource, Inc.
 
TECUNIQUE: Success Stories: IT Service provider
TECUNIQUE: Success Stories: IT Service providerTECUNIQUE: Success Stories: IT Service provider
TECUNIQUE: Success Stories: IT Service providermohitmore19
 
5 Signs You Need a Fashion PLM Software.pdf
5 Signs You Need a Fashion PLM Software.pdf5 Signs You Need a Fashion PLM Software.pdf
5 Signs You Need a Fashion PLM Software.pdfWave PLM
 
Engage Usergroup 2024 - The Good The Bad_The Ugly
Engage Usergroup 2024 - The Good The Bad_The UglyEngage Usergroup 2024 - The Good The Bad_The Ugly
Engage Usergroup 2024 - The Good The Bad_The UglyFrank van der Linden
 
ODSC - Batch to Stream workshop - integration of Apache Spark, Cassandra, Pos...
ODSC - Batch to Stream workshop - integration of Apache Spark, Cassandra, Pos...ODSC - Batch to Stream workshop - integration of Apache Spark, Cassandra, Pos...
ODSC - Batch to Stream workshop - integration of Apache Spark, Cassandra, Pos...Christina Lin
 
Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...
Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...
Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...MyIntelliSource, Inc.
 
Cloud Management Software Platforms: OpenStack
Cloud Management Software Platforms: OpenStackCloud Management Software Platforms: OpenStack
Cloud Management Software Platforms: OpenStackVICTOR MAESTRE RAMIREZ
 
The Ultimate Test Automation Guide_ Best Practices and Tips.pdf
The Ultimate Test Automation Guide_ Best Practices and Tips.pdfThe Ultimate Test Automation Guide_ Best Practices and Tips.pdf
The Ultimate Test Automation Guide_ Best Practices and Tips.pdfkalichargn70th171
 
Advancing Engineering with AI through the Next Generation of Strategic Projec...
Advancing Engineering with AI through the Next Generation of Strategic Projec...Advancing Engineering with AI through the Next Generation of Strategic Projec...
Advancing Engineering with AI through the Next Generation of Strategic Projec...OnePlan Solutions
 
What is Binary Language? Computer Number Systems
What is Binary Language?  Computer Number SystemsWhat is Binary Language?  Computer Number Systems
What is Binary Language? Computer Number SystemsJheuzeDellosa
 

Recently uploaded (20)

Alluxio Monthly Webinar | Cloud-Native Model Training on Distributed Data
Alluxio Monthly Webinar | Cloud-Native Model Training on Distributed DataAlluxio Monthly Webinar | Cloud-Native Model Training on Distributed Data
Alluxio Monthly Webinar | Cloud-Native Model Training on Distributed Data
 
Unit 1.1 Excite Part 1, class 9, cbse...
Unit 1.1 Excite Part 1, class 9, cbse...Unit 1.1 Excite Part 1, class 9, cbse...
Unit 1.1 Excite Part 1, class 9, cbse...
 
Optimizing AI for immediate response in Smart CCTV
Optimizing AI for immediate response in Smart CCTVOptimizing AI for immediate response in Smart CCTV
Optimizing AI for immediate response in Smart CCTV
 
chapter--4-software-project-planning.ppt
chapter--4-software-project-planning.pptchapter--4-software-project-planning.ppt
chapter--4-software-project-planning.ppt
 
BATTLEFIELD ORM: TIPS, TACTICS AND STRATEGIES FOR CONQUERING YOUR DATABASE
BATTLEFIELD ORM: TIPS, TACTICS AND STRATEGIES FOR CONQUERING YOUR DATABASEBATTLEFIELD ORM: TIPS, TACTICS AND STRATEGIES FOR CONQUERING YOUR DATABASE
BATTLEFIELD ORM: TIPS, TACTICS AND STRATEGIES FOR CONQUERING YOUR DATABASE
 
Project Based Learning (A.I).pptx detail explanation
Project Based Learning (A.I).pptx detail explanationProject Based Learning (A.I).pptx detail explanation
Project Based Learning (A.I).pptx detail explanation
 
Asset Management Software - Infographic
Asset Management Software - InfographicAsset Management Software - Infographic
Asset Management Software - Infographic
 
The Essentials of Digital Experience Monitoring_ A Comprehensive Guide.pdf
The Essentials of Digital Experience Monitoring_ A Comprehensive Guide.pdfThe Essentials of Digital Experience Monitoring_ A Comprehensive Guide.pdf
The Essentials of Digital Experience Monitoring_ A Comprehensive Guide.pdf
 
Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...
Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...
Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...
 
Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...
Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...
Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...
 
TECUNIQUE: Success Stories: IT Service provider
TECUNIQUE: Success Stories: IT Service providerTECUNIQUE: Success Stories: IT Service provider
TECUNIQUE: Success Stories: IT Service provider
 
5 Signs You Need a Fashion PLM Software.pdf
5 Signs You Need a Fashion PLM Software.pdf5 Signs You Need a Fashion PLM Software.pdf
5 Signs You Need a Fashion PLM Software.pdf
 
Engage Usergroup 2024 - The Good The Bad_The Ugly
Engage Usergroup 2024 - The Good The Bad_The UglyEngage Usergroup 2024 - The Good The Bad_The Ugly
Engage Usergroup 2024 - The Good The Bad_The Ugly
 
ODSC - Batch to Stream workshop - integration of Apache Spark, Cassandra, Pos...
ODSC - Batch to Stream workshop - integration of Apache Spark, Cassandra, Pos...ODSC - Batch to Stream workshop - integration of Apache Spark, Cassandra, Pos...
ODSC - Batch to Stream workshop - integration of Apache Spark, Cassandra, Pos...
 
Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...
Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...
Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...
 
Cloud Management Software Platforms: OpenStack
Cloud Management Software Platforms: OpenStackCloud Management Software Platforms: OpenStack
Cloud Management Software Platforms: OpenStack
 
The Ultimate Test Automation Guide_ Best Practices and Tips.pdf
The Ultimate Test Automation Guide_ Best Practices and Tips.pdfThe Ultimate Test Automation Guide_ Best Practices and Tips.pdf
The Ultimate Test Automation Guide_ Best Practices and Tips.pdf
 
Advancing Engineering with AI through the Next Generation of Strategic Projec...
Advancing Engineering with AI through the Next Generation of Strategic Projec...Advancing Engineering with AI through the Next Generation of Strategic Projec...
Advancing Engineering with AI through the Next Generation of Strategic Projec...
 
What is Binary Language? Computer Number Systems
What is Binary Language?  Computer Number SystemsWhat is Binary Language?  Computer Number Systems
What is Binary Language? Computer Number Systems
 
Call Girls In Mukherjee Nagar 📱 9999965857 🤩 Delhi 🫦 HOT AND SEXY VVIP 🍎 SE...
Call Girls In Mukherjee Nagar 📱  9999965857  🤩 Delhi 🫦 HOT AND SEXY VVIP 🍎 SE...Call Girls In Mukherjee Nagar 📱  9999965857  🤩 Delhi 🫦 HOT AND SEXY VVIP 🍎 SE...
Call Girls In Mukherjee Nagar 📱 9999965857 🤩 Delhi 🫦 HOT AND SEXY VVIP 🍎 SE...
 

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.