Returning Function Values

Not for 'how-to' coding questions but PHP theory instead, this forum is here for those of us who wish to learn about design aspects of programming with PHP.

Moderator: General Moderators

Post Reply
User avatar
snowrhythm
Forum Commoner
Posts: 75
Joined: Thu May 04, 2006 1:14 pm
Location: North Bay Area, CA

Returning Function Values

Post by snowrhythm »

i've heard that a function is always supposed to return something or else it's technically a subroutine.
what if i'm using a function in a class (a method) and it's just setting a member variable and not
really returning anything, am i supposed to

Code: Select all

return false;
or should I return that member
variable? it seems redundant to return a variable that can be used throughout the class once the function
has been called.
User avatar
Jenk
DevNet Master
Posts: 3587
Joined: Mon Sep 19, 2005 6:24 am
Location: London

Post by Jenk »

no, a function does not have to return a value.
User avatar
Burrito
Spockulator
Posts: 4715
Joined: Wed Feb 04, 2004 8:15 pm
Location: Eden, Utah

Post by Burrito »

a function can 'return' void.

in other languages you must explicitly define this such as:

Code: Select all

public void someMethod()
{
  //..
}

// vs

public string someMethod(string someVar)
{
  return someVar + " some appended string";
}
but with php such definitions are NOT required.
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post by John Cartwright »

if no return is specified, the function will return null regardless.
User avatar
Luke
The Ninja Space Mod
Posts: 6424
Joined: Fri Aug 05, 2005 1:53 pm
Location: Paradise, CA

Post by Luke »

User avatar
raghavan20
DevNet Resident
Posts: 1451
Joined: Sat Jun 11, 2005 6:57 am
Location: London, UK
Contact:

Post by raghavan20 »

i would say it does not really matter; if you want to return, return something else do not return.

eventhough technically one which returns is a function and which does not is a subroutine; it also depends on how programming languages consider it.

but it is important to return proper values
1. like making decisions: return TRUE or FALSE
2. if object, result set or some value is epcted: return value if available else return NULL

but there is no point in keep returning NULL if return is not expected by calling function.
Post Reply