SlideShare a Scribd company logo
Introduction to Programming
with JavaScript
Week 2: Function
Jeongbae Oh

YCC JavaScript Seminar

2017.09.25
Function
• The most important part of JavaScript

• A "mini program" within the program

• Basis of the functional programming paradigm
Input / Output
• A function can receive inputs, process them, and return an output.

• But it doesn't have to get an input.

• Or return an output.

• Or process anything.

• Of course a function not doing any of the three is pretty meaningless.

• Put simply, the input of a function is called a parameter (매개변수), and 

the output is called a return value (반환값, 리턴값)
Declaration
• function name(parameters) { } 

• No semi-colon necessary

• Function naming rules (same as variable)

• Must consist of lower and upper case alphabet letters, numbers, and _

• Can only begin with lower and upper case alphabet letters

• Cannot use reserved words (e.g. function, var, etc.)

• Lower camel case recommended (e.g. getTaxRate) → Convention

• Function without a name is called an anonymous function (익명 함수).
Call / Invocation
• To execute codes within a function, it must be called (invoked).

• To call a function: name(argument);

• A function can be declared and called 

immediately (immediately invoked function):

• (function name(parameter) { })(); 

• Anonymous functions are usually called immediately.
Call / Invocation
• Location of a function within a source code has no effect on
whether the function can be called.

(i.e. you can call a function before defining it)

• Not calling a function makes that function to have no effect
on the execution of the code.
return
• A function finishes running with return.

• Anything after return is not executed.

• Even without return, function finishes running at the end.

• Value after return becomes the return value of the function.

• Return value is not necessary.
return and console.log
• return makes the result of execution of the function available for use as a value,
and therefore not usually "printed" like console.log in a real setting.

• console.log is a special function/method which "prints" the value to the
console/REPL to make debugging easy.

• Therefore, the return value of console.log() is undefined (nothing is
returned).
console.log does not 

have a return value.
console.log
return
console.log
return
Parameter / Argument
• To put simply:

• A parameter (매개변수) is the input of a function.

• An argument (인자) is what is passed to a function when called.
parameters
arguments
Function as a First-Class Citizen
• A function can be passed to
another function as an
argument.

• A function can be returned by
another function.

• A function can be assigned to a
variable (a function expression)
Scope
• Scope (범위) of a function
is defined by the portion
encompassed by braces
(block).

• Each function has its
own scope.
Nested Scope
• A function can have another function
within itself, which is called a nested
function.

• Function inside can access values in
the function outside.

(i.e. manipulate them without initializing)

• This means that if a variable is assigned
a different value within the inner
function, it changes the value for the
outer function as well.
Nested Scope
• However, if a variable is first initialized
and changed value within the inner
function, that change only takes effect
within that inner function.

• In other words, if the value in the outer
function's scope should not be changed,

1) Initialize the variable

2) Use a different variable name
Nested Scope
• If the outer function does not
directly call the inner function (i.e.
only returns the "function object"),
the latter should be called together
when the outer is called such as:

name()();
• To put simply, since the return
value of outer() is inner,
outer()() can be understood as
outer() + inner().
Multiple-Nested Scope
• Characteristics of nested scope apply to more-than-double-nested scope.

• The below two have the same results, but different approaches to nested
function structure.
Global Scope
• Scope outside of all functions is
called global scope.

• Global scope acts like a "global
function" that is defined and
executed automatically by
JavaScript interpreter itself.

• Characteristics of the global scope
is identical to the outermost function
in the nested function structure.
Closure
• A value/variable defined in the outside function can be accessed
by the inside function(s) without being explicitly defined in the
inside function.

• The reverse does not hold (i.e. the outer function cannot access
values of the inner function).
Closure Not a closure
Closure
• Closure makes programming easier by allowing variables to passed to inner
functions without precise declarations.

• Without closure, parameters/arguments throughout the entirety of the function
need to be matched.
Using closure Not using closure
Stack
• In JavaScript, information is stored in
memory as a "stack."

• LIFO (last-in, first-out): value stored
last is taken out first

