Solved

Recursive function problem

Posted on 2006-11-19
4
154 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
4 Comments
 
LVL 49

Accepted Solution

by:
Roonaan earned 125 total points
ID: 17974180
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
ID: 17974221
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
ID: 17974232
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
ID: 17974242
Okay, i understand. Thanks again.
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I imagine that there are some, like me, who require a way of getting currency exchange rates for implementation in web project from time to time, so I thought I would share a solution that I have developed for this purpose. It turns out that Yaho…
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…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

734 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