FRIHOST FORUMS SEARCH FAQ TOS BLOGS COMPETITIONS
You are invited to Log in or Register a free Frihost Account!


The keyword "return" in functions





pollux1er
Hi to everybody.
I want to understand the behaviour of the keyword return in functions. I have an example of code were its action seems to be not clear.
Code:

function connect()
      {
         $this->db = mysql_connect($this->host, $this->user, $this->password) or die("Database Connection Access Error..".mysql_error());
         if($this->db == false) return false;
         mysql_select_db($this->dbname, $this->db);
      }


After the keyword return does the remaining script should continue execution or not??

Thanks to all.
sheedatali
pollux1er wrote:
Hi to everybody.
Code:

function connect()
      {
         $this->db = mysql_connect($this->host, $this->user, $this->password) or die("Database Connection Access Error..".mysql_error());
         if($this->db == false) return false;
         mysql_select_db($this->dbname, $this->db);
      }


After the keyword return does the remaining script should continue execution or not??


The keyword return means return the control back to whoever called you. Functions are called at runtime, to do something/work, by default the sequence of commands in a function are executed in order until the last statement is executed, once this done, function by default returns control to where it was called from and the main program can continue. However there can be situations as shown in code above where you would want to do things inside the function only if certain criteria is met. In your case your function is connecting to a database, if the connection is successful, it is returning the database name it connected to successfully. However there is a check before last line to see if the connection successful or not, and if not successful it should return false. You main program should check if the function is returning something at all or just the false value. The code execution in function is stopped as the following condition is met.

Code:
if($this->db == false) return false;


No further action is taken and control is returned back. Otherwise your main program can run into trouble if the database connection was not made correctly and no checks are being made to test. I hope this helps.
Star Wars Fanatic
It also returns a variable to the place that called it.
For example if you called the function like so:

Code:
$var = connect ( ) ;


$var would be false if it gets to the return code.
You generally use false to say that the function didn't work correctly. If it doesn't then you can output an error. But you can also return any other type of variable, for example, a number, or a string, or just true to say that the function worked.
Peterssidan
If you are not interested in the returning type it's also possible to write
Code:
return;
instead of
Code:
return false;
Both stops the execution of the function but the first one doesn't return a value. (function is void)
Fire Boar
Really, either all control paths should return a value, or none. So return; is perfectly fine as long as you only use return;, return $var; is fine as long as you only use return $var; and there's no chance of the script reaching the end of the function without hitting a return somewhere.

So I'm not at all happy with that particular function, as a matter of course I'd either add "return true;" to the end, or change "return false;" to "return;". Probably the former.
pollux1er
I think with all what has been said, I know something more on the keyword return.
Related topics
Great php editor
C++ TUTORIAL : INTRODUCTION TO C++
Return Fire
Tutorial: Image Rollovers w/ Javascript
php image exif functions
Fortran Tutorials(77)
mysql table keyword search
Functional Programming
PHP Functions...
C++ Using class methods in inline functions? Scope problem?
Outside phpBB Integration Functions
re functions
What does the + symbol before new keyword does (tuts)
How to write your own OpenOffice Calc Functions
Reply to topic    Frihost Forum Index -> Scripting -> Php and MySQL

FRIHOST HOME | FAQ | TOS | ABOUT US | CONTACT US | SITE MAP
© 2005-2011 Frihost, forums powered by phpBB.