190 likes | 355 Views
CSCI 116. Functions. Functions. A group of statements that you can execute as a single unit May or may not accept parameters An input to the function Placed inside parentheses May or may not return a value An output of the function. Built-in Functions. See http://php.net/manual
E N D
CSCI 116 Functions
Functions • A group of statements that you can execute as a single unit • May or may not accept parameters • An input to the function • Placed inside parentheses • May or may not return a value • An output of the function
Built-in Functions • See http://php.net/manual • Type function name in Search box parameters array explode ( string $delimiter , string $string [, int$limit ] ) return value optional
Invoking a Function • Pass an argument for each parameter • “Capture” a return value, if there is one • Assign to a variable • As an argument of another function • In a print or echo statement • In a decision $str = “Tom^Dick^Harry”; $arr = explode (“^”, $str); print_r($arr);
Built-in Functions What are the parameters? What are the return values? How might you invoke the function? • addslashes • string addslashes ( string $str ) • $newPhrase = addslashes($oldPhrase); • pow • number pow ( number $base , number $exp ) • if(pow(2, 3) > 5) … • str_repeat • string str_repeat ( string $input , int $multiplier ) • print str_repeat(“Hello”, 5); • rand • int rand ( int $min , int $max ) • $randomNumber = rand(1, 10);
Defining Functions <?php function name_of_function(parameters) { statements; } ?> A function may have zero or more parameters
Function Example function printPhrase($phrase) { echo “<p>$phrase</p>”; } printPhrase(“Silly goose!”); defining the function: invoking the function:
Returning Values • A return statement returns a value to the statement that called (invoked) the function • A function does not have to return a value function averageNumbers($a, $b, $c) { $sumOfNumbers = $a + $b + $c; $average = $sumOfNumbers / 3; return $average; }
Function Practice Define a function greetingthat takes a name as a parameter and prints “Hello, name!”. (This function has no return value.) Invoke the function function greeting($name) { print “Hello, $name!”; } greeting(“Sam”);
Function Practice Write a function average that returns the average of two values. Invoke the function. function average($num1, $num2) { $avg = ($num1 + $num2)/2; return $avg; } $a = 5; $b = 3; print “The average of $a and $b is ” . average($a, $b);
Function Practice Write a function largest that returns the maximum of two values. Invoke the function. function largest($num1, $num2) { if($num1 > $num2) return $num1; else return $num2; } $a = 5; $b = 3; print “The largest of $a and $b is ” . largest($a, $b);
Function Practice Write a function circumference that takes a radius and returns the circumference of a circle (C=3.14*Diameter). Invoke the function. function circumference($radius) { $circ = 3.14 * 2 * $radius; return $circ; } $radius = 5; print “The circumference is ” . circumference($radius);
Setting Default Parameter Values defining the function: function printPhrase($phrase = “Quack”) { echo “<p>$phrase</p>”; } printPhrase(); invoking the function:
Understanding Variable Scope • Variable scope • Where in your program a declared variable can be used • Can be either global or local • Global variable • Declared outside a function and available to all parts of your program • Local variable • Declared inside a function and only available within that function
Variable Scope <?php $globalVar = "Global"; function scopeExample() { global $globalVar; echo "<b>Inside function:</b><br />"; $localVar = "Local"; echo "$localVar<br/>"; echo $globalVar.”<br/><br />"; } scopeExample(); echo "<b>Outside function:</b><br />"; echo "$localVar<br />"; echo "$globalVar<br />"; ?> global keyword used inside function to reference global variable
Variable Scope <?php $globalVar = "Global"; function scopeExample() { echo "<b>Inside function:</b><br />"; $localVar = "Local"; echo "$localVar<br/>"; echo $GLOBALS[‘globalVar’].”<br/><br />"; } scopeExample(); echo "<b>Outside function:</b><br />"; echo "$localVar<br />"; echo "$globalVar<br />"; ?> $GLOBALS array used inside function to reference global variable
functions.php Include Files • <?php • /* circumference takes a radius • * and returns the circumference • * of a circle. • */ • function circumference($radius) • { • $circ = 3.14 * 2 * $radius; • return $circ; • } • /* area takes a radius and returns • * the area of a circle. • */ • function area($radius) • { • $area = 3.14 * pow($radius, 2); • return $area; • } • ?>
Include Files testScript.php • <?php • include 'functions.php'; • $radius = 5; • print "The circumference is " . • circumference($radius); • print "The area is " . area($radius); • ?>
Why use Functions? • Reusability • The same function can be used more than once • Within the same script • Across different scrips • Function libraries • Functions make your code: • Easier to read • Easier to modify • Easier to maintain