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.

{[ snackBarMessage ]}

### What students are saying

• As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

Kiran Temple University Fox School of Business ‘17, Course Hero Intern

• I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

Dana University of Pennsylvania ‘17, Course Hero Intern

• The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

Jill Tulane University ‘16, Course Hero Intern