Solved

Recursive function problem

Posted on 2006-11-19
4
150 Views
Last Modified: 2006-11-19
function test($a) {
 if ($a > 10) {
  if ($a > 50) {
   test($a-10);
  }
  else {
   test($a-5);
  }
 }
 else {
  return $a;
 }
}

var_dump(test(100));

The above "example" function returns NULL.

Where am i going wrong?

Thank you in advance.
0
Comment
Question by:fox_km
  • 2
  • 2
4 Comments
 
LVL 49

Accepted Solution

by:
Roonaan earned 125 total points
Comment Utility
Use:

function test($a) {
 if ($a > 10) {
  if ($a > 50) {
   return test($a-10);
  }
  else {
   return test($a-5);
  }
 }
 else {
  return $a;
 }
}

-r-
0
 

Author Comment

by:fox_km
Comment Utility
Thank you.

Could you possibly explain why this works?

My logic was, you only want to return something if $a is less then 10. I'm assuming the extra returns serve the purpose of halting the execution of the current function call. I had assumed that it wouldn't matter, as those calls do not return anything, therefore the original function call would only have a value returned when $a <= 10. Is my understanding of recursive functions incorrect?
0
 
LVL 49

Expert Comment

by:Roonaan
Comment Utility
Well you would either return it, or store the subfunctions call into a:

function test($a) {
 if ($a > 10) {
  if ($a > 50) {
   $a = test($a-10);
  }
  else {
   $a = test($a-5);
  }
 }
 return $a;
}

When you just call text($a - 10) and do nothing with its result, there is no sense in calling the function at all.

-r-
0
 

Author Comment

by:fox_km
Comment Utility
Okay, i understand. Thanks again.
0

Featured Post

Easy Project Management (No User Manual Required)

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Suggested Solutions

Things That Drive Us Nuts Have you noticed the use of the reCaptcha feature at EE and other web sites?  It wants you to read and retype something that looks like this.Insanity!  It's not EE's fault - that's just the way reCaptcha works.  But it is …
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

771 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now