SlideShare une entreprise Scribd logo
1  sur  23
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 되었을 때 찾아오라는
뜻?)

Contenu connexe

Tendances

Functional programming in scala
Functional programming in scalaFunctional programming in scala
Functional programming in scalaStratio
 
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 FunctionsJay 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 styleNiranjan Sarade
 
Ruby Functional Programming
Ruby Functional ProgrammingRuby Functional Programming
Ruby Functional ProgrammingGeison Goes
 
Rdbms chapter 1 function
Rdbms chapter 1 functionRdbms chapter 1 function
Rdbms chapter 1 functiondipumaliy
 
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 FundamentalsSrdjan Strbanovic
 
Functional programming with Java 8
Functional programming with Java 8Functional programming with Java 8
Functional programming with Java 8LivePerson
 
Function different types of funtion
Function different types of funtionFunction different types of funtion
Function different types of funtionsvishalsingh01
 
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 typeChang 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 smartChen Fisher
 
Introduction to c first week slides
Introduction to c first week slidesIntroduction to c first week slides
Introduction to c first week slidesluqman bawany
 
Functional programming with Java 8
Functional programming with Java 8Functional programming with Java 8
Functional programming with Java 8Talha Ocakçı
 

Tendances (20)

Client sidescripting javascript
Client sidescripting javascriptClient sidescripting javascript
Client sidescripting javascript
 
C++ Functions
C++ FunctionsC++ Functions
C++ Functions
 
Functional programming in scala
Functional programming in scalaFunctional programming in scala
Functional programming in scala
 
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
 
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
 
Ruby Functional Programming
Ruby Functional ProgrammingRuby Functional Programming
Ruby Functional Programming
 
FUNCTION CPU
FUNCTION CPUFUNCTION CPU
FUNCTION CPU
 
Rdbms chapter 1 function
Rdbms chapter 1 functionRdbms chapter 1 function
Rdbms chapter 1 function
 
Function class in c++
Function class in c++Function class in c++
Function class in c++
 
Function overloading in c++
Function overloading in c++Function overloading in c++
Function overloading in c++
 
Functional JavaScript Fundamentals
Functional JavaScript FundamentalsFunctional JavaScript Fundamentals
Functional JavaScript Fundamentals
 
Functional programming with Java 8
Functional programming with Java 8Functional programming with Java 8
Functional programming with Java 8
 
Think in linq
Think in linqThink in linq
Think in linq
 
Function different types of funtion
Function different types of funtionFunction different types of funtion
Function different types of funtion
 
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
 
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
 
Introduction to c first week slides
Introduction to c first week slidesIntroduction to c first week slides
Introduction to c first week slides
 
Functional programming with Java 8
Functional programming with Java 8Functional programming with Java 8
Functional programming with Java 8
 
Java script function
Java script functionJava script function
Java script function
 
Functions in c++
Functions in c++Functions in c++
Functions in c++
 

Similaire à Intro to JavaScript - Week 2: Function

662213141-Tuxdoc-com-Programming-in-c-Reema-Thareja.pdf
662213141-Tuxdoc-com-Programming-in-c-Reema-Thareja.pdf662213141-Tuxdoc-com-Programming-in-c-Reema-Thareja.pdf
662213141-Tuxdoc-com-Programming-in-c-Reema-Thareja.pdfManiMala75
 
CH.4FUNCTIONS IN C (1).pptx
CH.4FUNCTIONS IN C (1).pptxCH.4FUNCTIONS IN C (1).pptx
CH.4FUNCTIONS IN C (1).pptxsangeeta borde
 
Function in C++, Methods in C++ coding programming
Function in C++, Methods in C++ coding programmingFunction in C++, Methods in C++ coding programming
Function in C++, Methods in C++ coding programmingestorebackupr
 
358 33 powerpoint-slides_2-functions_chapter-2
358 33 powerpoint-slides_2-functions_chapter-2358 33 powerpoint-slides_2-functions_chapter-2
358 33 powerpoint-slides_2-functions_chapter-2sumitbardhan
 
Booting into functional programming
Booting into functional programmingBooting into functional programming
Booting into functional programmingDhaval Dalal
 
Chapter Introduction to Modular Programming.ppt
Chapter Introduction to Modular Programming.pptChapter Introduction to Modular Programming.ppt
Chapter Introduction to Modular Programming.pptAmanuelZewdie4
 
Functional programming in clojure
Functional programming in clojureFunctional programming in clojure
Functional programming in clojureJuan-Manuel Gimeno
 
Chapter One Function.pptx
Chapter One Function.pptxChapter One Function.pptx
Chapter One Function.pptxmiki304759
 
(3) cpp procedural programming
(3) cpp procedural programming(3) cpp procedural programming
(3) cpp procedural programmingNico Ludwig
 
OOP-Module-1-Section-4-LectureNo1-5.pptx
OOP-Module-1-Section-4-LectureNo1-5.pptxOOP-Module-1-Section-4-LectureNo1-5.pptx
OOP-Module-1-Section-4-LectureNo1-5.pptxsarthakgithub
 
11.C++Polymorphism [Autosaved].pptx
11.C++Polymorphism [Autosaved].pptx11.C++Polymorphism [Autosaved].pptx
11.C++Polymorphism [Autosaved].pptxAtharvPotdar2
 
Functions in c++
Functions in c++Functions in c++
Functions in c++Maaz Hasan
 

