DIG 3563: Lecture 2a: Regular Expressions Michael Moshell University of Central Florida
DIG 3134 Lecture 5: Functions Michael Moshell University of Central Florida
description
Transcript of DIG 3134 Lecture 5: Functions Michael Moshell University of Central Florida
![Page 1: DIG 3134 Lecture 5: Functions Michael Moshell University of Central Florida](https://reader034.fdocuments.in/reader034/viewer/2022051401/56814bb1550346895db884f9/html5/thumbnails/1.jpg)
1
DIG 3134
Lecture 5: Functions
Michael MoshellUniversity of Central Florida
Media Software Design
![Page 2: DIG 3134 Lecture 5: Functions Michael Moshell University of Central Florida](https://reader034.fdocuments.in/reader034/viewer/2022051401/56814bb1550346895db884f9/html5/thumbnails/2.jpg)
2
The Objective:• Learn what a function is,
how to make one,and how to use it.
![Page 3: DIG 3134 Lecture 5: Functions Michael Moshell University of Central Florida](https://reader034.fdocuments.in/reader034/viewer/2022051401/56814bb1550346895db884f9/html5/thumbnails/3.jpg)
3
What is a function?
Think of it as a machine.You put in some inputs, and
it does some work, andproduces some outputs.
4 square($n) 16
print "Four squared is".square(4);
Four squared is 16
![Page 4: DIG 3134 Lecture 5: Functions Michael Moshell University of Central Florida](https://reader034.fdocuments.in/reader034/viewer/2022051401/56814bb1550346895db884f9/html5/thumbnails/4.jpg)
4
How do you make a functionYou define it like this:
function square($n){
$product=$n*$n;return $product;
}
![Page 5: DIG 3134 Lecture 5: Functions Michael Moshell University of Central Florida](https://reader034.fdocuments.in/reader034/viewer/2022051401/56814bb1550346895db884f9/html5/thumbnails/5.jpg)
5
How do you make a functionYou define it like this:
function square($n){
$product=$n*$n;return $product;
}// and then you "Call" it like this.
$x=square(5);// or like this
print square(6);
function name
![Page 6: DIG 3134 Lecture 5: Functions Michael Moshell University of Central Florida](https://reader034.fdocuments.in/reader034/viewer/2022051401/56814bb1550346895db884f9/html5/thumbnails/6.jpg)
6
How do you make a functionYou define it like this:
function square($n){
$product=$n*$n;return $product;
}// and then you "Call" it like this.
$x=square(5);// or like this
print square(6);
input ("argument, or "parameter")
![Page 7: DIG 3134 Lecture 5: Functions Michael Moshell University of Central Florida](https://reader034.fdocuments.in/reader034/viewer/2022051401/56814bb1550346895db884f9/html5/thumbnails/7.jpg)
7
How do you make a functionYou define it like this:
function square($n){
$product=$n*$n;return $product;
}// and then you "Call" it like this.
$x=square(5);// or like this
print square(6);
output, or"return value"
![Page 8: DIG 3134 Lecture 5: Functions Michael Moshell University of Central Florida](https://reader034.fdocuments.in/reader034/viewer/2022051401/56814bb1550346895db884f9/html5/thumbnails/8.jpg)
8
How do you make a functionYou define it like this:
function square($n){
$value=$n*$n;return $value;
}// and then you "Call" it like this.
$x=square(5);// or like this
print square(6);using the function
![Page 9: DIG 3134 Lecture 5: Functions Michael Moshell University of Central Florida](https://reader034.fdocuments.in/reader034/viewer/2022051401/56814bb1550346895db884f9/html5/thumbnails/9.jpg)
9
How do you make a functionYou define it like this:
function square($n){
$value=$n*$n;return $value;
}// and then you "Call" it like this.
$x=square(5);// or like this
print square(6);
$x is now 25
36
![Page 10: DIG 3134 Lecture 5: Functions Michael Moshell University of Central Florida](https://reader034.fdocuments.in/reader034/viewer/2022051401/56814bb1550346895db884f9/html5/thumbnails/10.jpg)
10
Here's a fancier function.
function makebox($height,$width){
$answer='<table>';for ($row=1; $row<=$height;$row++){ $answer .='<tr>';
for ($col=1; $col<=$width; $col++){ $answer.='<td>*</td>'; }$answer .='</tr>';
}$answer .='</table>';return $answer;
}
print makebox(3,4);
* * * ** * * ** * * *
![Page 11: DIG 3134 Lecture 5: Functions Michael Moshell University of Central Florida](https://reader034.fdocuments.in/reader034/viewer/2022051401/56814bb1550346895db884f9/html5/thumbnails/11.jpg)
11
function makebox($height,$width){
$answer='<table>';for ($row=1; $row<=$height;$row++){ $answer .='<tr>';
for ($col=1; $col<=$width; $col++){ $answer.='<td>*</td>'; }$answer .='</tr>';
}$answer .='</table>';return $answer;
}
print makebox(3,4);
* * * ** * * ** * * *
function name
Here's a fancier function.
![Page 12: DIG 3134 Lecture 5: Functions Michael Moshell University of Central Florida](https://reader034.fdocuments.in/reader034/viewer/2022051401/56814bb1550346895db884f9/html5/thumbnails/12.jpg)
12
function makebox($height,$width){
$answer='<table>';for ($row=1; $row<=$height;$row++){ $answer .='<tr>';
for ($col=1; $col<=$width; $col++){ $answer.='<td>*</td>'; }$answer .='</tr>';
}$answer .='</table>';return $answer;
}
print makebox(3,4);
* * * ** * * ** * * *
arguments, or parameters
Here's a fancier function.
![Page 13: DIG 3134 Lecture 5: Functions Michael Moshell University of Central Florida](https://reader034.fdocuments.in/reader034/viewer/2022051401/56814bb1550346895db884f9/html5/thumbnails/13.jpg)
13
function makebox($height,$width){
$answer='<table>';for ($row=1; $row<=$height;$row++){ $answer .='<tr>';
for ($col=1; $col<=$width; $col++){ $answer.='<td>*</td>'; }$answer .='</tr>';
}$answer .='</table>';return $answer;
}
print makebox(3,4);
* * * ** * * ** * * *
output value
Here's a fancier function.
![Page 14: DIG 3134 Lecture 5: Functions Michael Moshell University of Central Florida](https://reader034.fdocuments.in/reader034/viewer/2022051401/56814bb1550346895db884f9/html5/thumbnails/14.jpg)
14
function makebox($height,$width){
$answer='<table>';for ($row=1; $row<=$height;$row++){ $answer .='<tr>';
for ($col=1; $col<=$width; $col++){ $answer.='<td>*</td>'; }$answer .='</tr>';
}$answer .='</table>';return $answer;
}
print makebox(3,4);
* * * ** * * ** * * *
functiondeclaration
Here's a fancier function.
![Page 15: DIG 3134 Lecture 5: Functions Michael Moshell University of Central Florida](https://reader034.fdocuments.in/reader034/viewer/2022051401/56814bb1550346895db884f9/html5/thumbnails/15.jpg)
15
function makebox($height,$width){
$answer='<table>';for ($row=1; $row<=$height;$row++){ $answer .='<tr>';
for ($col=1; $col<=$width; $col++){ $answer.='<td>*</td>'; }$answer .='</tr>';
}$answer .='</table>';return $answer;
}
print makebox(3,4);
* * * ** * * ** * * *
functionInvocation("call")
Here's a fancier function.
![Page 16: DIG 3134 Lecture 5: Functions Michael Moshell University of Central Florida](https://reader034.fdocuments.in/reader034/viewer/2022051401/56814bb1550346895db884f9/html5/thumbnails/16.jpg)
16
Returning a value
function makebox($height,$width){
$answer='<table>';for ($row=1; $row<=$height;$row++){ $answer .='<tr>';
for ($col=1; $col<=$width; $col++){ $answer.='<td>*</td>'; }$answer .='</tr>';
}$answer .='</table>';return $answer;
}
print makebox(3,4);
* * * ** * * ** * * *
Returnstatement
![Page 17: DIG 3134 Lecture 5: Functions Michael Moshell University of Central Florida](https://reader034.fdocuments.in/reader034/viewer/2022051401/56814bb1550346895db884f9/html5/thumbnails/17.jpg)
17
NOT Returning a value
Not all functions have to return a value.Sometimes they just print their results... like this
function printbold($text){
print "<strong>$text</strong>";}
print "I am ";printbold("Mike Moshell");print " and you are not.";
I am Mike Moshell and you are not.
![Page 18: DIG 3134 Lecture 5: Functions Michael Moshell University of Central Florida](https://reader034.fdocuments.in/reader034/viewer/2022051401/56814bb1550346895db884f9/html5/thumbnails/18.jpg)
18
Function Exercise 1
Create a function called print3times($text),that prints three copies of the given text, eachon a separate row. (Use the <br /> tag.)
function print3times($text){?? you design what goes in here.?}
print3times("Hooray no Ike!");Do it now, before we reveal solution.
Hooray no Ike!Hooray no Ike!Hooray no Ike!
![Page 19: DIG 3134 Lecture 5: Functions Michael Moshell University of Central Florida](https://reader034.fdocuments.in/reader034/viewer/2022051401/56814bb1550346895db884f9/html5/thumbnails/19.jpg)
19
Function Exercise 1
Create a function called print3times($text),that produces three copies of the given text, eachon a separate row. (Use the <br /> tag.)
function print3times($text){ // the BFI ("brute force & ignorance") method:
print $text.'<br />';print $text.'<br />';print $text.'<br />';
}
print3times('Hooray no Ike!');
Hooray no Ike!Hooray no Ike!Hooray no Ike!
![Page 20: DIG 3134 Lecture 5: Functions Michael Moshell University of Central Florida](https://reader034.fdocuments.in/reader034/viewer/2022051401/56814bb1550346895db884f9/html5/thumbnails/20.jpg)
20
Function Exercise 1
Create a function called print3times($text),that produces three copies of the given text, eachon a separate row. (Use the <br /> tag.)
function print3times($text){ // the elegant (extensible) method
for ($i=1; $i<=3; $i++){
print $text.'<br />'; }
} # print3times
print3times('Hooray no Ike!');
Hooray no Ike!Hooray no Ike!Hooray no Ike!
![Page 21: DIG 3134 Lecture 5: Functions Michael Moshell University of Central Florida](https://reader034.fdocuments.in/reader034/viewer/2022051401/56814bb1550346895db884f9/html5/thumbnails/21.jpg)
21
Function Exercise 2Each person do one or 'tother.. NOW
SIMPLE Create a function called polite($g,$name)where input $g will be 'm' or 'f' for male or female.It prints "Good morning Mr. Smith" or "Good morning, Ms. Smith"
based on input like polite('m','Smith').
ADVANCED: In the fictional language Ispanglish, all femalenames end in 'a' and all male names end in 'e'. Produce a functioncalled ispolite($name) that prints
"Boonie Doozie, Madama Elena" for a female name like Elena,"Boonie Doozie, Maestro Mike' for a mail name like Mike.
![Page 22: DIG 3134 Lecture 5: Functions Michael Moshell University of Central Florida](https://reader034.fdocuments.in/reader034/viewer/2022051401/56814bb1550346895db884f9/html5/thumbnails/22.jpg)
22
Function Exercise 2SIMPLE Create a function called polite($g,$name)where input $g will be 'm' or 'f' for male or female.It prints "Good morning Mr. Smith."
or "Good morning, Ms. Smith."
based on input like this: polite('m','Smith').
That is, the parameter $g will have value 'm' or 'f'.
![Page 23: DIG 3134 Lecture 5: Functions Michael Moshell University of Central Florida](https://reader034.fdocuments.in/reader034/viewer/2022051401/56814bb1550346895db884f9/html5/thumbnails/23.jpg)
23
Function Exercise 2SIMPLE Create a function called polite($g,$name)where input $g will be 'm' or 'f' for male or female.It prints "Good morning Mr. Smith."
or "Good morning, Ms. Smith."
based on input like this: polite('m','Smith').
That is, the parameter $g will have value 'm' or 'f'.
((Don't look here if you want to figure it outfor yourself!))
Hint->
![Page 24: DIG 3134 Lecture 5: Functions Michael Moshell University of Central Florida](https://reader034.fdocuments.in/reader034/viewer/2022051401/56814bb1550346895db884f9/html5/thumbnails/24.jpg)
24
Function Exercise 2SIMPLE Create a function called polite($g,$name)where input $g will be 'm' or 'f' for male or female.It prints "Good morning Mr. Smith."
or "Good morning, Ms. Smith."
based on input like this: polite('m','Smith').
That is, the parameter $g will have value 'm' or 'f'.
function polite ($g, $name){
if (something) {print "Good morning Mr. $name";}
else { print "Good morning Ms. $name." }}polite('m','Jones'); polite('f','Ramirez');
Hint->
Calls->
![Page 25: DIG 3134 Lecture 5: Functions Michael Moshell University of Central Florida](https://reader034.fdocuments.in/reader034/viewer/2022051401/56814bb1550346895db884f9/html5/thumbnails/25.jpg)
25
Function Exercise 2
SIMPLE Create a function called polite($g,$name)where input $g will be 'm' or 'f' for male or female.It prints "Good morning Mr. Smith, or Ms. Smith.",based on input like polite('m','Smith').
function polite($g,$name){
if ($g=='m'){ print "Good morning Mr. $name";}else{ print "Good morning Ms. $name";}
}
Basic Solution
![Page 26: DIG 3134 Lecture 5: Functions Michael Moshell University of Central Florida](https://reader034.fdocuments.in/reader034/viewer/2022051401/56814bb1550346895db884f9/html5/thumbnails/26.jpg)
26
Function Exercise 2
SIMPLE Create a function called polite($g,$name)where input $g will be 'm' or 'f' for male or female.It prints "Good morning Mr. Smith, or Ms. Smith.",based on input like polite('m','Smith').
function polite($g,$name){
if ($g=='m'){ print "Good morning Mr. $name";}else if ($g == 'f'){ print "Good morning Ms. $name";}else{ print "error 001: unknown gender selector $g.";}
}
Better Solution
- covers all cases
- numbered error msgs
- specific feedback on type of error (could be better)
![Page 27: DIG 3134 Lecture 5: Functions Michael Moshell University of Central Florida](https://reader034.fdocuments.in/reader034/viewer/2022051401/56814bb1550346895db884f9/html5/thumbnails/27.jpg)
27
Function Exercise 2ADVANCED: In the fictional language Ispanglish, all femalenames end in 'a' and all male names end in 'e'. Produce a functioncalled ispolite($name) that prints "Boonie Doozie, Madama Elena" for a female name like Elena, "Boonie Doozie, Maestro Mike' for a mail name like Mike.
-- Advanced students:Work on this problem for homework –
You can check your work against my solution,on the last slide of this Powerpoint document.
![Page 28: DIG 3134 Lecture 5: Functions Michael Moshell University of Central Florida](https://reader034.fdocuments.in/reader034/viewer/2022051401/56814bb1550346895db884f9/html5/thumbnails/28.jpg)
28
The Scope of Variables
Variables created inside a function cannot beseen outside that function.Variables created outside a function cannot beseen inside that function.
$x=3;function monster( ){
print "inside first x=$x";$x=5; print " second inside x=$x";
}monster( );print "outside x=$x";
declare function 'monster'first pass: check syntax,
![Page 29: DIG 3134 Lecture 5: Functions Michael Moshell University of Central Florida](https://reader034.fdocuments.in/reader034/viewer/2022051401/56814bb1550346895db884f9/html5/thumbnails/29.jpg)
29
The Scope of Variables
Variables created inside a function cannot beseen outside that function.Variables created outside a function cannot beseen inside that function.
$x=3;function monster( ){
print "inside first x=$x";$x=5; print " second inside x=$x";
}monster( );print "outside x=$x";
outside x::3
second pass:execute code.
variable xis createdin globalnamespace
![Page 30: DIG 3134 Lecture 5: Functions Michael Moshell University of Central Florida](https://reader034.fdocuments.in/reader034/viewer/2022051401/56814bb1550346895db884f9/html5/thumbnails/30.jpg)
30
The Scope of Variables
Variables created inside a function cannot beseen outside that function.Variables created outside a function cannot beseen inside that function.
$x=3;function monster( ){
print "inside first x=$x";$x=5; print " second inside x=$x";
}monster( );print "outside x=$x";
x::3
invoking 'monster'
![Page 31: DIG 3134 Lecture 5: Functions Michael Moshell University of Central Florida](https://reader034.fdocuments.in/reader034/viewer/2022051401/56814bb1550346895db884f9/html5/thumbnails/31.jpg)
31
The Scope of Variables
Variables created inside a function cannot beseen outside that function.Variables created outside a function cannot beseen inside that function.
$x=3;function monster( ){
print “first inside x=$x”;$x=5; print " second inside x=$x";
}monster( );print "outside x=$x";
x::3
invoking 'monster'
first inside x=
![Page 32: DIG 3134 Lecture 5: Functions Michael Moshell University of Central Florida](https://reader034.fdocuments.in/reader034/viewer/2022051401/56814bb1550346895db884f9/html5/thumbnails/32.jpg)
32
The Scope of Variables
Variables created inside a function cannot beseen outside that function.Variables created outside a function cannot beseen inside that function.
$x=3;function monster( ){
print "inside first x=$x";$x=5; print " second inside x=$x";
}monster( );print "outside x=$x";
x::3
invoking 'monster'
first inside x=x::5
another xis createdinside the function
![Page 33: DIG 3134 Lecture 5: Functions Michael Moshell University of Central Florida](https://reader034.fdocuments.in/reader034/viewer/2022051401/56814bb1550346895db884f9/html5/thumbnails/33.jpg)
33
The Scope of Variables
Variables created inside a function cannot beseen outside that function.Variables created outside a function cannot beseen inside that function.
$x=3;function monster( ){
print "inside first x=$x";$x=5; print " second inside x=$x";
}monster( );print "outside x=$x";
x::3
invoking 'monster'
first inside x=
second inside x=5x::5
![Page 34: DIG 3134 Lecture 5: Functions Michael Moshell University of Central Florida](https://reader034.fdocuments.in/reader034/viewer/2022051401/56814bb1550346895db884f9/html5/thumbnails/34.jpg)
34
The Scope of Variables
Variables created inside a function cannot beseen outside that function.Variables created outside a function cannot beseen inside that function.
$x=3;function monster( ){
print "inside first x=$x";$x=5; print " second inside x=$x";
}monster( );print "outside x=$x";
x::3
first inside x=
second inside x=5
outside x=3
![Page 35: DIG 3134 Lecture 5: Functions Michael Moshell University of Central Florida](https://reader034.fdocuments.in/reader034/viewer/2022051401/56814bb1550346895db884f9/html5/thumbnails/35.jpg)
35
The Global Declaration
If you declare a variable 'global', the functionuses the variable from the outside world.
$x=3;function monster( ){ global $x;
print "inside first x=$x";$x=5; print " second inside x=$x";
}monster( );print "outside x=$x";
x::3
![Page 36: DIG 3134 Lecture 5: Functions Michael Moshell University of Central Florida](https://reader034.fdocuments.in/reader034/viewer/2022051401/56814bb1550346895db884f9/html5/thumbnails/36.jpg)
36
The Global Declaration
If you declare a variable 'global', the functionuses the variable from the outside world.
$x=3;function monster( ){ global $x;
print "inside first x=$x";$x=5; print " second inside x=$x";
}monster( );print "outside x=$x";
x::3
first inside x=3
![Page 37: DIG 3134 Lecture 5: Functions Michael Moshell University of Central Florida](https://reader034.fdocuments.in/reader034/viewer/2022051401/56814bb1550346895db884f9/html5/thumbnails/37.jpg)
37
The Global Declaration
If you declare a variable 'global', the functionuses the variable from the outside world.
$x=3;function monster( ){ global $x;
print "inside first x=$x";$x=5; print " second inside x=$x";
}monster( );print "outside x=$x";
x::5
first inside x=3
![Page 38: DIG 3134 Lecture 5: Functions Michael Moshell University of Central Florida](https://reader034.fdocuments.in/reader034/viewer/2022051401/56814bb1550346895db884f9/html5/thumbnails/38.jpg)
38
The Global Declaration
If you declare a variable 'global', the functionuses the variable from the outside world.
$x=3;function monster( ){ global $x;
print "inside first x=$x";$x=5; print " second inside x=$x";
}monster( );print "outside x=$x";
x::5
first inside x=3
second inside x=5
![Page 39: DIG 3134 Lecture 5: Functions Michael Moshell University of Central Florida](https://reader034.fdocuments.in/reader034/viewer/2022051401/56814bb1550346895db884f9/html5/thumbnails/39.jpg)
39
The Global Declaration
If you declare a variable 'global', the functionuses the variable from the outside world.
$x=3;function monster( ){ global $x;
print "inside first x=$x";$x=5; print " second inside x=$x";
}monster( );print "outside x=$x";
x::5
first inside x=3
second inside x=5
outside x=5
![Page 40: DIG 3134 Lecture 5: Functions Michael Moshell University of Central Florida](https://reader034.fdocuments.in/reader034/viewer/2022051401/56814bb1550346895db884f9/html5/thumbnails/40.jpg)
40
A parameter can be an arrayConsider this example:
$invoice[0]=24.95;$invoice[1]=188.40;$invoice[2]=33.22;...Here's a function to compute the total of the invoices.
function total($n,$list){ $sum=0;
for ($i=0; $i<$n; $i++){
$sum=$sum+$list[$i];}return $sum;
}print “Total charge:”. total(3,$invoice);
![Page 41: DIG 3134 Lecture 5: Functions Michael Moshell University of Central Florida](https://reader034.fdocuments.in/reader034/viewer/2022051401/56814bb1550346895db884f9/html5/thumbnails/41.jpg)
41
Advanced topic:The Foreach command
Consider this example:
$partypeople[0]="Maria";$partypeople[1]="Alfonso";$partypeople[2]="Santa Claus";$partypeople[3]="Billy";...
but maybe we don't KNOW how many people are in the list.(It was created somewhere else in the code.)
So we just need to have a way to say "For ALL the list's elements.."
![Page 42: DIG 3134 Lecture 5: Functions Michael Moshell University of Central Florida](https://reader034.fdocuments.in/reader034/viewer/2022051401/56814bb1550346895db884f9/html5/thumbnails/42.jpg)
42
function hasFred($list){
foreach ($list as $value) // no key needed this time{
if ($value= = 'Fred'){ return 1;}
} // if you got here, no Fred was foundreturn 0;
}
if (hasFred($partypeople)) { print "Yay, Fred will come!"; }else { print "Boo hoo, Fred won't come to our party!"; }
Advanced topic:The Foreach command
Consider this example:
![Page 43: DIG 3134 Lecture 5: Functions Michael Moshell University of Central Florida](https://reader034.fdocuments.in/reader034/viewer/2022051401/56814bb1550346895db884f9/html5/thumbnails/43.jpg)
43
Function HomeworkDesign and program these functions for next week
1. function punchfred($who)If the parameter value is 'Fred' it prints out
"I punch you in the mouf, Fred."If any other name (like Suzie), it prints out
"A very good morning to you, Suzie!"
2. function maxtwo($x,$y)It returns the value of the larger of its two inputs.
3. function maxlist($list, $length)It returns the largest value in an array ($list) whose
cells are numbered 0, 1, .... ($length-1).
![Page 44: DIG 3134 Lecture 5: Functions Michael Moshell University of Central Florida](https://reader034.fdocuments.in/reader034/viewer/2022051401/56814bb1550346895db884f9/html5/thumbnails/44.jpg)
44
An Answer to Function Exercise 2ADVANCED: In the fictional language Ispanglish, all femalenames end in 'a' and all male names end in 'e'. Produce a functioncalled ispolite($name) that prints "Boonie Doozie, Madama Elena" for a female name like Elena, "Boonie Doozie, Maestro Mike' for a mail name like Mike.
Spoiler Alert:Do not view the next slide unless you want toexamine an answer to the Ispanglish exercise above.
![Page 45: DIG 3134 Lecture 5: Functions Michael Moshell University of Central Florida](https://reader034.fdocuments.in/reader034/viewer/2022051401/56814bb1550346895db884f9/html5/thumbnails/45.jpg)
45
An Answer to Function Exercise 2ADVANCED: In the fictional language Ispanglish, all femalenames end in 'a' and all male names end in 'e'. Produce a functioncalled ispolite($name) that prints "Boonie Doozie, Madama Elena" for a female name like Elena, "Boonie Doozie, Maestro Mike' for a mail name like Mike.
function ispolite($name){ $len=strlen($name);
$last=substr($name,$len-1,1);if ($last = = 'a'){ print "Boonie Doozie, Madama $name"; }else{ print "Boonie Doozie, Maestro $name"; }
} // It SHOULD include an error case ... no space here!