Foundations of IT (INFO1003) : Last Week
Foundations of IT (INFO1003) : Last Week
Semester 2, 2012
Agenda:
!Last Week
-! Control Structure -! IF-statement -! IF-THEN-ELSE statement -! IF-ELSE IF statement
2/28
What is a Function?
Predefined Functions
! Suppose you needed to do some calculations like finding the square root, rounding numbers, calculating the sine value. ! We use the Math object and its functions
! A function is a little program within your JavaScript program. ! Its purpose is to perform a single task or a series of tasks.
-! Printing a message -! Performing a calculation
<script type="text/javascript" > var num1 = prompt("Input a number with 2 decimal places: ");
! JavaScript has predefined functions ! We can also write our own functions
3/19
Defining Custom Functions ! The syntax for defining a function with no parameters is:
function keyword (required)
}
Name of Function (Must be followed by parenthesis)
<script> <script>
Step 1. This statement calls the function displayMessage alert(Good Afternoon INFO1003 Student!); displayMessage();
! The syntax for calling a function with no parameters is to simply specify the function name: myFunction()
5/19
6/19
Functions Organisations
! Syntax:
7/19
8/19
Example
Step 2. When this function is executed, the value of Joe is put into the variable parameter name.
Example
<script> function sum(a,b,c) { var sum = a + b + c; alert("The sum is: + sum); } </script> <script> x=10; y=20; z=30; sum(x, y, z); sum(10,20,30); </script>
Step 2. When this function is executed, the variables a, b, c are populated with the below values a = 10 b = 20 c = 30
<script> Joe function displayMessage(name) { Wakeup upJoe alert(Wake + name); Joe alert(Stop falling asleep!); } </script> <script> Joe displayMessage(Joe); </script>
+ is symbol for concatenate. This appends joe to the string Wake up to produce Wake up Joe
Step 1. We now call the sum function with three parameters: 10, 20, 30
Step 1. This statement calls the function displayMessage with a parameter value of Joe.
9/19
10/19
Example (Q)
<script> function sum(a,b,c) { var result = a + b + c; return result; }
a=10 b=20 C=30
!In JavaScript, you can write functions to perform some task and return a value. !The value returned by a function is placed in some variable. !Syntax:
function myFunction() { specify the value or Use the return some statements; variable to return keyword return value; The value returned by myFunction is put } into someVariable <script> myFunction(); var someVariable = myFunction(); </script>
11/19
Step 2. The sum function is executed. The value of result 60 is returned to the caller.
Step 1. We now call the sum function with three parameters: 10, 20, 30
12/19
Example Input/Output
<script> function sum(a,b,c) { var result = a + b + c; return result; } </script> <script> 10; var n1 = prompt(Enter 1st number:); 20; var n2 = prompt(Enter 2nd number:); 30; var n3 = prompt(Enter 3rd number:); var total = sum(10,20,30); var total = sum(n1,n2,n3); document.write(total); </script>
13/19
Exercise (Q):
Complete the following program writing a function called getGrade. getGrade will take 1 <script> function getGrade(mark) {
if (mark<0 || mark>100) { var grade=error; } else if (mark <50) { var grade = Fail!; } else if (mark>=50 && mark <65) { var grade = Pass!; } else if (mark >=65 && mark <75) { var grade = Credit!; } else { var grade=Distinction; } return grade;
<script> function sum(a,b,c) { total=a+b+c; } </script> </head> <body> <script> var total=10; sum(10,20,30); alert(total); </script> </body> </html> What is the value of total?_________________
16/19
//First example
} </script> <script> var examResult = prompt(What mark did you get (0-100)?") var theGrade =getGrade(examResult); alert(theGrade); </script>
15/19
// Second example
var total=10; total=sum(10,20,30); alert(total); </script> </body> </html> What is the value of total?_________________
18/19
// Third example