1 / 19

CSCI 116

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

barr
Download Presentation

CSCI 116

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. CSCI 116 Functions

  2. 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

  3. 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

  4. 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);

  5. 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);

  6. Defining Functions <?php function name_of_function(parameters) { statements; } ?> A function may have zero or more parameters

  7. Function Example function printPhrase($phrase) { echo “<p>$phrase</p>”; } printPhrase(“Silly goose!”); defining the function: invoking the function:

  8. 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; }

  9. 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”);

  10. 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);

  11. 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);

  12. 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);

  13. Setting Default Parameter Values defining the function: function printPhrase($phrase = “Quack”) { echo “<p>$phrase</p>”; } printPhrase(); invoking the function:

  14. 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

  15. 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

  16. 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

  17. 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; • } • ?>

  18. Include Files testScript.php • <?php • include 'functions.php'; • $radius = 5; • print "The circumference is " . • circumference($radius); • print "The area is " . area($radius); • ?>

  19. 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

More Related