• Put very very simply, values are
"pushed" into stack when defined
within a function, and "popped" from
stack when the function returns or
finishes running.
https://upload.wikimedia.org/wikipedia/commons/b/b4/Lifo_stack.png
How Stack Works
• outer() is called and pushed to the stack.

• inner() is called and pushed to the stack.

• inner() is executed, returns, and popped
from the stack.

• outer() is executed, returns, and popped
from the stack.

• Stack is emptied at the end.
* Please note that this is NOT actual way stack works; just an illustration
How Stack Works
• outer() is called and pushed to the
stack.

• inner() is called and pushed to the
stack.

• inner() is executed, returns, and
popped from the stack.

• outer() is executed, returns, and
popped from the s tack.

• Stack is emptied at the end.
* Please note that this is NOT actual way stack works; just an illustration
Recursion
• Calling the function within itself is called recursion (재귀).

• Recursion is an "elegant" way to code, but is inefficient because
it uses much more memory than non-recursive way.

• Example: the Fibonacci sequence

• 1 + 1 + 2 + 3 + 5 + 8 + ...

• Try fib(1000). 

How does it work?
Stack Overflow
• Since memory is limited, size of stack
is limited as well. If a program creates
stack larger than the memory space, it
can no longer run. This is called stack
overflow.

• Recursion is the easiest way to cause
stack overflow, if it is not stopped at a
proper time.

• Stack Overflow is also the name of the
most popular developer community. 

(뇌가 stack overflow 되었을 때 찾아오라는
뜻?)

More Related Content

What's hot (20)

Client sidescripting javascript
Client sidescripting javascriptClient sidescripting javascript
Client sidescripting javascript
Selvin Josy Bai Somu
 
C++ Functions
C++ FunctionsC++ Functions
C++ Functions
Jari Abbas
 
Functional programming in scala
Functional programming in scalaFunctional programming in scala
Functional programming in scala
Stratio
 
Notes: Verilog Part 5 - Tasks and Functions
Notes: Verilog Part 5 - Tasks and FunctionsNotes: Verilog Part 5 - Tasks and Functions
Notes: Verilog Part 5 - Tasks and Functions
Jay Baxi
 
Functional programming and ruby in functional style
Functional programming and ruby in functional styleFunctional programming and ruby in functional style
Functional programming and ruby in functional style
Niranjan Sarade
 
Ruby Functional Programming
Ruby Functional ProgrammingRuby Functional Programming
Ruby Functional Programming
Geison Goes
 
FUNCTION CPU
FUNCTION CPUFUNCTION CPU
FUNCTION CPU
Krushal Kakadia
 
Rdbms chapter 1 function
Rdbms chapter 1 functionRdbms chapter 1 function
Rdbms chapter 1 function
dipumaliy
 
Function class in c++
Function class in c++Function class in c++
Function class in c++
Kumar
 
Function overloading in c++
Function overloading in c++Function overloading in c++
Function overloading in c++
Learn By Watch
 
Functional JavaScript Fundamentals
Functional JavaScript FundamentalsFunctional JavaScript Fundamentals
Functional JavaScript Fundamentals
Srdjan Strbanovic
 
Functional programming with Java 8
Functional programming with Java 8Functional programming with Java 8
Functional programming with Java 8
LivePerson
 
Think in linq
Think in linqThink in linq
Think in linq
Sudipta Mukherjee
 
Function different types of funtion
Function different types of funtionFunction different types of funtion
Function different types of funtion
svishalsingh01
 
Functions, anonymous functions and the function type
Functions, anonymous functions and the function typeFunctions, anonymous functions and the function type
Functions, anonymous functions and the function type
Chang John
 
Functional programming with Ruby - can make you look smart
Functional programming with Ruby - can make you look smartFunctional programming with Ruby - can make you look smart
Functional programming with Ruby - can make you look smart
Chen Fisher
 
Introduction to c first week slides
Introduction to c first week slidesIntroduction to c first week slides
Introduction to c first week slides
luqman bawany
 
Functional programming with Java 8
Functional programming with Java 8Functional programming with Java 8
Functional programming with Java 8
Talha Ocakçı
 
Java script function
Java script functionJava script function
Java script function
suresh raj sharma
 
Functions in c++
Functions in c++Functions in c++
Functions in c++
Rokonuzzaman Rony
 
