0% found this document useful (0 votes)
100 views29 pages

Class 9-Term 2-Advanced Javascript-Functions

Here are functions to solve the problems: 1. Function to calculate area and perimeter of a rectangle: function rectAreaPeri(length, breadth) { let area = length * breadth; let peri = 2 * (length + breadth); console.log("Area is: " + area); console.log("Perimeter is: " + peri); } 2. Function to calculate cube of a number: function cube(num) { return num * num * num; } 3. Function to calculate sum and average of 5 subjects: function marksStats(s1, s2, s3, s4, s5) { let sum
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
100 views29 pages

Class 9-Term 2-Advanced Javascript-Functions

Here are functions to solve the problems: 1. Function to calculate area and perimeter of a rectangle: function rectAreaPeri(length, breadth) { let area = length * breadth; let peri = 2 * (length + breadth); console.log("Area is: " + area); console.log("Perimeter is: " + peri); } 2. Function to calculate cube of a number: function cube(num) { return num * num * num; } 3. Function to calculate sum and average of 5 subjects: function marksStats(s1, s2, s3, s4, s5) { let sum
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 29

Class 9

TERM 2
Advanced JavaScript
FUNCTIONS

OBJECTIVE

Understand the concept of modular programming using functions.


FUNCTIONS

A large program is broken into small, simple manageable code called functions or
modules.

ADVANTAGES

•Functions allow the programmer to modularize a program.

•By doing so it becomes easy to debug and manage the function and integrate the
modules and run as a whole unit. This is called divide and conquer.

•Also they help in reusability of code.

DISADVANTAGES
As the code is divided into small modules, it increases the complexity of understanding.
TYPES OF FUNCTIONS

There are 2 different types of functions


 Built-in functions which are present default in
the library.
Example - alert(), parseInt() etc

 User defined functions which are created by


the user.
CREATING USER-DEFINED FUNCTIONS

•A function is written with its body and called (invoked/executed)


•Define a function using the function keyword, followed by a unique function
name
•Use a list of parameters (that might be empty)
• statement block surrounded by curly braces.
•The general syntax of writing functions is:
<script type="text/javascript">
function functionname(parameter-list)
{ To execute the function syntax is
statements functionname()
}
</script>
Defining and executing UDF

<html>
<head>
<script language="JavaScript">
function sayHello() //function definition
{
alert("Hello from the world of Javascript");
}

sayHello(); //function call


</script>
</head>
<body></body>
</html>
A FUNCTION CAN BE CALLED MANY TIMES
FROM WITHIN A SCRIPT.
<html>
<head>
<script language="JavaScript">
function sayHello() //function definition
{
alert("Hello from the world of javascript");
}

sayHello(); //function call 3 times


sayHello();
sayHello();

</script>
</head>
<body></body>
</html>
A FUNCTION CAN BE CALLED MANY TIMES
FROM WITHIN A SCRIPT.
<html>
<head>
<script language="JavaScript">
function sayHello() //function definition
{
alert("Hello from the world of javascript");
}

for(i=1;i<11;i++)
sayHello(); //function call 10 times

</script>
</head>
<body></body>
</html>
JUST A MINUTE…
EXERCISE

Write a user defined function that accepts the length and


breadth of a rectangle and prints its area and perimeter.
•Write a function calc( ) that accepts the price of a book and the
discount given. It calculates the discounted price of the book
and displays it. Also call the function
Scope of Variable
The scope of a variable is controlled by the location of the variable
declaration, and defines the part of the program where a particular variable
is accessible.

There are two types of variables in JavaScript :


Scope of Variable
LOCAL VARIABLE AND GLOBAL VARIABLE.

Local variable is declared inside block or


function. It is accessible within the function or
block only. They are lost from the memory once function is over

Global variable is declared outside the


function. It is accessible from any function.
Example Local & Global Variables

<<script language="JavaScript">
var g=200; //global variable
function a()
{ Output
var l=10 // local variable
document.write("Local to fn a " +l+"<BR>"); Global data before function calls 200
g+=10 Local to fn a 10
document.write("Global data inside fn a " +g+"<BR>");
} Global data inside fn a 210
Local to fn b 5
function b()
{ Global data inside fn b 230
var l=5 Global data after function calls 230
g+=20
document.write("Local to fn b " +l+"<BR>");
document.write("Global data inside fn b " +g+"<BR>");
}
document.write("Global data before function calls " + g+"<BR>");
a();
b();
document.write("Global data after function calls " +g+"<BR>");
</script>
<script language="javascript">
var x=30
function calculate()
{
var y=90 // LOCAL VARIABLE ONLY AVAILABLE IN THIS FUNCTION
abc()
}
function abc(y)
{
alert(x) /valid
alert(y) // INVALID , CANNOT ACCESS VARIABLE y USED IN calculate()
y=y+80 // INVALID
}
calculate()
</script>
Passing parameters to functions
Most functions have a list of formal parameters or
arguments that provide the means for communicating
information between function via function calls.

When a function is called, the actual


parameters/arguments in the function call are assigned to
the corresponding formal parameters/arguments in the
function definition.
Passing parameters to functions

By adding one formal parameter to the above function, “name”,


sayHello() function works differently according to the value passed as a
parameter.

function sayHello(name) //function definition


{
alert("Hello " + name + " from the world of Javacript");
}
sayHello(“aditi"); //function call 1
sayHello(“ayush”); //function call 2
Write a script to print whether the user is eligible to
vote or not. (using if .. else)
<script language="JavaScript">
eligible("User1", 20);
eligible("User2", 12);

function eligible(name, age)


{
if (age>= 18)
document.writeln(name + " eligible to vote");
else
document.writeln(name + " not eligible to vote");
}
</script>
Using return to send values back

The return statement is optional in the function definition.

A function can return value by using this statement.

Syntax
return(value);

A function can return only one value to the calling area.


Using return to send values back

<script language=“JavaScript">
function area(a)
{
return (a * a)
}
document.write(area(4))
</script>
SCRIPT USING ARGUMENTS TO CALCULATE
AVERAGE OF STUDENT MARKS

<script language="JavaScript">
function average(x,y,z)
//function definition with arguments x,y,z
{
var sum= x+y+z; // statement 1
var avg= sum/3; //2
return avg; //3
}
var result=average(1,2,3); //4 passing parameters 1,2,3
document.write("Average is " + result); //5
</script>
JUST A MINUTE…
EXERCISE

Modify the previous script and calculate the


average of 3 marks entered by the user.
Predict the output:

<script language="javascript">
function area(a)
{
return (a * a)
}

x=area(6)
alert(x)
</script>
Predict the output

<script language=”javascript”>
function sumfn( y)
{
if(y%2= =0)
document.write(y+6*5);
}
Z=sumfn(4)
alert(Z)
</script>
JUST A MINUTE…
EXERCISE

Write a function product( ) that receives two


parameters and calculates the product the two
numbers.
Accept the marks of 5 subjects (out of 25) for a student.
Call a function that calculates and returns the total
average marks. Call another function that returns the
remark as “GOOD” if the average is above 70 else
returns “IMPROVEMENT REQD.”
Predict the output

<script language="javascript">
function square(i,j)
{
i=i* i
j=j*j
x=i+j
return(x)
}
var i=4
var j=10
var result=square(i,j)
document.write(result);
</script>
Write a script that defines a function square() to calculate
the square of the integers 1 to 10 (using for loop).

<script language=”JavaScript”>
function square(x) //function definition
{
return x*x;
}

for (var i=1;i<=10;i++)


document.writeln(“Square of “ + i + “=”+square(i) +”<BR>”);
</script>
Predict the output
<script language=”javascript”>
function execute(b)
{
var c=100;
var temp=b+c;
b += temp;
if(c!=200)
document.write(temp+” “ +b+” ”+c);
}

var m=90;
execute(m);

</script>
JUST A MINUTE…
EXERCISE

1) Fill in the blanks


a) Functions help in achieving ____________________ in programming.
b) There are two types of functions ________________ and _______________.
c) When we pass values to a function they are called_____________ and these are assigned to
__________________ in a function definition.
d) A function is defined/written with the keyword ___________.
1. Write a user defined function that accepts the length and breadth of a
rectangle and prints its area and perimeter.
1. Write a function to accept a number and print its cube.
2. Write a function to accept 5 subject marks and print their sum and
average
3. Write a function that receives a number as an argument and prints
whether it is even or odd.
4. Accept the total marks of a student as an argument in a function. If his
total score is above 230 then display a message “Pass” otherwise “Fail”.
Call the function also.
5. Write a function that accepts the day no of the week and prints the day
of the week.
6. Accept a number and send it as an argument to function that calculates
and displays its square and cube 8. Write a function that accepts two
numbers and prints their product.

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy