predefined and user defined functions

17
PREDEFINED & USER DEFINED FUNCTIONS Presenting By – Swapnil Yadav , Class - 10

Upload: swapnil-yadav

Post on 13-Apr-2017

407 views

Category:

Technology


3 download

TRANSCRIPT

Page 1: predefined and user defined functions

PREDEFINED & USER DEFINED FUNCTIONS

Presenting By – Swapnil Yadav , Class - 10

Page 2: predefined and user defined functions

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.

Page 3: predefined and user defined functions

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.

Page 4: predefined and user defined functions

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.

Page 5: predefined and user defined functions

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)

Page 6: predefined and user defined functions

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

Page 7: predefined and user defined functions

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.

Page 8: predefined and user defined functions

• 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.

Page 9: predefined and user defined functions

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

Page 10: predefined and user defined functions

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

Page 11: predefined and user defined functions

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.

Page 12: predefined and user defined functions

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);

Page 13: predefined and user defined functions

<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>

Page 14: predefined and user defined functions

Screenshot of Function Example

Page 15: predefined and user defined functions

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>

Page 16: predefined and user defined functions

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>

Page 17: predefined and user defined functions