Functional programming in scala
Functional programming in scalaFunctional programming in scala
Functional programming in scala
Stratio
 
Notes: Verilog Part 5 - Tasks and Functions
Notes: Verilog Part 5 - Tasks and FunctionsNotes: Verilog Part 5 - Tasks and Functions
Notes: Verilog Part 5 - Tasks and Functions
Jay Baxi
 
Functional programming and ruby in functional style
Functional programming and ruby in functional styleFunctional programming and ruby in functional style
Functional programming and ruby in functional style
Niranjan Sarade
 
Ruby Functional Programming
Ruby Functional ProgrammingRuby Functional Programming
Ruby Functional Programming
Geison Goes
 
Rdbms chapter 1 function
Rdbms chapter 1 functionRdbms chapter 1 function
Rdbms chapter 1 function
dipumaliy
 
Function class in c++
Function class in c++Function class in c++
Function class in c++
Kumar
 
Function overloading in c++
Function overloading in c++Function overloading in c++
Function overloading in c++
Learn By Watch
 
Functional JavaScript Fundamentals
Functional JavaScript FundamentalsFunctional JavaScript Fundamentals
Functional JavaScript Fundamentals
Srdjan Strbanovic
 
Functional programming with Java 8
Functional programming with Java 8Functional programming with Java 8
Functional programming with Java 8
LivePerson
 
Function different types of funtion
Function different types of funtionFunction different types of funtion
Function different types of funtion
svishalsingh01
 
Functions, anonymous functions and the function type
Functions, anonymous functions and the function typeFunctions, anonymous functions and the function type
Functions, anonymous functions and the function type
Chang John
 
Functional programming with Ruby - can make you look smart
Functional programming with Ruby - can make you look smartFunctional programming with Ruby - can make you look smart
Functional programming with Ruby - can make you look smart
Chen Fisher
 
Introduction to c first week slides
Introduction to c first week slidesIntroduction to c first week slides
Introduction to c first week slides
luqman bawany
 
Functional programming with Java 8
Functional programming with Java 8Functional programming with Java 8
Functional programming with Java 8
Talha Ocakçı
 

Similar to Intro to JavaScript - Week 2: Function (20)

Javascripts hidden treasures BY - https://geekyants.com/
Javascripts hidden treasures            BY  -  https://geekyants.com/Javascripts hidden treasures            BY  -  https://geekyants.com/
Javascripts hidden treasures BY - https://geekyants.com/
Geekyants
 
Web 4 | Core JavaScript
Web 4 | Core JavaScriptWeb 4 | Core JavaScript
Web 4 | Core JavaScript
Mohammad Imam Hossain
 
Web development basics (Part-4)
Web development basics (Part-4)Web development basics (Part-4)
Web development basics (Part-4)
Rajat Pratap Singh
 
11_Functions_Introduction.pptx javascript notes
11_Functions_Introduction.pptx javascript notes11_Functions_Introduction.pptx javascript notes
11_Functions_Introduction.pptx javascript notes
tayyabbiswas2025
 
JavaScript Introductin to Functions
JavaScript Introductin to FunctionsJavaScript Introductin to Functions
JavaScript Introductin to Functions
Charles Russell
 
Wt unit 5
Wt unit 5Wt unit 5
Wt unit 5
team11vgnt
 
Lecture 4- Javascript Function presentation
Lecture 4- Javascript Function presentationLecture 4- Javascript Function presentation
Lecture 4- Javascript Function presentation
GomathiUdai
 
JavaScript Interview Questions 2023
JavaScript Interview Questions 2023JavaScript Interview Questions 2023
JavaScript Interview Questions 2023
Laurence Svekis ✔
 
1669958779195.pdf
1669958779195.pdf1669958779195.pdf
1669958779195.pdf
venud11
 
CS101- Introduction to Computing- Lecture 29
CS101- Introduction to Computing- Lecture 29CS101- Introduction to Computing- Lecture 29
CS101- Introduction to Computing- Lecture 29
Bilal Ahmed
 
Comprehensive JavaScript Cheat Sheet for Quick Reference and Mastery
Comprehensive JavaScript Cheat Sheet for Quick Reference and MasteryComprehensive JavaScript Cheat Sheet for Quick Reference and Mastery
Comprehensive JavaScript Cheat Sheet for Quick Reference and Mastery
pavanbackup22
 
JavaScript Cheatsheets with easy way .pdf
JavaScript Cheatsheets with easy way .pdfJavaScript Cheatsheets with easy way .pdf
JavaScript Cheatsheets with easy way .pdf
ranjanadeore1
 
JavaScript - Chapter 6 - Basic Functions
 JavaScript - Chapter 6 - Basic Functions JavaScript - Chapter 6 - Basic Functions
JavaScript - Chapter 6 - Basic Functions
WebStackAcademy
 
The mighty js_function
The mighty js_functionThe mighty js_function
The mighty js_function
timotheeg
 
Js scope
Js scopeJs scope
Js scope
santhosh kumar.s sampath.v
 
Javascript foundations: scope
Javascript foundations: scopeJavascript foundations: scope
Javascript foundations: scope
John Hunter
 
Let's JavaScript
Let's JavaScriptLet's JavaScript
Let's JavaScript
Paweł Dorofiejczyk
 
Function Returns
Function ReturnsFunction Returns
Function Returns
primeteacher32
 
The JavaScript Programming Primer
The JavaScript  Programming PrimerThe JavaScript  Programming Primer
The JavaScript Programming Primer
Mike Wilcox
 
JS OO and Closures
JS OO and ClosuresJS OO and Closures
JS OO and Closures
Jussi Pohjolainen
 
Javascripts hidden treasures BY - https://geekyants.com/
Javascripts hidden treasures            BY  -  https://geekyants.com/Javascripts hidden treasures            BY  -  https://geekyants.com/
Javascripts hidden treasures BY - https://geekyants.com/
Geekyants
 
Web development basics (Part-4)
Web development basics (Part-4)Web development basics (Part-4)
Web development basics (Part-4)
Rajat Pratap Singh
 
11_Functions_Introduction.pptx javascript notes
11_Functions_Introduction.pptx javascript notes11_Functions_Introduction.pptx javascript notes
11_Functions_Introduction.pptx javascript notes
tayyabbiswas2025
 
JavaScript Introductin to Functions
JavaScript Introductin to FunctionsJavaScript Introductin to Functions
JavaScript Introductin to Functions
Charles Russell
 
Lecture 4- Javascript Function presentation
Lecture 4- Javascript Function presentationLecture 4- Javascript Function presentation
Lecture 4- Javascript Function presentation
GomathiUdai
 
JavaScript Interview Questions 2023
JavaScript Interview Questions 2023JavaScript Interview Questions 2023
JavaScript Interview Questions 2023
Laurence Svekis ✔
 
1669958779195.pdf
1669958779195.pdf1669958779195.pdf
1669958779195.pdf
venud11
 
CS101- Introduction to Computing- Lecture 29
CS101- Introduction to Computing- Lecture 29CS101- Introduction to Computing- Lecture 29
CS101- Introduction to Computing- Lecture 29
Bilal Ahmed
 
Comprehensive JavaScript Cheat Sheet for Quick Reference and Mastery
Comprehensive JavaScript Cheat Sheet for Quick Reference and MasteryComprehensive JavaScript Cheat Sheet for Quick Reference and Mastery
Comprehensive JavaScript Cheat Sheet for Quick Reference and Mastery
pavanbackup22
 
JavaScript Cheatsheets with easy way .pdf
JavaScript Cheatsheets with easy way .pdfJavaScript Cheatsheets with easy way .pdf
JavaScript Cheatsheets with easy way .pdf
ranjanadeore1
 
JavaScript - Chapter 6 - Basic Functions
 JavaScript - Chapter 6 - Basic Functions JavaScript - Chapter 6 - Basic Functions
JavaScript - Chapter 6 - Basic Functions
WebStackAcademy
 
The mighty js_function
The mighty js_functionThe mighty js_function
The mighty js_function
timotheeg
 
Javascript foundations: scope
Javascript foundations: scopeJavascript foundations: scope
Javascript foundations: scope
John Hunter
 
The JavaScript Programming Primer
The JavaScript  Programming PrimerThe JavaScript  Programming Primer
The JavaScript Programming Primer
Mike Wilcox
 
Ad

Recently uploaded (20)

Scaling GenAI Inference From Prototype to Production: Real-World Lessons in S...
Scaling GenAI Inference From Prototype to Production: Real-World Lessons in S...Scaling GenAI Inference From Prototype to Production: Real-World Lessons in S...
Scaling GenAI Inference From Prototype to Production: Real-World Lessons in S...
Anish Kumar
 
Domino IQ – What to Expect, First Steps and Use Cases
Domino IQ – What to Expect, First Steps and Use CasesDomino IQ – What to Expect, First Steps and Use Cases
Domino IQ – What to Expect, First Steps and Use Cases
panagenda
 
Your startup on AWS - How to architect and maintain a Lean and Mean account J...
Your startup on AWS - How to architect and maintain a Lean and Mean account J...Your startup on AWS - How to architect and maintain a Lean and Mean account J...
Your startup on AWS - How to architect and maintain a Lean and Mean account J...
angelo60207
 
Developing Schemas with FME and Excel - Peak of Data & AI 2025
Developing Schemas with FME and Excel - Peak of Data & AI 2025Developing Schemas with FME and Excel - Peak of Data & AI 2025
Developing Schemas with FME and Excel - Peak of Data & AI 2025
Safe Software
 
MCP vs A2A vs ACP: Choosing the Right Protocol | Bluebash
MCP vs A2A vs ACP: Choosing the Right Protocol | BluebashMCP vs A2A vs ACP: Choosing the Right Protocol | Bluebash
MCP vs A2A vs ACP: Choosing the Right Protocol | Bluebash
Bluebash
 
Improving Developer Productivity With DORA, SPACE, and DevEx
Improving Developer Productivity With DORA, SPACE, and DevExImproving Developer Productivity With DORA, SPACE, and DevEx
Improving Developer Productivity With DORA, SPACE, and DevEx
Justin Reock
 
Cybersecurity Fundamentals: Apprentice - Palo Alto Certificate
Cybersecurity Fundamentals: Apprentice - Palo Alto CertificateCybersecurity Fundamentals: Apprentice - Palo Alto Certificate
Cybersecurity Fundamentals: Apprentice - Palo Alto Certificate
VICTOR MAESTRE RAMIREZ
 
Data Virtualization: Bringing the Power of FME to Any Application
Data Virtualization: Bringing the Power of FME to Any ApplicationData Virtualization: Bringing the Power of FME to Any Application
Data Virtualization: Bringing the Power of FME to Any Application
Safe Software
 
ISOIEC 42005 Revolutionalises AI Impact Assessment.pptx
ISOIEC 42005 Revolutionalises AI Impact Assessment.pptxISOIEC 42005 Revolutionalises AI Impact Assessment.pptx
ISOIEC 42005 Revolutionalises AI Impact Assessment.pptx
AyilurRamnath1
 
Compliance-as-a-Service document pdf text
Compliance-as-a-Service document pdf textCompliance-as-a-Service document pdf text
Compliance-as-a-Service document pdf text
Earthling security
 
The case for on-premises AI
The case for on-premises AIThe case for on-premises AI
The case for on-premises AI
Principled Technologies
 
Extend-Microsoft365-with-Copilot-agents.pptx
Extend-Microsoft365-with-Copilot-agents.pptxExtend-Microsoft365-with-Copilot-agents.pptx
Extend-Microsoft365-with-Copilot-agents.pptx
hoang971
 
Jira Administration Training – Day 1 : Introduction
Jira Administration Training – Day 1 : IntroductionJira Administration Training – Day 1 : Introduction
Jira Administration Training – Day 1 : Introduction
Ravi Teja
 
What is Oracle EPM A Guide to Oracle EPM Cloud Everything You Need to Know
What is Oracle EPM A Guide to Oracle EPM Cloud Everything You Need to KnowWhat is Oracle EPM A Guide to Oracle EPM Cloud Everything You Need to Know
What is Oracle EPM A Guide to Oracle EPM Cloud Everything You Need to Know
SMACT Works
 
