SlideShare une entreprise Scribd logo
1  sur  17
Télécharger pour lire hors ligne
PREDEFINED & USER
DEFINED FUNCTIONS
Presenting By – Swapnil Yadav ,
Class - 10
The Function Definition
• Control is passed to the function by the function
call. The statements within the function body will
then be executed.
function PrintMessage()
{
alert("A message for you:nnHave a nice day!");
}
• After the statements in the function have
completed, control is passed back to the place
where the function was called.
General Function Definition Syntax
function FunctionName ( parameter1, . . . , parametern )
{
variable declaration(s)
statement(s)
}
• If there are no parameters, there should be nothing inside of
the ()'s
function FunctionName()
{
...
}
• There may be no variable declarations.
Functions
• When program control encounters a function name, the function is called
(invoked).
• Program control passes to the function.
• The function is executed.
• Control is passed back to the place where the function
was called.
• A function is a block of code that will be executed
when "someone" calls it. In JavaScript, we can define
our own functions, called user-defined functions, or
we can use built-in functions already defined in the
JavaScript language.
The Function Call
• Passes program control to the function
• Must match the definition in name and number of
arguments
........
function PrintMessage()
{
alert("A message for you:nnHave a nice day!");
}
........
<body>
<script type="text/javascript">
<!--
PrintMessage();
//-->
</script>
</body>
Same name and
no arguments
(nothing inside of
the parentheses)
Sample Function Call
alert is the name of a predefined
function in the JavaScript language
alert("Hello World!"); this statement is
is known as a
function call
this is a string we are passing
as an argument (parameter) to
the alert function
Predefined Functions
• We have used several predefined functions so far:
• alert()
• prompt()
• document.write()
• toFixed()
• parseInt()
• parseFloat()
• Programmers can write their own functions.
• Typically, each module in a program’s design hierarchy chart is
implemented as a function.
• write() is a method of the document object that writes the content
"Hello World" on the web page. There are several Javascript built-in
objects such as,
• Number
• String
• RegExp
• Array
• Math
• Date
• Boolean
• Each of the above objects hold several built-in functions to perform
object related functionality. Apart from these methods, Javascript
provides few predefined functions which do not stick to a particular
object type but are global.
Predefined Function - String
• String() function converts the object argument passed to it to a string
value.
•
• var obj1=new Boolean(0);
• var obj2=new Boolean(1);
• var obj3=new Date();
•
• document.write(String(obj1));
• document.write(String(obj2));
• document.write(String(obj3));
• Result:
• false
• true
• Thu Jul 19 2012 23:28:08 GMT+0530 (India Standard Time
Predefined Function - parseInt
• parseInt()
• parseInt() function takes string as a parameter and converts it to
integer.
• document.write(parseInt("50"));
• document.write(parseInt("77 days"));
• document.write(parseInt("this is 7"));
• Result:
• 50
• 77
• NaN
• An optional radix parameter can also be used to specify the number
system to be used to parse the string argument. For example,
• document.write(parseInt("10",16));
• Result:
16
Predefined Function - parseFloat
• parseFloat()
• parseFloat() function takes a string as parameter and parses it to a
floating point number.
• document.write(parseFloat("10.33"));
• document.write(parseFloat("15 66 75"));
• document.write(parseFloat("this is 77"));
• document.write(pareFloat(" 77 "));
• Result:
• 10.33
• 15
• NaN
• 77
• Note: This function allows leading and trailing spaces. If the first character
in the string is not a number, then it returns NaN. If the string has more
than one set of number separated by delimiters such as spaces,
semicolons,commas then it returns only the first set of number before the
first delimiter.
User-defined Functions
• JavaScript’s predefined functions represent a collection of useful, general-purpose abstractions
• the programmer can add additional abstractions via user-defined functions
• once defined, a user-defined function can be used the same way as a predefined function
• e.g., consider converting a temperature from Fahrenheit to Celsius
tempInCelsius = (5/9) * (tempInFahr - 32);
function FahrToCelsius(tempInFahr)
// Assumes: tempInFahr is a temperature in Fahrenheit
// Returns: the equivalent temperature in Celsius
{
return (5/9) * (tempInFahr - 32);
}
this expression & assignment could be used whenever we want to convert
 requires remembering the formula every time
instead, we could define a function to encapsulate the calculation
could then call that function whenever a conversion was needed
freezing = FahrToCelsius(32); current = FahrToCelsius(78);
<head>
<title>Function Example</title>
<script type="text/javascript">
<!--
function PrintMessage()
{
alert("A message for you:nnHave a nice day!");
}
//-->
</script>
</head>
<body>
<script type="text/javascript">
<!--
PrintMessage();
//-->
</script>
</body>
Screenshot of Function Example
User – Defined function
• Multiple JavaScript functions can be defined in the HEAD section of a HTML document. <html>
• <head> <script type="text/javascript">
• function function1( ) {
• some code
• }
• function function2( )
• {
• some code
• } </script></head>
• <body>
• …
• </body> </html>
User – Defined function
• In the following example, we define a function named welcome( ) that
takes in one parameter, named user. When the function is called, we
pass the argument "John Smith" to the function.
• <script> function welcome( user )
• {
• alert( "Welcome " + user + " to my website!" );
• }
• </script> <script>
• welcome( "John Smith" );
• </script>
predefined and user defined functions

Contenu connexe

Tendances

OOP Introduction with java programming language
OOP Introduction with java programming languageOOP Introduction with java programming language
OOP Introduction with java programming languageMd.Al-imran Roton
 
Python/Flask Presentation
Python/Flask PresentationPython/Flask Presentation
Python/Flask PresentationParag Mujumdar
 
Event Handling in java
Event Handling in javaEvent Handling in java
Event Handling in javaGoogle
 
Android styles and themes
Android   styles and themesAndroid   styles and themes
Android styles and themesDeepa Rani
 
Input output statement in C
Input output statement in CInput output statement in C
Input output statement in CMuthuganesh S
 
User defined functions in C
User defined functions in CUser defined functions in C
User defined functions in CHarendra Singh
 
Class and Objects in Java
Class and Objects in JavaClass and Objects in Java
Class and Objects in JavaSpotle.ai
 
Loops PHP 04
Loops PHP 04Loops PHP 04
Loops PHP 04Spy Seat
 
JavaScript - Chapter 13 - Browser Object Model(BOM)
JavaScript - Chapter 13 - Browser Object Model(BOM)JavaScript - Chapter 13 - Browser Object Model(BOM)
JavaScript - Chapter 13 - Browser Object Model(BOM)WebStackAcademy
 
Chapter Introduction to Modular Programming.ppt
Chapter Introduction to Modular Programming.pptChapter Introduction to Modular Programming.ppt
Chapter Introduction to Modular Programming.pptAmanuelZewdie4
 
Object Oriented Programming Using C++
Object Oriented Programming Using C++Object Oriented Programming Using C++
Object Oriented Programming Using C++Muhammad Waqas
 
Presentation on Function in C Programming
Presentation on Function in C ProgrammingPresentation on Function in C Programming
Presentation on Function in C ProgrammingShuvongkor Barman
 

Tendances (20)

OOP Introduction with java programming language
OOP Introduction with java programming languageOOP Introduction with java programming language
OOP Introduction with java programming language
 
Python/Flask Presentation
Python/Flask PresentationPython/Flask Presentation
Python/Flask Presentation
 
PHP FUNCTIONS
PHP FUNCTIONSPHP FUNCTIONS
PHP FUNCTIONS
 
VB net lab.pdf
VB net lab.pdfVB net lab.pdf
VB net lab.pdf
 
Php.ppt
Php.pptPhp.ppt
Php.ppt
 
Delegates and events in C#
Delegates and events in C#Delegates and events in C#
Delegates and events in C#
 
Event Handling in java
Event Handling in javaEvent Handling in java
Event Handling in java
 
Javascript
JavascriptJavascript
Javascript
 
Android styles and themes
Android   styles and themesAndroid   styles and themes
Android styles and themes
 
Input output statement in C
Input output statement in CInput output statement in C
Input output statement in C
 
User defined functions in C
User defined functions in CUser defined functions in C
User defined functions in C
 
Class and Objects in Java
Class and Objects in JavaClass and Objects in Java
Class and Objects in Java
 
Loops PHP 04
Loops PHP 04Loops PHP 04
Loops PHP 04
 
JavaScript - Chapter 13 - Browser Object Model(BOM)
JavaScript - Chapter 13 - Browser Object Model(BOM)JavaScript - Chapter 13 - Browser Object Model(BOM)
JavaScript - Chapter 13 - Browser Object Model(BOM)
 
Chapter Introduction to Modular Programming.ppt
Chapter Introduction to Modular Programming.pptChapter Introduction to Modular Programming.ppt
Chapter Introduction to Modular Programming.ppt
 
Object Oriented Programming Using C++
Object Oriented Programming Using C++Object Oriented Programming Using C++
Object Oriented Programming Using C++
 
Presentation on Function in C Programming
Presentation on Function in C ProgrammingPresentation on Function in C Programming
Presentation on Function in C Programming
 
functions of C++
functions of C++functions of C++
functions of C++
 
Dart ppt
Dart pptDart ppt
Dart ppt
 
Functions in c
Functions in cFunctions in c
Functions in c
 

Similaire à predefined and user defined functions

Similaire à predefined and user defined functions (20)

Function in C Programming
Function in C ProgrammingFunction in C Programming
Function in C Programming
 
Unit 8
Unit 8Unit 8
Unit 8
 
Python_Functions_Unit1.pptx
Python_Functions_Unit1.pptxPython_Functions_Unit1.pptx
Python_Functions_Unit1.pptx
 
Basic c++
Basic c++Basic c++
Basic c++
 
Lecture 1_Functions in C.pptx
Lecture 1_Functions in C.pptxLecture 1_Functions in C.pptx
Lecture 1_Functions in C.pptx
 
Functions
FunctionsFunctions
Functions
 
Function
Function Function
Function
 
POLITEKNIK MALAYSIA
POLITEKNIK MALAYSIAPOLITEKNIK MALAYSIA
POLITEKNIK MALAYSIA
 
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
 
Chapter One Function.pptx
Chapter One Function.pptxChapter One Function.pptx
Chapter One Function.pptx
 
Functions
FunctionsFunctions
Functions
 
5. Functions in C.pdf
5. Functions in C.pdf5. Functions in C.pdf
5. Functions in C.pdf
 
JavaScript for real men
JavaScript for real menJavaScript for real men
JavaScript for real men
 
CH.4FUNCTIONS IN C (1).pptx
CH.4FUNCTIONS IN C (1).pptxCH.4FUNCTIONS IN C (1).pptx
CH.4FUNCTIONS IN C (1).pptx
 
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
 
Function
FunctionFunction
Function
 
Unit iii
Unit iiiUnit iii
Unit iii
 
Programming Fundamentals Functions in C and types
Programming Fundamentals  Functions in C  and typesProgramming Fundamentals  Functions in C  and types
Programming Fundamentals Functions in C and types
 
Functions
FunctionsFunctions
Functions
 
Basic information of function in cpu
Basic information of function in cpuBasic information of function in cpu
Basic information of function in cpu
 

Dernier

Explore the UiPath Community and ways you can benefit on your journey to auto...
Explore the UiPath Community and ways you can benefit on your journey to auto...Explore the UiPath Community and ways you can benefit on your journey to auto...
Explore the UiPath Community and ways you can benefit on your journey to auto...DianaGray10
 
Key Trends Shaping the Future of Infrastructure.pdf
Key Trends Shaping the Future of Infrastructure.pdfKey Trends Shaping the Future of Infrastructure.pdf
Key Trends Shaping the Future of Infrastructure.pdfCheryl Hung
 
Emil Eifrem at GraphSummit Copenhagen 2024 - The Art of the Possible.pptx
Emil Eifrem at GraphSummit Copenhagen 2024 - The Art of the Possible.pptxEmil Eifrem at GraphSummit Copenhagen 2024 - The Art of the Possible.pptx
Emil Eifrem at GraphSummit Copenhagen 2024 - The Art of the Possible.pptxNeo4j
 
Q4 2023 Quarterly Investor Presentation - FINAL - v1.pdf
Q4 2023 Quarterly Investor Presentation - FINAL - v1.pdfQ4 2023 Quarterly Investor Presentation - FINAL - v1.pdf
Q4 2023 Quarterly Investor Presentation - FINAL - v1.pdfTejal81
 
My key hands-on projects in Quantum, and QAI
My key hands-on projects in Quantum, and QAIMy key hands-on projects in Quantum, and QAI
My key hands-on projects in Quantum, and QAIVijayananda Mohire
 
Outage Analysis: March 5th/6th 2024 Meta, Comcast, and LinkedIn
Outage Analysis: March 5th/6th 2024 Meta, Comcast, and LinkedInOutage Analysis: March 5th/6th 2024 Meta, Comcast, and LinkedIn
Outage Analysis: March 5th/6th 2024 Meta, Comcast, and LinkedInThousandEyes
 
Where developers are challenged, what developers want and where DevEx is going
Where developers are challenged, what developers want and where DevEx is goingWhere developers are challenged, what developers want and where DevEx is going
Where developers are challenged, what developers want and where DevEx is goingFrancesco Corti
 
Novo Nordisk's journey in developing an open-source application on Neo4j
Novo Nordisk's journey in developing an open-source application on Neo4jNovo Nordisk's journey in developing an open-source application on Neo4j
Novo Nordisk's journey in developing an open-source application on Neo4jNeo4j
 
SIM INFORMATION SYSTEM: REVOLUTIONIZING DATA MANAGEMENT
SIM INFORMATION SYSTEM: REVOLUTIONIZING DATA MANAGEMENTSIM INFORMATION SYSTEM: REVOLUTIONIZING DATA MANAGEMENT
SIM INFORMATION SYSTEM: REVOLUTIONIZING DATA MANAGEMENTxtailishbaloch
 
How to release an Open Source Dataweave Library
How to release an Open Source Dataweave LibraryHow to release an Open Source Dataweave Library
How to release an Open Source Dataweave Libraryshyamraj55
 
Extra-120324-Visite-Entreprise-icare.pdf
Extra-120324-Visite-Entreprise-icare.pdfExtra-120324-Visite-Entreprise-icare.pdf
Extra-120324-Visite-Entreprise-icare.pdfInfopole1
 
How to become a GDSC Lead GDSC MI AOE.pptx
How to become a GDSC Lead GDSC MI AOE.pptxHow to become a GDSC Lead GDSC MI AOE.pptx
How to become a GDSC Lead GDSC MI AOE.pptxKaustubhBhavsar6
 
3 Pitfalls Everyone Should Avoid with Cloud Data
3 Pitfalls Everyone Should Avoid with Cloud Data3 Pitfalls Everyone Should Avoid with Cloud Data
3 Pitfalls Everyone Should Avoid with Cloud DataEric D. Schabell
 
From the origin to the future of Open Source model and business
From the origin to the future of  Open Source model and businessFrom the origin to the future of  Open Source model and business
From the origin to the future of Open Source model and businessFrancesco Corti
 
Top 10 Squarespace Development Companies
Top 10 Squarespace Development CompaniesTop 10 Squarespace Development Companies
Top 10 Squarespace Development CompaniesTopCSSGallery
 
Graphene Quantum Dots-Based Composites for Biomedical Applications
Graphene Quantum Dots-Based Composites for  Biomedical ApplicationsGraphene Quantum Dots-Based Composites for  Biomedical Applications
Graphene Quantum Dots-Based Composites for Biomedical Applicationsnooralam814309
 
IT Service Management (ITSM) Best Practices for Advanced Computing
IT Service Management (ITSM) Best Practices for Advanced ComputingIT Service Management (ITSM) Best Practices for Advanced Computing
IT Service Management (ITSM) Best Practices for Advanced ComputingMAGNIntelligence
 
The Importance of Indoor Air Quality (English)
The Importance of Indoor Air Quality (English)The Importance of Indoor Air Quality (English)
The Importance of Indoor Air Quality (English)IES VE
 
The Zero-ETL Approach: Enhancing Data Agility and Insight
The Zero-ETL Approach: Enhancing Data Agility and InsightThe Zero-ETL Approach: Enhancing Data Agility and Insight
The Zero-ETL Approach: Enhancing Data Agility and InsightSafe Software
 

Dernier (20)

Explore the UiPath Community and ways you can benefit on your journey to auto...
Explore the UiPath Community and ways you can benefit on your journey to auto...Explore the UiPath Community and ways you can benefit on your journey to auto...
Explore the UiPath Community and ways you can benefit on your journey to auto...
 
Key Trends Shaping the Future of Infrastructure.pdf
Key Trends Shaping the Future of Infrastructure.pdfKey Trends Shaping the Future of Infrastructure.pdf
Key Trends Shaping the Future of Infrastructure.pdf
 
Emil Eifrem at GraphSummit Copenhagen 2024 - The Art of the Possible.pptx
Emil Eifrem at GraphSummit Copenhagen 2024 - The Art of the Possible.pptxEmil Eifrem at GraphSummit Copenhagen 2024 - The Art of the Possible.pptx
Emil Eifrem at GraphSummit Copenhagen 2024 - The Art of the Possible.pptx
 
Q4 2023 Quarterly Investor Presentation - FINAL - v1.pdf
Q4 2023 Quarterly Investor Presentation - FINAL - v1.pdfQ4 2023 Quarterly Investor Presentation - FINAL - v1.pdf
Q4 2023 Quarterly Investor Presentation - FINAL - v1.pdf
 
My key hands-on projects in Quantum, and QAI
My key hands-on projects in Quantum, and QAIMy key hands-on projects in Quantum, and QAI
My key hands-on projects in Quantum, and QAI
 
Outage Analysis: March 5th/6th 2024 Meta, Comcast, and LinkedIn
Outage Analysis: March 5th/6th 2024 Meta, Comcast, and LinkedInOutage Analysis: March 5th/6th 2024 Meta, Comcast, and LinkedIn
Outage Analysis: March 5th/6th 2024 Meta, Comcast, and LinkedIn
 
Where developers are challenged, what developers want and where DevEx is going
Where developers are challenged, what developers want and where DevEx is goingWhere developers are challenged, what developers want and where DevEx is going
Where developers are challenged, what developers want and where DevEx is going
 
Novo Nordisk's journey in developing an open-source application on Neo4j
Novo Nordisk's journey in developing an open-source application on Neo4jNovo Nordisk's journey in developing an open-source application on Neo4j
Novo Nordisk's journey in developing an open-source application on Neo4j
 
SIM INFORMATION SYSTEM: REVOLUTIONIZING DATA MANAGEMENT
SIM INFORMATION SYSTEM: REVOLUTIONIZING DATA MANAGEMENTSIM INFORMATION SYSTEM: REVOLUTIONIZING DATA MANAGEMENT
SIM INFORMATION SYSTEM: REVOLUTIONIZING DATA MANAGEMENT
 
How to release an Open Source Dataweave Library
How to release an Open Source Dataweave LibraryHow to release an Open Source Dataweave Library
How to release an Open Source Dataweave Library
 
Extra-120324-Visite-Entreprise-icare.pdf
Extra-120324-Visite-Entreprise-icare.pdfExtra-120324-Visite-Entreprise-icare.pdf
Extra-120324-Visite-Entreprise-icare.pdf
 
SheDev 2024
SheDev 2024SheDev 2024
SheDev 2024
 
How to become a GDSC Lead GDSC MI AOE.pptx
How to become a GDSC Lead GDSC MI AOE.pptxHow to become a GDSC Lead GDSC MI AOE.pptx
How to become a GDSC Lead GDSC MI AOE.pptx
 
3 Pitfalls Everyone Should Avoid with Cloud Data
3 Pitfalls Everyone Should Avoid with Cloud Data3 Pitfalls Everyone Should Avoid with Cloud Data
3 Pitfalls Everyone Should Avoid with Cloud Data
 
From the origin to the future of Open Source model and business
From the origin to the future of  Open Source model and businessFrom the origin to the future of  Open Source model and business
From the origin to the future of Open Source model and business
 
Top 10 Squarespace Development Companies
Top 10 Squarespace Development CompaniesTop 10 Squarespace Development Companies
Top 10 Squarespace Development Companies
 
Graphene Quantum Dots-Based Composites for Biomedical Applications
Graphene Quantum Dots-Based Composites for  Biomedical ApplicationsGraphene Quantum Dots-Based Composites for  Biomedical Applications
Graphene Quantum Dots-Based Composites for Biomedical Applications
 
IT Service Management (ITSM) Best Practices for Advanced Computing
IT Service Management (ITSM) Best Practices for Advanced ComputingIT Service Management (ITSM) Best Practices for Advanced Computing
IT Service Management (ITSM) Best Practices for Advanced Computing
 
The Importance of Indoor Air Quality (English)
The Importance of Indoor Air Quality (English)The Importance of Indoor Air Quality (English)
The Importance of Indoor Air Quality (English)
 
The Zero-ETL Approach: Enhancing Data Agility and Insight
The Zero-ETL Approach: Enhancing Data Agility and InsightThe Zero-ETL Approach: Enhancing Data Agility and Insight
The Zero-ETL Approach: Enhancing Data Agility and Insight
 

predefined and user defined functions

  • 1. PREDEFINED & USER DEFINED FUNCTIONS Presenting By – Swapnil Yadav , Class - 10
  • 2. The Function Definition • Control is passed to the function by the function call. The statements within the function body will then be executed. function PrintMessage() { alert("A message for you:nnHave a nice day!"); } • After the statements in the function have completed, control is passed back to the place where the function was called.
  • 3. General Function Definition Syntax function FunctionName ( parameter1, . . . , parametern ) { variable declaration(s) statement(s) } • If there are no parameters, there should be nothing inside of the ()'s function FunctionName() { ... } • There may be no variable declarations.
  • 4. Functions • When program control encounters a function name, the function is called (invoked). • Program control passes to the function. • The function is executed. • Control is passed back to the place where the function was called. • A function is a block of code that will be executed when "someone" calls it. In JavaScript, we can define our own functions, called user-defined functions, or we can use built-in functions already defined in the JavaScript language.
  • 5. The Function Call • Passes program control to the function • Must match the definition in name and number of arguments ........ function PrintMessage() { alert("A message for you:nnHave a nice day!"); } ........ <body> <script type="text/javascript"> <!-- PrintMessage(); //--> </script> </body> Same name and no arguments (nothing inside of the parentheses)
  • 6. Sample Function Call alert is the name of a predefined function in the JavaScript language alert("Hello World!"); this statement is is known as a function call this is a string we are passing as an argument (parameter) to the alert function
  • 7. Predefined Functions • We have used several predefined functions so far: • alert() • prompt() • document.write() • toFixed() • parseInt() • parseFloat() • Programmers can write their own functions. • Typically, each module in a program’s design hierarchy chart is implemented as a function.
  • 8. • write() is a method of the document object that writes the content "Hello World" on the web page. There are several Javascript built-in objects such as, • Number • String • RegExp • Array • Math • Date • Boolean • Each of the above objects hold several built-in functions to perform object related functionality. Apart from these methods, Javascript provides few predefined functions which do not stick to a particular object type but are global.
  • 9. Predefined Function - String • String() function converts the object argument passed to it to a string value. • • var obj1=new Boolean(0); • var obj2=new Boolean(1); • var obj3=new Date(); • • document.write(String(obj1)); • document.write(String(obj2)); • document.write(String(obj3)); • Result: • false • true • Thu Jul 19 2012 23:28:08 GMT+0530 (India Standard Time
  • 10. Predefined Function - parseInt • parseInt() • parseInt() function takes string as a parameter and converts it to integer. • document.write(parseInt("50")); • document.write(parseInt("77 days")); • document.write(parseInt("this is 7")); • Result: • 50 • 77 • NaN • An optional radix parameter can also be used to specify the number system to be used to parse the string argument. For example, • document.write(parseInt("10",16)); • Result: 16
  • 11. Predefined Function - parseFloat • parseFloat() • parseFloat() function takes a string as parameter and parses it to a floating point number. • document.write(parseFloat("10.33")); • document.write(parseFloat("15 66 75")); • document.write(parseFloat("this is 77")); • document.write(pareFloat(" 77 ")); • Result: • 10.33 • 15 • NaN • 77 • Note: This function allows leading and trailing spaces. If the first character in the string is not a number, then it returns NaN. If the string has more than one set of number separated by delimiters such as spaces, semicolons,commas then it returns only the first set of number before the first delimiter.
  • 12. User-defined Functions • JavaScript’s predefined functions represent a collection of useful, general-purpose abstractions • the programmer can add additional abstractions via user-defined functions • once defined, a user-defined function can be used the same way as a predefined function • e.g., consider converting a temperature from Fahrenheit to Celsius tempInCelsius = (5/9) * (tempInFahr - 32); function FahrToCelsius(tempInFahr) // Assumes: tempInFahr is a temperature in Fahrenheit // Returns: the equivalent temperature in Celsius { return (5/9) * (tempInFahr - 32); } this expression & assignment could be used whenever we want to convert  requires remembering the formula every time instead, we could define a function to encapsulate the calculation could then call that function whenever a conversion was needed freezing = FahrToCelsius(32); current = FahrToCelsius(78);
  • 13. <head> <title>Function Example</title> <script type="text/javascript"> <!-- function PrintMessage() { alert("A message for you:nnHave a nice day!"); } //--> </script> </head> <body> <script type="text/javascript"> <!-- PrintMessage(); //--> </script> </body>
  • 15. User – Defined function • Multiple JavaScript functions can be defined in the HEAD section of a HTML document. <html> • <head> <script type="text/javascript"> • function function1( ) { • some code • } • function function2( ) • { • some code • } </script></head> • <body> • … • </body> </html>
  • 16. User – Defined function • In the following example, we define a function named welcome( ) that takes in one parameter, named user. When the function is called, we pass the argument "John Smith" to the function. • <script> function welcome( user ) • { • alert( "Welcome " + user + " to my website!" ); • } • </script> <script> • welcome( "John Smith" ); • </script>