1 / 45

# DIG 3134 Lecture 5: Functions Michael Moshell University of Central Florida - PowerPoint PPT Presentation

Media Software Design. DIG 3134 Lecture 5: Functions Michael Moshell University of Central Florida. The Objective: Learn what a function is, how to make one, and how to use it. What is a function? Think of it as a machine. You put in some inputs, and it does some work, and

I am the owner, or an agent authorized to act on behalf of the owner, of the copyrighted work described.

## PowerPoint Slideshow about 'DIG 3134 Lecture 5: Functions Michael Moshell University of Central Florida' - floria

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.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.

- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -
Presentation Transcript

DIG 3134

Lecture 5: Functions

Michael Moshell

University of Central Florida

• The Objective:

• Learn what a function is,

• how to make one,

• and how to use it.

Think of it as a machine.

You put in some inputs, and

it does some work, and

produces some outputs.

4

square(\$n)

16

print "Four squared is".square(4);

Four squared is 16

You define it like this:

function square(\$n)

{

\$product=\$n*\$n;

return \$product;

}

You define it like this:

function name

function square(\$n)

{

\$product=\$n*\$n;

return \$product;

}

// and then you "Call" it like this.

\$x=square(5);

// or like this

print square(6);

You 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")

You 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"

You 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

You 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

function makebox(\$height,\$width)

{

for (\$row=1; \$row<=\$height;\$row++)

for (\$col=1; \$col<=\$width; \$col++)

}

}

* * * *

* * * *

* * * *

print makebox(3,4);

function makebox(\$height,\$width)

{

for (\$row=1; \$row<=\$height;\$row++)

for (\$col=1; \$col<=\$width; \$col++)

}

}

function name

* * * *

* * * *

* * * *

print makebox(3,4);

function makebox(\$height,\$width)

{

for (\$row=1; \$row<=\$height;\$row++)

for (\$col=1; \$col<=\$width; \$col++)

}

}

arguments, or parameters

* * * *

* * * *

* * * *

print makebox(3,4);

function makebox(\$height,\$width)

{

for (\$row=1; \$row<=\$height;\$row++)

for (\$col=1; \$col<=\$width; \$col++)

}

}

output value

* * * *

* * * *

* * * *

print makebox(3,4);

function makebox(\$height,\$width)

{

for (\$row=1; \$row<=\$height;\$row++)

for (\$col=1; \$col<=\$width; \$col++)

}

}

function

declaration

* * * *

* * * *

* * * *

print makebox(3,4);

function makebox(\$height,\$width)

{

for (\$row=1; \$row<=\$height;\$row++)

for (\$col=1; \$col<=\$width; \$col++)

}

}

function

Invocation

("call")

* * * *

* * * *

* * * *

print makebox(3,4);

function makebox(\$height,\$width)

{

for (\$row=1; \$row<=\$height;\$row++)

for (\$col=1; \$col<=\$width; \$col++)

}

}

Return

statement

* * * *

* * * *

* * * *

print makebox(3,4);

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.

Create a function called print3times(\$text),

that prints three copies of the given text, each

on 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!

Create a function called print3times(\$text),

that produces three copies of the given text, each

on 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!

Create a function called print3times(\$text),

that produces three copies of the given text, each

on 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!

Each 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 female

names end in 'a' and all male names end in 'e'. Produce a function

called ispolite(\$name) that prints

"Boonie Doozie, Madama Elena" for a female name like Elena,

"Boonie Doozie, Maestro Mike' for a mail name like Mike.

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 this: polite('m','Smith').

That is, the parameter \$g will have value 'm' or 'f'.

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 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 out

for yourself!))

Hint->

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

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').

Basic Solution

function polite(\$g,\$name)

{

if (\$g=='m')

{ print "Good morning Mr. \$name";}

else

{ print "Good morning Ms. \$name";}

}

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').

Better Solution

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

}

- covers all cases

- numbered error msgs

- specific feedback on type of error (could be better)

ADVANCED: In the fictional language Ispanglish, all female

names end in 'a' and all male names end in 'e'. Produce a function

called ispolite(\$name) that prints

"Boonie Doozie, Madama Elena" for a female name like Elena,

"Boonie Doozie, Maestro Mike' for a mail name like Mike.

Work on this problem for homework –

You can check your work against my solution,

on the last slide of this Powerpoint document.

Variables created inside a function cannot be

seen outside that function.

Variables created outside a function cannot be

seen inside that function.

first pass: check syntax,

\$x=3;

function monster( )

{

print "inside first x=\$x";

\$x=5;

print " second inside x=\$x";

}

monster( );

print "outside x=\$x";

declare function 'monster'

Variables created inside a function cannot be

seen outside that function.

Variables created outside a function cannot be

seen inside that function.

second pass:execute code.

\$x=3;

function monster( )

{

print "inside first x=\$x";

\$x=5;

print " second inside x=\$x";

}

monster( );

print "outside x=\$x";

outside x::3

variable x

is created

in global

namespace

Variables created inside a function cannot be

seen outside that function.

Variables created outside a function cannot be

seen 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'

Variables created inside a function cannot be

seen outside that function.

Variables created outside a function cannot be

seen 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

first inside x=

invoking 'monster'

Variables created inside a function cannot be

seen outside that function.

Variables created outside a function cannot be

seen 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=

x::5

another x

is created

inside the function

invoking 'monster'

Variables created inside a function cannot be

seen outside that function.

Variables created outside a function cannot be

seen 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

x::5

invoking 'monster'

Variables created inside a function cannot be

seen outside that function.

Variables created outside a function cannot be

seen 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

If you declare a variable 'global', the function

uses 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

If you declare a variable 'global', the function

uses 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

If you declare a variable 'global', the function

uses 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

If you declare a variable 'global', the function

uses 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

If you declare a variable 'global', the function

uses 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

Consider 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);

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

The Foreach command

Consider this example:

function hasFred(\$list)

{

foreach (\$list as \$value) // no key needed this time

{

if (\$value= = 'Fred')

{ return 1;}

} // if you got here, no Fred was found

return 0;

}

if (hasFred(\$partypeople)) { print "Yay, Fred will come!"; }

else { print "Boo hoo, Fred won't come to our party!"; }

Design 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).

ADVANCED: In the fictional language Ispanglish, all female

names end in 'a' and all male names end in 'e'. Produce a function

called ispolite(\$name) that prints

"Boonie Doozie, Madama Elena" for a female name like Elena,

"Boonie Doozie, Maestro Mike' for a mail name like Mike.

Do not view the next slide unless you want to

examine an answer to the Ispanglish exercise above.

ADVANCED: In the fictional language Ispanglish, all female

names end in 'a' and all male names end in 'e'. Produce a function

called 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!