Jump to content

Functions


master82

Recommended Posts

The point of a function is to wrap some lines of code that you plan on using more than once into a single package.  This way if you need to change that code, you only have to do it once, rather than digging through all of your lines of code to find every instance.

For example:

[code]function foo($arg1) {
  echo $arg1;
}[/code]

Very simple function, just echo's out what it is passed.  You could use that function to output all of your data to the browser, for example.  Now, if you weren't using that function, and you wanted to make all text output to the user red, you would have to dig through your code and put a style tag around each one.  But with the function it's as easy as:

[code]function foo($arg1) {
  echo '<span style="color: red;">' . $arg1 . '</span>';
}[/code]

And now all text will be red.
Link to comment
Share on other sites

Generally, the purpose of a function is to prevent the repetition of code.  You create a function to perform  certain task that could be extremely inefficient to type out over and over again.  where a simple example of a function would be to add 1 to a number,

function addone($number)
{
    $number++;
}

This could be called like '$newnumber = addone($oldnumber)', which is actually less efficient to type thatn $oldnumber++...howver, it illustrates the use of a function.  Now, consider having to echo a large amount of HTML, this could exceed 200 lines.  Say, for example, you needed to show this HTML whenever a user-error occured (e.g. they didn't fill in a field in a form, they chose an incorrect answer in a quiz).  Now, without functions, for every error that occured, you would have to copy and paste the echo statement for every error.  This means your code could easily exceed 1,000 lines.  Now, consider this with functions.  You declare the function, in which you have the echo statement, then you just call the function ater every error.  this means, instead of 200 lines of code for every error, there is potentially just one.

Also, assume you made an error in the echo statement, if you had copied and pasted the statement, you would have to ammend the statement for each error.  If you used a single function, you would need to edit this once.
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.