Palo Alto Networks Cybersecurity Foundation
Palo Alto Networks Cybersecurity FoundationPalo Alto Networks Cybersecurity Foundation
Palo Alto Networks Cybersecurity Foundation
VICTOR MAESTRE RAMIREZ
 
Establish Visibility and Manage Risk in the Supply Chain with Anchore SBOM
Establish Visibility and Manage Risk in the Supply Chain with Anchore SBOMEstablish Visibility and Manage Risk in the Supply Chain with Anchore SBOM
Establish Visibility and Manage Risk in the Supply Chain with Anchore SBOM
Anchore
 
Your startup on AWS - How to architect and maintain a Lean and Mean account
Your startup on AWS - How to architect and maintain a Lean and Mean accountYour startup on AWS - How to architect and maintain a Lean and Mean account
Your startup on AWS - How to architect and maintain a Lean and Mean account
angelo60207
 
Introduction to Typescript - GDG On Campus EUE
Introduction to Typescript - GDG On Campus EUEIntroduction to Typescript - GDG On Campus EUE
Introduction to Typescript - GDG On Campus EUE
Google Developer Group On Campus European Universities in Egypt
 
DevOps in the Modern Era - Thoughtfully Critical Podcast
DevOps in the Modern Era - Thoughtfully Critical PodcastDevOps in the Modern Era - Thoughtfully Critical Podcast
DevOps in the Modern Era - Thoughtfully Critical Podcast
Chris Wahl
 
Create Your First AI Agent with UiPath Agent Builder
Create Your First AI Agent with UiPath Agent BuilderCreate Your First AI Agent with UiPath Agent Builder
Create Your First AI Agent with UiPath Agent Builder
DianaGray10
 
Scaling GenAI Inference From Prototype to Production: Real-World Lessons in S...
Scaling GenAI Inference From Prototype to Production: Real-World Lessons in S...Scaling GenAI Inference From Prototype to Production: Real-World Lessons in S...
Scaling GenAI Inference From Prototype to Production: Real-World Lessons in S...
Anish Kumar
 
Domino IQ – What to Expect, First Steps and Use Cases
Domino IQ – What to Expect, First Steps and Use CasesDomino IQ – What to Expect, First Steps and Use Cases
Domino IQ – What to Expect, First Steps and Use Cases
panagenda
 
Your startup on AWS - How to architect and maintain a Lean and Mean account J...
Your startup on AWS - How to architect and maintain a Lean and Mean account J...Your startup on AWS - How to architect and maintain a Lean and Mean account J...
Your startup on AWS - How to architect and maintain a Lean and Mean account J...
angelo60207
 
Developing Schemas with FME and Excel - Peak of Data & AI 2025
Developing Schemas with FME and Excel - Peak of Data & AI 2025Developing Schemas with FME and Excel - Peak of Data & AI 2025
Developing Schemas with FME and Excel - Peak of Data & AI 2025
Safe Software
 
MCP vs A2A vs ACP: Choosing the Right Protocol | Bluebash
MCP vs A2A vs ACP: Choosing the Right Protocol | BluebashMCP vs A2A vs ACP: Choosing the Right Protocol | Bluebash
MCP vs A2A vs ACP: Choosing the Right Protocol | Bluebash
Bluebash
 
Improving Developer Productivity With DORA, SPACE, and DevEx
Improving Developer Productivity With DORA, SPACE, and DevExImproving Developer Productivity With DORA, SPACE, and DevEx
Improving Developer Productivity With DORA, SPACE, and DevEx
Justin Reock
 
Cybersecurity Fundamentals: Apprentice - Palo Alto Certificate
Cybersecurity Fundamentals: Apprentice - Palo Alto CertificateCybersecurity Fundamentals: Apprentice - Palo Alto Certificate
Cybersecurity Fundamentals: Apprentice - Palo Alto Certificate
VICTOR MAESTRE RAMIREZ
 
Data Virtualization: Bringing the Power of FME to Any Application
Data Virtualization: Bringing the Power of FME to Any ApplicationData Virtualization: Bringing the Power of FME to Any Application
Data Virtualization: Bringing the Power of FME to Any Application
Safe Software
 
