CLASS IX
COMPUTER SCIENCE- TERM 2
FUNCTIONS
Functions
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 ___________
e) Functions send an optional value back to the calling area using the keyword
_________
f) When a variable is declared within/inside a function, it is called a ___________
variable
g) In the code snippet given below, the global variable is ____________ and local
variable is __________
<script language=”javascript”>
var carName = "Volvo";
myFunction();
function myFunction()
{
var y=90;
}
</script>
2) True or false
a) A built in function can have any name.
b) Local variables are deleted when the function is completed.
c) We can call/execute a function many times
d) A function is like a loop.
e) It is easy to debug a modular program.
f) Global variables are deleted when you close the page.
3) Write javascript code for the following questions
a. Write a user defined function that accepts the length and breadth of a rectangle and
prints its area and perimeter.
b. 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
c. Accept a number and send it as an argument to function that calculates and displays
its square and cube
d. Write a function that receives a parameter as rating. If the employee rating is above
9, display a message “you are entitled for a salary raise this year”;
e. Write a function that accepts 3 numbers, calculates the sum and returns the value.
f. Write a function that receives 2 numbers as parameters and sends the larger of the
two numbers.
Predict the output
a) <script language=”javascript”> b) <script language=”javascript”>
function sumfn( y) function execute(b)
{ {
if(y%2= =0) var c=100;
document.write(y+6*5); var temp=b+c;
} b += temp;
if(c!=200)
sumfn(4) document.write(temp+” “ +b+” ”+c);
</script> }
var m=90;
execute(m);
</script>
c) d) Rewrite the correct code with changes
<script language="javascript"> underlined.
function square(i,j) <script type="javascript">
{ Function area(a,b)
i=i*i {
j=j*j Return (a * a)
x=i+j
return(x) document.write(Area(4))
} </script>
var i=4
var j=10
var result=square(i,j)
document.write(result);
</script>