SlideShare une entreprise Scribd logo
1  sur  17
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 (20)

Html text and formatting
Html text and formattingHtml text and formatting
Html text and formatting
 
Python exception handling
Python   exception handlingPython   exception handling
Python exception handling
 
Lecture 01 - Basic Concept About OOP With Python
Lecture 01 - Basic Concept About OOP With PythonLecture 01 - Basic Concept About OOP With Python
Lecture 01 - Basic Concept About OOP With Python
 
Operator Overloading
Operator OverloadingOperator Overloading
Operator Overloading
 
Python tuple
Python   tuplePython   tuple
Python tuple
 
Subqueries -Oracle DataBase
Subqueries -Oracle DataBaseSubqueries -Oracle DataBase
Subqueries -Oracle DataBase
 
Regular expressions in Python
Regular expressions in PythonRegular expressions in Python
Regular expressions in Python
 
Functions in python slide share
Functions in python slide shareFunctions in python slide share
Functions in python slide share
 
Introduction to SQL
Introduction to SQLIntroduction to SQL
Introduction to SQL
 
Python Flow Control
Python Flow ControlPython Flow Control
Python Flow Control
 
C++ Overview PPT
C++ Overview PPTC++ Overview PPT
C++ Overview PPT
 
Strings
StringsStrings
Strings
 
Database Connectivity in PHP
Database Connectivity in PHPDatabase Connectivity in PHP
Database Connectivity in PHP
 
Event In JavaScript
Event In JavaScriptEvent In JavaScript
Event In JavaScript
 
Datatypes in python
Datatypes in pythonDatatypes in python
Datatypes in python
 
Operators in python
Operators in pythonOperators in python
Operators in python
 
Python Pandas
Python PandasPython Pandas
Python Pandas
 
Pseudocode haggis
Pseudocode   haggisPseudocode   haggis
Pseudocode haggis
 
Javascript
JavascriptJavascript
Javascript
 
Control Statements in Java
Control Statements in JavaControl Statements in Java
Control Statements in Java
 

En vedette

Database Systems - SQL - DDL Statements (Chapter 3/2)
Database Systems - SQL - DDL Statements (Chapter 3/2)Database Systems - SQL - DDL Statements (Chapter 3/2)
Database Systems - SQL - DDL Statements (Chapter 3/2)Vidyasagar Mundroy
 
Yearning jQuery
Yearning jQueryYearning jQuery
Yearning jQueryRemy Sharp
 
Pre defined Functions in C
Pre defined Functions in CPre defined Functions in C
Pre defined Functions in CPrabhu Govind
 
User defined functions in C
User defined functions in CUser defined functions in C
User defined functions in CHarendra Singh
 
OpenGurukul : Language : C++ Programming
OpenGurukul : Language : C++ ProgrammingOpenGurukul : Language : C++ Programming
OpenGurukul : Language : C++ ProgrammingOpen Gurukul
 

En vedette (6)

Database Systems - SQL - DDL Statements (Chapter 3/2)
Database Systems - SQL - DDL Statements (Chapter 3/2)Database Systems - SQL - DDL Statements (Chapter 3/2)
Database Systems - SQL - DDL Statements (Chapter 3/2)
 
Yearning jQuery
Yearning jQueryYearning jQuery
Yearning jQuery
 
Pre defined Functions in C
Pre defined Functions in CPre defined Functions in C
Pre defined Functions in C
 
User defined functions in C
User defined functions in CUser defined functions in C
User defined functions in C
 
Function in C program
Function in C programFunction in C program
Function in C program
 
OpenGurukul : Language : C++ Programming
OpenGurukul : Language : C++ ProgrammingOpenGurukul : Language : C++ Programming
OpenGurukul : Language : C++ Programming
 

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
 
FUNCTIONS IN c++ PPT
FUNCTIONS IN c++ PPTFUNCTIONS IN c++ PPT
FUNCTIONS IN c++ PPT
 
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
 

Dernier

A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?Igalia
 
Why Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire businessWhy Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire businesspanagenda
 
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
 
Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...apidays
 
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemkeProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemkeProduct Anonymous
 
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
 
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
 
Manulife - Insurer Innovation Award 2024
Manulife - Insurer Innovation Award 2024Manulife - Insurer Innovation Award 2024
Manulife - Insurer Innovation Award 2024The Digital Insurer
 
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...Miguel Araújo
 
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
 
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot TakeoffStrategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoffsammart93
 
Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024The Digital Insurer
 
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law DevelopmentsTrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law DevelopmentsTrustArc
 
Top 5 Benefits OF Using Muvi Live Paywall For Live Streams
Top 5 Benefits OF Using Muvi Live Paywall For Live StreamsTop 5 Benefits OF Using Muvi Live Paywall For Live Streams
Top 5 Benefits OF Using Muvi Live Paywall For Live StreamsRoshan Dwivedi
 
2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...Martijn de Jong
 
Deploy with confidence: VMware Cloud Foundation 5.1 on next gen Dell PowerEdg...
Deploy with confidence: VMware Cloud Foundation 5.1 on next gen Dell PowerEdg...Deploy with confidence: VMware Cloud Foundation 5.1 on next gen Dell PowerEdg...
Deploy with confidence: VMware Cloud Foundation 5.1 on next gen Dell PowerEdg...Principled Technologies
 
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
 
Artificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and MythsArtificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and MythsJoaquim Jorge
 
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
 
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
 

Dernier (20)

A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?
 
Why Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire businessWhy Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire business
 
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
 
Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...
 
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemkeProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
 
Partners Life - Insurer Innovation Award 2024
Partners Life - Insurer Innovation Award 2024Partners Life - Insurer Innovation Award 2024
Partners Life - Insurer Innovation Award 2024
 
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)
 
Manulife - Insurer Innovation Award 2024
Manulife - Insurer Innovation Award 2024Manulife - Insurer Innovation Award 2024
Manulife - Insurer Innovation Award 2024
 
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
 
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
 
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot TakeoffStrategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
 
Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024
 
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law DevelopmentsTrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
 
Top 5 Benefits OF Using Muvi Live Paywall For Live Streams
Top 5 Benefits OF Using Muvi Live Paywall For Live StreamsTop 5 Benefits OF Using Muvi Live Paywall For Live Streams
Top 5 Benefits OF Using Muvi Live Paywall For Live Streams
 
2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...
 
Deploy with confidence: VMware Cloud Foundation 5.1 on next gen Dell PowerEdg...
Deploy with confidence: VMware Cloud Foundation 5.1 on next gen Dell PowerEdg...Deploy with confidence: VMware Cloud Foundation 5.1 on next gen Dell PowerEdg...
Deploy with confidence: VMware Cloud Foundation 5.1 on next gen Dell PowerEdg...
 
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
 
Artificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and MythsArtificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and Myths
 
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
 
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
 

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>