predefined and user defined functions

Post on 13-Apr-2017

408 views 3 download

Transcript of predefined and user defined functions

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:\n\nHave 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:\n\nHave 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 CelsiustempInCelsius = (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 neededfreezing = FahrToCelsius(32); current = FahrToCelsius(78);

<head>

<title>Function Example</title>

<script type="text/javascript">

<!--

function PrintMessage()

{

alert("A message for you:\n\nHave 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>