lecture4.functions

# lecture4.functions - Media Software Design DIG 3134 Lecture...

This preview shows pages 1–18. Sign up to view the full content.

1 DIG 3134 Lecture 4: Functions Michael Moshell University of Central Florida Media Software Design

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
2 The Objective: Learn what a function is, how to make one, and how to use it.
3 What is a function? 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

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
4 How do you make a function You define it like this: function square(\$n) { \$product=\$n*\$n; return \$product; }
5 How do you make a function 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); function name

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
6 How do you make a function 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")
7 How do you make a function 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"

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
8 How do you make a 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); using the function
9 How do you make a 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

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
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); * * * * * * * * * * * *
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.

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
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.
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.

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
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); * * * * * * * * * * * * function declaration Here's a fancier function.
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); * * * * * * * * * * * * function Invocation ("call") Here's a fancier function.

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
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); * * * * * * * * * * * * Return statement
17 NOT Returning a value Not all functions have to return a value.

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
This is the end of the preview. Sign up to access the rest of the document.

## This note was uploaded on 08/25/2011 for the course DIG 3134 taught by Professor Moshell during the Fall '11 term at University of Central Florida.

### Page1 / 45

lecture4.functions - Media Software Design DIG 3134 Lecture...

This preview shows document pages 1 - 18. Sign up to view the full document.

View Full Document
Ask a homework question - tutors are online