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

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

View Full Document Right Arrow Icon
1 DIG 3134 Lecture 4: Functions Michael Moshell University of Central Florida Media Software Design
Image of page 1

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

View Full Document Right Arrow Icon
2 The Objective: Learn what a function is, how to make one, and how to use it.
Image of page 2
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
Image of page 3

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

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

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

View Full Document Right Arrow Icon
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")
Image of page 6
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"
Image of page 7

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

View Full Document Right Arrow Icon
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
Image of page 8
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
Image of page 9

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

View Full Document Right Arrow Icon
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); * * * * * * * * * * * *
Image of page 10
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.
Image of page 11

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

View Full Document Right Arrow Icon
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.
Image of page 12
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.
Image of page 13

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

View Full Document Right Arrow Icon
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.
Image of page 14
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.
Image of page 15

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

View Full Document Right Arrow Icon
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
Image of page 16
17 NOT Returning a value Not all functions have to return a value.
Image of page 17

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

View Full Document Right Arrow Icon
Image of page 18
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

What students are saying

  • Left Quote Icon

    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.

    Student Picture

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

  • Left Quote Icon

    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.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    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.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern