Upload
ervin-ventura
View
224
Download
0
Embed Size (px)
Citation preview
8/2/2019 Lecture 5-Functions of Array
1/11
Functions
8/2/2019 Lecture 5-Functions of Array
2/11
Working WithFunctions
Functions are useful becausethey make it possible totreat a related group of PHPstatements as a single unit.
8/2/2019 Lecture 5-Functions of Array
3/11
Defining Functions
The lines of a code that make upa function are called thefunction definition.
The syntax for defining afunction is as follows
8/2/2019 Lecture 5-Functions of Array
4/11
Parameters
Parameters are placedwithin the parentheses thatfollow the function name.
A parameter is a variablethat is used within afunction.
You can also assign defaultvalues to a parameter.
8/2/2019 Lecture 5-Functions of Array
5/11
Parameters
function computeX($number){
echo $number;
}==================
=============
functionsample($num1=10,$num2=5){
echo
$num1
;echo
$num2
;8/2/2019 Lecture 5-Functions of Array
6/11
Parameter
You can specify default valuesfor parameters.
If you specify default valuesfor parameters, thoseparameters are optional.You can call the function without
specifying those parameters.The default value will be used.
8/2/2019 Lecture 5-Functions of Array
7/11
Parameters
function sayHello($color,$size=2,$face=Arial){
echo Hello
;
}
sayHello(red);sayHello(blue,5);
sayHello(green,5,Courier);
8/2/2019 Lecture 5-Functions of Array
8/11
Calling Function
functionprintCompanyName($name){
echo
$name
;}printCompanyName(Course
Technology);
Note: Unlike variables,
function are case insensitive.This means that you can callprintCompanyName()function with any of the ff.
statements.
8/2/2019 Lecture 5-Functions of Array
9/11
Returning Values
$returnValue= aveNumber(1,2,3);
function aveNumber($a,$b,$c){
$sum=$a+$b+$c;
$result=$sum/3;return $result;
}
A return statement is astatement that returns a value tothe statement that called thefunction, which calculates the
average of three numbers.
8/2/2019 Lecture 5-Functions of Array
10/11
UnderstandingVariable Scope
Variable Scope: refers towhere in your program adeclared variable can be used.
A global variable is one that isdeclared outside a function andis available to all parts of yourprogram.
A local variable is declared insidea function and is only availablewithin the function in which it is
declared.
8/2/2019 Lecture 5-Functions of Array
11/11
Activity1. Create a function named greetings, include three
parameters inside it. When the function namegreetings is called, it should print the words:Hi, Hello, Goodbye!
Save your file as functionGreetings.html
2. Create a function named calculateSquare includeone parameter inside it named number, as you
call the name of your function it should displaythe square of the number. Try to pass aparameter value of 9 to the variable number.
3. Create a function named equivalentGrade, includetwo parameter inside named rawScore and
totlScore. When the function is called the ff.output is displayed in the browser:
Given: rawScore=50totlscore=100
A raw score of 50 is equivalent to 75%.