The ($arg) function
<?php
function show_number($num){
$new_number = make_double($num);
echo( "The value is $new_number" );
}
function make_double($arg){
return $arg + $arg;
}
?>
PHP Function
<?php
show_number(12);
?>
Could some one please explain this code to in very simple terms. It does work but I would like to make perfect sense of it. The make_double($arg)is diffrent to in the show_number($num) function it is make_double($num). Why?
Thanks
decibel.places posted this at 16:22 — 16th February 2009.
He has: 1,494 posts
Joined: Jun 2008
hi busman,
the code calls the show_number() function and passes the value 12 to it
show_number(12);
the function assigns the passed value 12 to the variable $num
function show_number($num)
then it passes the value of $num which is 12 to the function make_double() and assigns the returned value (24) to a variable $new_number
$new_number = make_double($num);
the make_double() function adds $arg + $arg to double its value - the value of $arg is the value of $num which is 12 - and returns the sum (24) to the show_number() function as the value of $new_number
function make_double($arg){
return $arg + $arg;
}
function show_number() continues by echoing (or writing) the text plus the value of $newnumber that was returned by function make_double() - ie 24
echo( "The value is $new_number" );
however, I would remove the parentheses from the echo statement:
echo "The value is $new_number";
on the page you should see
I would rewrite the show_number() function - the make_double() function is unnecessary:
<?php
function show_number($num){
$new_number = $num + $num;
echo "The value is $new_number";
}
?>
busman posted this at 16:36 — 16th February 2009.
They have: 20 posts
Joined: Dec 2008
Thank you for all your help. I agree with your method, it make more sence and it is much more practical.
Thanks
greg posted this at 16:50 — 16th February 2009.
He has: 1,581 posts
Joined: Nov 2005
EDIT: decibel beat me to it (although I mentioned not using a function at all, which is fine (better) in most cases)
I don't have a fantastic knowledge with functions, so perhaps someone else will explain why some of that code is "required", but to me it appears to be extremely bloated.
To double a number, wouldn't this be better:
<?php
function double_number($num){
$num = $num * 2;
return $num;
}
echo double_number(12);
?>
Or instead of echoing you can add the value to a var
<?php
$doubled_number = double_number(12);
?>
Then echo it in whatever text you want
<?php
echo "My new number now doubled is ".$doubled_number;
?>
But to be honest, I don't see the point in ever using a function for something so simple unless you use it many times, which is the point of a function.
Simply having this would suffice in "most" cases and is much less use of resource for the server:
<?php
echo "My new number is "; echo 12 * 2;
?>
The only time you would really use the above in a function is if you are doubling many numbers throughout your page/script/site, and even then only if there were other calculations, such as Round, Ceil or Floor
Want to join the discussion? Create an account or log in if you already have one. Joining is fast, free and painless! We’ll even whisk you back here when you’ve finished.