Jump to content


Photo

Functions


  • Please log in to reply
7 replies to this topic

#1 master82

master82
  • Members
  • PipPipPip
  • Advanced Member
  • 182 posts

Posted 13 August 2006 - 06:34 PM

I've just come across some code that had alot of functions within it - can somebody please try to explain the purpose of these and maybe give a practicle example to get the point across

Thanks for your time  ;)

#2 hitman6003

hitman6003
  • Members
  • PipPipPip
  • Advanced Member
  • 1,807 posts

Posted 13 August 2006 - 06:43 PM

The first place to look should always be the manual:

http://www.php.net/functions

Be sure to read the sub sections as well.

If you still have questions, please ask after reading the manual.

#3 master82

master82
  • Members
  • PipPipPip
  • Advanced Member
  • 182 posts

Posted 13 August 2006 - 06:47 PM

Sorry, should have said - I looked there and it made no sense to me! lol

Thats why I was hoping someone could put an easy/simple example of a function they have used then explain exactly what is happening.

#4 hitman6003

hitman6003
  • Members
  • PipPipPip
  • Advanced Member
  • 1,807 posts

Posted 13 August 2006 - 06:52 PM

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:

function foo($arg1) {
  echo $arg1;
}

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:

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

And now all text will be red.


#5 master82

master82
  • Members
  • PipPipPip
  • Advanced Member
  • 182 posts

Posted 13 August 2006 - 07:00 PM

Oh... Im starting to see it now.

Am i right in thinking that foo is the name of the function?

How would I call that function if it was on a different php page?

(use require page.php and use $foo ?)

#6 hitman6003

hitman6003
  • Members
  • PipPipPip
  • Advanced Member
  • 1,807 posts

Posted 13 August 2006 - 07:08 PM

include the file that the function is in, then call it like any other php function:

foo("this is a test");


#7 extrovertive

extrovertive
  • Members
  • PipPipPip
  • Advanced Member
  • 235 posts

Posted 13 August 2006 - 09:02 PM

a function is nothing more than an equation.

x = 5;

x = 2 *x;

x is now (2 *5 ) which is 10.

$x = 2;

echo timestwo($x);

function timestwo($x)
{
return (2 * $x);
}

#8 ScottRiley

ScottRiley
  • Members
  • PipPipPip
  • Advanced Member
  • 59 posts

Posted 13 August 2006 - 09:21 PM

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.





0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users