Similaire à Intro to JavaScript - Week 2: Function (20)

Polymorphism
PolymorphismPolymorphism
Polymorphism
 
662213141-Tuxdoc-com-Programming-in-c-Reema-Thareja.pdf
662213141-Tuxdoc-com-Programming-in-c-Reema-Thareja.pdf662213141-Tuxdoc-com-Programming-in-c-Reema-Thareja.pdf
662213141-Tuxdoc-com-Programming-in-c-Reema-Thareja.pdf
 
Functional programming in python
Functional programming in pythonFunctional programming in python
Functional programming in python
 
Functional programming in python
Functional programming in pythonFunctional programming in python
Functional programming in python
 
CH.4FUNCTIONS IN C (1).pptx
CH.4FUNCTIONS IN C (1).pptxCH.4FUNCTIONS IN C (1).pptx
CH.4FUNCTIONS IN C (1).pptx
 
Function in C++, Methods in C++ coding programming
Function in C++, Methods in C++ coding programmingFunction in C++, Methods in C++ coding programming
Function in C++, Methods in C++ coding programming
 
358 33 powerpoint-slides_2-functions_chapter-2
358 33 powerpoint-slides_2-functions_chapter-2358 33 powerpoint-slides_2-functions_chapter-2
358 33 powerpoint-slides_2-functions_chapter-2
 
Basic c++
Basic c++Basic c++
Basic c++
 
Booting into functional programming
Booting into functional programmingBooting into functional programming
Booting into functional programming
 
Function
Function Function
Function
 
Functions
FunctionsFunctions
Functions
 
Basics of cpp
Basics of cppBasics of cpp
Basics of cpp
 
Chapter Introduction to Modular Programming.ppt
Chapter Introduction to Modular Programming.pptChapter Introduction to Modular Programming.ppt
Chapter Introduction to Modular Programming.ppt
 
Functional programming in clojure
Functional programming in clojureFunctional programming in clojure
Functional programming in clojure
 
Chapter One Function.pptx
Chapter One Function.pptxChapter One Function.pptx
Chapter One Function.pptx
 
2.0 Stacks.pptx
2.0 Stacks.pptx2.0 Stacks.pptx
2.0 Stacks.pptx
 
(3) cpp procedural programming
(3) cpp procedural programming(3) cpp procedural programming
(3) cpp procedural programming
 
OOP-Module-1-Section-4-LectureNo1-5.pptx
OOP-Module-1-Section-4-LectureNo1-5.pptxOOP-Module-1-Section-4-LectureNo1-5.pptx
OOP-Module-1-Section-4-LectureNo1-5.pptx
 
11.C++Polymorphism [Autosaved].pptx
11.C++Polymorphism [Autosaved].pptx11.C++Polymorphism [Autosaved].pptx
11.C++Polymorphism [Autosaved].pptx
 
Functions in c++
Functions in c++Functions in c++
Functions in c++
 

Dernier

Scaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationScaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationRadu Cotescu
 
Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)wesley chun
 
How to convert PDF to text with Nanonets
How to convert PDF to text with NanonetsHow to convert PDF to text with Nanonets
How to convert PDF to text with Nanonetsnaman860154
 
🐬 The future of MySQL is Postgres 🐘
🐬  The future of MySQL is Postgres   🐘🐬  The future of MySQL is Postgres   🐘
🐬 The future of MySQL is Postgres 🐘RTylerCroy
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerThousandEyes
 
Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024The Digital Insurer
 
GenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationGenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationMichael W. Hawkins
 
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationFrom Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationSafe Software
 
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptxHampshireHUG
 
Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024The Digital Insurer
 
Exploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone ProcessorsExploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone Processorsdebabhi2
 
[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdfhans926745
 
CNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of ServiceCNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of Servicegiselly40
 
Partners Life - Insurer Innovation Award 2024
Partners Life - Insurer Innovation Award 2024Partners Life - Insurer Innovation Award 2024
Partners Life - Insurer Innovation Award 2024The Digital Insurer
 
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024The Digital Insurer
 
The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024Rafal Los
 
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...Igalia
 
Automating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps ScriptAutomating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps Scriptwesley chun
 
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdfThe Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdfEnterprise Knowledge
 
Understanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdfUnderstanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdfUK Journal
 

Dernier (20)

Scaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationScaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organization
 
Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)
 
How to convert PDF to text with Nanonets
How to convert PDF to text with NanonetsHow to convert PDF to text with Nanonets
How to convert PDF to text with Nanonets
 
🐬 The future of MySQL is Postgres 🐘
🐬  The future of MySQL is Postgres   🐘🐬  The future of MySQL is Postgres   🐘
🐬 The future of MySQL is Postgres 🐘
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected Worker
 
Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024
 
GenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationGenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day Presentation
 
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationFrom Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
 
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
 
Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024
 
Exploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone ProcessorsExploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone Processors
 
[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf
 
CNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of ServiceCNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of Service
 
Partners Life - Insurer Innovation Award 2024
Partners Life - Insurer Innovation Award 2024Partners Life - Insurer Innovation Award 2024
Partners Life - Insurer Innovation Award 2024
 
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
 
The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024
 
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
 
Automating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps ScriptAutomating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps Script
 
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdfThe Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
 
Understanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdfUnderstanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdf
 

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 되었을 때 찾아오라는 뜻?)