ISOIEC 42005 Revolutionalises AI Impact Assessment.pptx
ISOIEC 42005 Revolutionalises AI Impact Assessment.pptxISOIEC 42005 Revolutionalises AI Impact Assessment.pptx
ISOIEC 42005 Revolutionalises AI Impact Assessment.pptx
AyilurRamnath1
 
Compliance-as-a-Service document pdf text
Compliance-as-a-Service document pdf textCompliance-as-a-Service document pdf text
Compliance-as-a-Service document pdf text
Earthling security
 
Extend-Microsoft365-with-Copilot-agents.pptx
Extend-Microsoft365-with-Copilot-agents.pptxExtend-Microsoft365-with-Copilot-agents.pptx
Extend-Microsoft365-with-Copilot-agents.pptx
hoang971
 
Jira Administration Training – Day 1 : Introduction
Jira Administration Training – Day 1 : IntroductionJira Administration Training – Day 1 : Introduction
Jira Administration Training – Day 1 : Introduction
Ravi Teja
 
What is Oracle EPM A Guide to Oracle EPM Cloud Everything You Need to Know
What is Oracle EPM A Guide to Oracle EPM Cloud Everything You Need to KnowWhat is Oracle EPM A Guide to Oracle EPM Cloud Everything You Need to Know
What is Oracle EPM A Guide to Oracle EPM Cloud Everything You Need to Know
SMACT Works
 
Palo Alto Networks Cybersecurity Foundation
Palo Alto Networks Cybersecurity FoundationPalo Alto Networks Cybersecurity Foundation
Palo Alto Networks Cybersecurity Foundation
VICTOR MAESTRE RAMIREZ
 
Establish Visibility and Manage Risk in the Supply Chain with Anchore SBOM
Establish Visibility and Manage Risk in the Supply Chain with Anchore SBOMEstablish Visibility and Manage Risk in the Supply Chain with Anchore SBOM
Establish Visibility and Manage Risk in the Supply Chain with Anchore SBOM
Anchore
 
Your startup on AWS - How to architect and maintain a Lean and Mean account
Your startup on AWS - How to architect and maintain a Lean and Mean accountYour startup on AWS - How to architect and maintain a Lean and Mean account
Your startup on AWS - How to architect and maintain a Lean and Mean account
angelo60207
 
DevOps in the Modern Era - Thoughtfully Critical Podcast
DevOps in the Modern Era - Thoughtfully Critical PodcastDevOps in the Modern Era - Thoughtfully Critical Podcast
DevOps in the Modern Era - Thoughtfully Critical Podcast
Chris Wahl
 
Create Your First AI Agent with UiPath Agent Builder
Create Your First AI Agent with UiPath Agent BuilderCreate Your First AI Agent with UiPath Agent Builder
Create Your First AI Agent with UiPath Agent Builder
DianaGray10
 
Ad

