Return in a function

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
iantimmy123
Forum Newbie
Posts: 7
Joined: Sun Nov 12, 2006 6:27 am

Return in a function

Post by iantimmy123 »

I've started looking at using functions and am a bit confused about the purpose of return().

I have looked on the net but all the examples that I understand I can see other statements that can be used more effectively, or the code they give as an example doesn't work. It may be that the code I'm looking at is too simple as some excerpts where it is used is way above my current understanding.

I'm probably just having a blonde day but if someone has a way of explaining to me in a practical sense I would much appreciate it as it's really bugging me :D

Thanks
Ian
User avatar
aaronhall
DevNet Resident
Posts: 1040
Joined: Tue Aug 13, 2002 5:10 pm
Location: Back in Phoenix, missing the microbrews
Contact:

Post by aaronhall »

If a function is used in the context of an expression, the return value is used in that expression. Here's a little example:

Code: Select all

<?
function squareNumber($number) {
	$returnVal = $number*$number;
	return $returnVal;	
}

$num = 2;
$squaredNum = squareNumber($num);

// $squaredNum = 4
?>
the function squareNumber() takes $number and squares it. The $returnVal is assigned to $squaredNum automatically.
User avatar
BDKR
DevNet Resident
Posts: 1207
Joined: Sat Jun 08, 2002 1:24 pm
Location: Florida
Contact:

Post by BDKR »

It's actually most correct to say that whatever you are writing is a function only if it returns something. Otherwise, it's a sub-routine. :D

For all that matters.....

The purpose of return is simple. Return the output of the logic in question. You may need it to compute something for your right? Don't want to duplicate that code in 835 places in your application? Put it in a function. The result is what's returned back to you.

Something else that may be confusing you also is how the php manual may say something like....

Code: Select all

string sha1 ( string str [, bool raw_output] )

/* Or */

int crc32 ( string str )
As you can see above, the function names are sha1 and crc32 proper. So what's the 'string' and 'int' business in front of the function names? That's to tell you what variable type the functions will return. sha1() will return a string and crc32 will return an integer.

I trust you can take it from there. :D

Cheers,
BDKR
iantimmy123
Forum Newbie
Posts: 7
Joined: Sun Nov 12, 2006 6:27 am

Post by iantimmy123 »

lol right I've got it. Thanks guys
User avatar
Luke
The Ninja Space Mod
Posts: 6424
Joined: Fri Aug 05, 2005 1:53 pm
Location: Paradise, CA

Post by Luke »

I've started looking at using functions and am a bit confused about the purpose of return().
It should also be noted that return is not a function and therefor does not require the parentheses...

Code: Select all

function returnMe($value){
    return $value;  // See... no parentheses 
}
User avatar
BDKR
DevNet Resident
Posts: 1207
Joined: Sat Jun 08, 2002 1:24 pm
Location: Florida
Contact:

Post by BDKR »

The Ninja Space Goat wrote:
I've started looking at using functions and am a bit confused about the purpose of return().
It should also be noted that return is not a function and therefor does not require the parentheses...

Code: Select all

function returnMe($value){
    return $value;  // See... no parentheses 
}
Yep!
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post by Chris Corbyn »

it should also be noted that return isn't only used in functions. You can place it anywhere you want a value back from a call to something:

your_include.php

Code: Select all

<?php
return 42;
your_file.php

Code: Select all

<?php
$number = include("your_include.php");

echo $number * 10; //420
eval()'d code:

Code: Select all

<?php
$number = eval("return 7 * 3;");

echo $number; //21
Post Reply