JavaScript Editor Javascript validator     Website design 



Team LiB
Previous Section Next Section

Using Functions to Encapsulate Parts of the Program

It hasn't taken long for your programs to get complex. As soon as the code gets a little bit larger than the size of a screen in your editor, it gets much harder to keep track of. Programmers like to break up code into smaller segments called functions to help keep everything straight. A function is like a miniature program. It should be designed to do one job well. As a silly example, look at Figure 3.12.

Click To expand
Figure 3.12: This song has a straightforward verse, chorus, verse, chorus pattern.

Examining the This Old Man Program

Song lyrics often have a very repetitive nature. The This Old Man song shown in Figure 3.12 is a good example. Each verse is different, but the chorus is always the same. Often when you write the lyrics to such a song, you will write out each verse, but you'll only write the chorus once. After that, you simply write "chorus" and people generally understand they are not to sing the word "chorus" but to repeat the song's chorus. This works very much like subroutines in programming language. The code for the thisOldMan program illustrates how it works.

<html>
<head>
<title>This Old Man</title>
</head>
<body>
<h1>This Old Man</h1>
<h3>Demonstrates use of functions</h3>
<?

verse1();
chorus();
verse2();
chorus();

function verse1(){
  print <<<HERE
  This old man, he played 1<br>
  He played knick-knack on my thumb<br><br>
HERE;
} // end verse1

function verse2(){
  print <<<HERE
  This old man, he played 2<br>
  He played knick-knack on my shoe<br><br>
HERE;
} // end verse1

function chorus(){
  print <<<HERE
  ...with a knick-knack<br>
  paddy-whack<br>
  giva a dog a bone<br>
  this old man came rolling home<br>
  <br><br>
} // end chorus

?>
</body>
</html>

Careful examination of this code will show how it works. The main part of the program is extremely simple:

verse1();
chorus();
verse2();
chorus();

Creating New Functions

There appear to be some new PHP functions. I called the verse1() function, then the chorus() function, and so on. There are some new functions, but they weren't shipped with PHP. Instead, I made them as part of the page. You can take a set of instructions and store them with a name. This essentially builds a new temporary command in PHP, so you can combine simple commands to do complex things. Building a function is simple. Use the keyword function followed by the function's name and a set of parentheses. Keep the parentheses empty for now. You'll learn how to use this feature in the next section. Use a pair of braces ({}) to combine a series of code lines into one function. Don't forget the right brace (}) to end the function definition. It's smart to indent everything between the beginning and end of a function.

TRICK 

When you look at my code, you'll note there's one line I never indent. That's the HERE token used for multi-line strings. Recall that the word HERE is acting like a closing quote, and it must be all the way on the left side of the screen, so it can't be indented.

TRAP 

Although you can use any function name you like, be careful. If you try to define a function that already exists, you're bound to get confused. PHP has a large number of functions already built in. If you're having strange problems with a function, you might look at the online help (it comes with PHP, or you can get it at www.php.net) to see if that function already exists.

The chorus() function is especially handy in this program because it can be re-used. It isn't necessary to re-write the code for the chorus each time when you can simply call a function instead.


Team LiB
Previous Section Next Section


JavaScript Editor Javascript validator     Website design


essay services