Recursion in PHP PHP

Your functions may make calls to other functions, and they may also make calls to themselves. The process of a function calling itself is recursion. This circular definition usually leads to elegant algorithms. The problem is broken down into a small task that's repeated many times.

Recursive definitions are common in mathematics. Consider this definition of an integer: the sum or difference between one and any other integer, with one being an integer. Is three an integer? Yes, because one plus one must be an integer, which is two. And the sum of one and two must also be an integer.

Recursion is a difficult concept to understand, but it usually leads to clearcode. Take a look at The function checkInteger takes a number as input. We know that the difference between an integer and one is an integer. So if the function gets a number bigger than one, it simply checks the number minus one. If we start out with a number less than zero, we multiply it by negative one and check it. Eventually we will reach one or a number between zero and one, unless we are passed zero, which is an integer.

Using Recursion

Using Recursion

Using Recursion

Face Book Twitter Google Plus Instagram Youtube Linkedin Myspace Pinterest Soundcloud Wikipedia

All rights reserved © 2018 Wisdom IT Services India Pvt. Ltd Protection Status

PHP Topics