Intro to JavaScript - Week 2: Function

  • 1. Introduction to Programming with JavaScript Week 2: Function Jeongbae Oh YCC JavaScript Seminar 2017.09.25
  • 2. Function • The most important part of JavaScript • A "mini program" within the program • Basis of the functional programming paradigm
  • 3. Input / Output • A function can receive inputs, process them, and return an output. • But it doesn't have to get an input. • Or return an output. • Or process anything. • Of course a function not doing any of the three is pretty meaningless. • Put simply, the input of a function is called a parameter (매개변수), and 
 the output is called a return value (반환값, 리턴값)
  • 4. Declaration • function name(parameters) { } • No semi-colon necessary • Function naming rules (same as variable) • Must consist of lower and upper case alphabet letters, numbers, and _ • Can only begin with lower and upper case alphabet letters • Cannot use reserved words (e.g. function, var, etc.) • Lower camel case recommended (e.g. getTaxRate) → Convention • Function without a name is called an anonymous function (익명 함수).
  • 5. Call / Invocation • To execute codes within a function, it must be called (invoked). • To call a function: name(argument); • A function can be declared and called 
 immediately (immediately invoked function): • (function name(parameter) { })(); • Anonymous functions are usually called immediately.
  • 6. Call / Invocation • Location of a function within a source code has no effect on whether the function can be called.
 (i.e. you can call a function before defining it) • Not calling a function makes that function to have no effect on the execution of the code.
  • 7. return • A function finishes running with return. • Anything after return is not executed. • Even without return, function finishes running at the end. • Value after return becomes the return value of the function. • Return value is not necessary.
  • 8. return and console.log • return makes the result of execution of the function available for use as a value, and therefore not usually "printed" like console.log in a real setting. • console.log is a special function/method which "prints" the value to the console/REPL to make debugging easy. • Therefore, the return value of console.log() is undefined (nothing is returned). console.log does not 
 have a return value. console.log return console.log return
  • 9. Parameter / Argument • To put simply: • A parameter (매개변수) is the input of a function. • An argument (인자) is what is passed to a function when called. parameters arguments
  • 10. Function as a First-Class Citizen • A function can be passed to another function as an argument. • A function can be returned by another function. • A function can be assigned to a variable (a function expression)
  • 11. Scope • Scope (범위) of a function is defined by the portion encompassed by braces (block). • Each function has its own scope.
  • 12. Nested Scope • A function can have another function within itself, which is called a nested function. • Function inside can access values in the function outside.
 (i.e. manipulate them without initializing) • This means that if a variable is assigned a different value within the inner function, it changes the value for the outer function as well.
  • 13. Nested Scope • However, if a variable is first initialized and changed value within the inner function, that change only takes effect within that inner function. • In other words, if the value in the outer function's scope should not be changed, 1) Initialize the variable 2) Use a different variable name
  • 14. Nested Scope • If the outer function does not directly call the inner function (i.e. only returns the "function object"), the latter should be called together when the outer is called such as:
 name()(); • To put simply, since the return value of outer() is inner, outer()() can be understood as outer() + inner().
  • 15. Multiple-Nested Scope • Characteristics of nested scope apply to more-than-double-nested scope. • The below two have the same results, but different approaches to nested function structure.
  • 16. Global Scope • Scope outside of all functions is called global scope. • Global scope acts like a "global function" that is defined and executed automatically by JavaScript interpreter itself. • Characteristics of the global scope is identical to the outermost function in the nested function structure.
  • 17. Closure • A value/variable defined in the outside function can be accessed by the inside function(s) without being explicitly defined in the inside function. • The reverse does not hold (i.e. the outer function cannot access values of the inner function). Closure Not a closure
  • 18. Closure • Closure makes programming easier by allowing variables to passed to inner functions without precise declarations. • Without closure, parameters/arguments throughout the entirety of the function need to be matched. Using closure Not using closure
  • 19. Stack • In JavaScript, information is stored in memory as a "stack." • LIFO (last-in, first-out): value stored last is taken out first • Put very very simply, values are "pushed" into stack when defined within a function, and "popped" from stack when the function returns or finishes running. https://upload.wikimedia.org/wikipedia/commons/b/b4/Lifo_stack.png
  • 20. How Stack Works • outer() is called and pushed to the stack. • inner() is called and pushed to the stack. • inner() is executed, returns, and popped from the stack. • outer() is executed, returns, and popped from the stack. • Stack is emptied at the end. * Please note that this is NOT actual way stack works; just an illustration
  • 21. How Stack Works • outer() is called and pushed to the stack. • inner() is called and pushed to the stack. • inner() is executed, returns, and popped from the stack. • outer() is executed, returns, and popped from the s tack. • Stack is emptied at the end. * Please note that this is NOT actual way stack works; just an illustration
  • 22. Recursion • Calling the function within itself is called recursion (재귀). • Recursion is an "elegant" way to code, but is inefficient because it uses much more memory than non-recursive way. • Example: the Fibonacci sequence • 1 + 1 + 2 + 3 + 5 + 8 + ... • Try fib(1000). 
 How does it work?
  • 23. Stack Overflow • Since memory is limited, size of stack is limited as well. If a program creates stack larger than the memory space, it can no longer run. This is called stack overflow. • Recursion is the easiest way to cause stack overflow, if it is not stopped at a proper time. • Stack Overflow is also the name of the most popular developer community. 
 (뇌가 stack overflow 되었을 때 찾아오라는 뜻?)