Page 1 of 1

Functions

Posted: Wed Aug 28, 2002 2:51 pm
by Xerpher
Lets say I make a function that takes the inputed value and manipulates it then orints it out... That all works great for me, but I was wondering if I could make my function be used without having to put the '(' and ')' just like how the print function doesn't need those, it works with just the double quotes.

Posted: Thu Aug 29, 2002 2:32 am
by Jay
Any user defined function HAS to be in the format of:
functionName ([arg1.[arg2[arg3...]]]) { function parameters }

You can't create a function without the paranthesis otherwise it wouldn't know where to get the variables from!

Posted: Thu Aug 29, 2002 4:53 am
by Xerpher
I didn't mean the actual function creation... I meant when you call the function. For example:

print ("Welcome!") // This outputs "Welcome!"
print "Welcome!" // This outputs "Welcome!"

Whether you use the parentheses or not, the print function still works... Can I do that to my own functions?

Posted: Thu Aug 29, 2002 4:55 am
by elitecodex
No
At least not to my knowledge

Posted: Thu Aug 29, 2002 2:06 pm
by Jay
No, don't try to copy the built-in PHP functions!

If you made your own function like so
function myFunct($arg) {
$arg .= $arg;
return $arg
}

you could write the following code:
print myFunct("Some text");

which will output
Some textSome text

But when you call a function, even one with no additional parameters, you have to use the paranthesis

Posted: Thu Aug 29, 2002 3:02 pm
by Xerpher
Or I could make it simpler by doing this...

If you made your own function like so
function myFunct($arg) {
$arg .= $arg;
print $arg
}

you could write the following code:
myFunct("Some text");

which will output
Some textSome text

Posted: Fri Aug 30, 2002 12:53 am
by Jay
True, but I find the 'return' idea to be much more useful as a function because you've still got an element of control over the value it returns, and you can pass it through another function, say.

If you're function is printing it out, then you're stuffed if you want to manipulate it more!